Accessing files on a USB drive in C

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

User avatar
InkL0sed
Posts: 205
Joined: Wed Sep 10, 2008 3:46 am UTC

Accessing files on a USB drive in C

Postby InkL0sed » Thu Nov 26, 2009 5:09 am UTC

I'm sure there must be some simple way to do this, but I don't know how and the Oracle (aka Google) doesn't seem to understand.

So, how is it done? Can you just call fopen()? If so, how do you specify the path?

I'm on a Mac btw, so I'm looking for a Unix-y way to do it. I also happen to be running Linux virtually, so if there's a Linux specific way that you know of, that'd be cool to know of too.

fazzone
Posts: 186
Joined: Wed Dec 10, 2008 9:38 pm UTC
Location: A boat

Re: Accessing files on a USB drive in C

Postby fazzone » Thu Nov 26, 2009 5:12 am UTC

Um...mount it and use it like anything else?
*/

rflrob
Posts: 235
Joined: Wed Oct 31, 2007 6:45 pm UTC
Location: Berkeley, CA, USA, Terra, Sol
Contact:

Re: Accessing files on a USB drive in C

Postby rflrob » Thu Nov 26, 2009 5:42 am UTC

On a mac, all the external devices (including CDs, DVDs, and USB drives) get mounted in "/Volumes/" . I forget exactly what the command to cd or open is in C, but that should get you there. Do you know your drive will always have the same volume name? If not, you might need to go through more convoluted goodness to get the right thing.
Ten is approximately infinity (It's very large)
Ten is approximately zero (It's very small)

User avatar
InkL0sed
Posts: 205
Joined: Wed Sep 10, 2008 3:46 am UTC

Re: Accessing files on a USB drive in C

Postby InkL0sed » Thu Nov 26, 2009 2:35 pm UTC

Oh, I see. I feel stupid now.

User avatar
TheChewanater
Posts: 1279
Joined: Sat Aug 08, 2009 5:24 am UTC
Location: lol why am I still wearing a Santa suit?

Re: Accessing files on a USB drive in C

Postby TheChewanater » Sat Nov 28, 2009 4:53 am UTC

And you asked about Linux? Mounted media are in /media. Devices, namely storage devices ready to be mounted, are in /dev.
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Accessing files on a USB drive in C

Postby You, sir, name? » Sat Nov 28, 2009 1:00 pm UTC

TheChewanater wrote:And you asked about Linux? Mounted media are in /media. Devices, namely storage devices ready to be mounted, are in /dev.


Uh, no? Mounted media are where ever they are mounted. Which may be pretty much anywhere. The USB drive may be mounted in /usr/share/doc/SDL-1.2.13/html/ if someone mounted it there. The best way of figuring out where it is would be to check in /proc/mounts.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
TheChewanater
Posts: 1279
Joined: Sat Aug 08, 2009 5:24 am UTC
Location: lol why am I still wearing a Santa suit?

Re: Accessing files on a USB drive in C

Postby TheChewanater » Sat Nov 28, 2009 3:05 pm UTC

I meant the default mount point.
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.

User avatar
You, sir, name?
Posts: 6983
Joined: Sun Apr 22, 2007 10:07 am UTC
Location: Chako Paul City
Contact:

Re: Accessing files on a USB drive in C

Postby You, sir, name? » Sat Nov 28, 2009 6:45 pm UTC

TheChewanater wrote:I meant the default mount point.


The default mount point is wherever the default mount point is set in /etc/fstab. Which again may be just about anywhere imaginable, so long as it's a valid directory.
I edit my posts a lot and sometimes the words wrong order words appear in sentences get messed up.

User avatar
Earlz
Gets Obvious Implications
Posts: 785
Joined: Sat Jun 09, 2007 8:38 am UTC
Location: USA
Contact:

Re: Accessing files on a USB drive in C

Postby Earlz » Sat Nov 28, 2009 7:40 pm UTC

You, sir, name? wrote:
TheChewanater wrote:I meant the default mount point.


The default mount point is wherever the default mount point is set in /etc/fstab. Which again may be just about anywhere imaginable, so long as it's a valid directory.


I hardly ever have more than 1 external device mounted at a time(as in, thumb drive, SD card, CD, etc) so all of my mounts just go to /mnt. I use OpenBSD though.

I'm really not sure what the best way of doing an OS independent way of getting files off of a specific USB thumb drive because I sometimes even run my OS off of a thumb drive(on some computers).

Why not just let the user choose where the files are?
My new blag(WIP, so yes it's still ugly..)
DEFIANCE!
Image
This is microtext. Zooming in digitally makes it worse. Get a magnifying glass.. works only on LCD

User avatar
TheChewanater
Posts: 1279
Joined: Sat Aug 08, 2009 5:24 am UTC
Location: lol why am I still wearing a Santa suit?

Re: Accessing files on a USB drive in C

Postby TheChewanater » Sat Nov 28, 2009 7:51 pm UTC

Earlz wrote:I'm really not sure what the best way of doing an OS independent way of getting files off of a specific USB thumb drive because I sometimes even run my OS off of a thumb drive(on some computers).


Yeah, and I think it even varies between different versions or distros.

Better (or worse?) yet, why not use the native filechooser? Unless you plan to use it on different OS's, in which case a cross platform GUI library with a file chooser widget would probably be in order.
ImageImage
http://internetometer.com/give/4279
No one can agree how to count how many types of people there are. You could ask two people and get 10 different answers.


Return to “Coding”

Who is online

Users browsing this forum: Steeler [Crawler] and 3 guests