Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: write a file into a pen drive (usb) using c code in Linux

1,309 views
Skip to first unread message
Message has been deleted

Ian Collins

unread,
Jan 20, 2011, 1:33:29 AM1/20/11
to
On 01/20/11 07:03 PM, sonali sarange wrote:
> Hello everbody I have an task to write a c code for writing a file
> into USB drive via that code...

Where's the problem? Open a file on the drive's filesystem and write away.

--
Ian Collins

loozadroog

unread,
Jan 20, 2011, 3:28:28 AM1/20/11
to
On Jan 20, 12:03 am, sonali sarange <sonalisaran...@gmail.com> wrote:
> Hello everbody I have an task to write a c code for writing a file
> into USB drive via that code...
> Please help me out ASAP...

Assuming you have done a variation of the following:
attach drive to usb port
arranged for automounting -or-
waited for new device in /dev (on mine it's /dev/sda1 )
create a line in /etc/fstab , something like:
/dev/sda1 /mnt/usb1 vfat user 0 0
mount the device ( %mount /dev/sda1 )

Then you simply:
FILE *f = fopen("/mnt/usb1/myfilename", "w");
...

Message has been deleted

Richard Kettlewell

unread,
Jan 20, 2011, 6:39:41 AM1/20/11
to
sonali sarange <sonalis...@gmail.com> writes:
> hey thanks for replying...
> actually am new to Linux and dont know much tricks about Linux...
> Can you help more?
> If yes...?
> I want to write a C program for-
> Copying a file to the USB drive(Pendrive).

Don't write a program; use cp.

--
http://www.greenend.org.uk/rjk/

Message has been deleted
Message has been deleted
Message has been deleted
Message has been deleted

Richard Kettlewell

unread,
Jan 20, 2011, 7:13:17 AM1/20/11
to
sonali sarange <sonalis...@gmail.com> writes:
> Richard Kettlewell <r...@greenend.org.uk> wrote:
>> sonali sarange <sonalisaran...@gmail.com> writes:

>>> hey thanks for replying...
>>> actually am new to Linux and dont know much tricks about Linux...
>>> Can you help more?
>>> If yes...?
>>> I want to write a C program for-
>>> Copying a file to the USB drive(Pendrive).
>>
>> Don't write a program; use cp.
>

> But how could i recognize that Usb device

Widespread practice on many modern systems is that it is automatically
mounted at something like /media/<name>, where <name> is the filesystem
name. With a brand new device it might be the vendor or model name.

If you don't have this set up then you will have to identify the device
name (which will look something like /dev/sdb1) manually. Do this by
looking in the directories under /dev/disk or by looking in dmesg output
after insertion if you don't have /dev/disk. Having done this mount it
manually at the location of your choice; see 'man mount' for details.

--
http://www.greenend.org.uk/rjk/

Simon Roust

unread,
Jan 20, 2011, 1:25:02 PM1/20/11
to
> But how could i recognize that Usb device

They are mostly white or black, about 6cm long and have a shiny bit on
one end. Quite often it'll have USB written on it too.

loozadroog

unread,
Jan 20, 2011, 10:34:24 PM1/20/11
to
On Jan 20, 5:38 am, sonali sarange <sonalisaran...@gmail.com> wrote:
> hey thanks for replying...
> actually am new to Linux and dont know much tricks about Linux...
> Can you help more?
> If yes...?
> I want to write a C program for-
> Copying a file to the USB drive(Pendrive).
>
> Please do some more help

That sounds like the same question.
I think I've given you the information you need.
But you have to investigate the tools you have.

Get to a prompt.
It'll say something like
bash$
or
%
or it may have your current directory
/home/myusername/>
But whatever it looks like, it'll have a blinking cursor.
Then you can issue commands by typing them.

You can investigate the shell built-in commands with 'help'
% help cd
cd: cd [-L|-P] [dir]
Change the current directory to DIR. The variable $HOME is the
default DIR. The variable CDPATH defines the search path for
the directory containing DIR. Alternative directory names in
CDPATH
are separated by a colon (:). A null directory name is the same
as
the current directory, i.e. `.'. If DIR begins with a slash (/),
then CDPATH is not used. If the directory is not found, and the
shell option `cdable_vars' is set, then try the word as a variable
name. If that variable has a value, then cd to the value of that
variable. The -P option says to use the physical directory
structure
instead of following symbolic links; the -L option forces symbolic
links
to be followed.

Use 'cd' to change to the directory /dev .
% cd /dev
%
The prompt may or may not change. If there are no error messages,
then it worked! Most commands are silent.

Then use 'ls' to look at the contents of the directory.
% ls

Then, attach your USB drive. Wait a few seconds for the device
to become available. Then type 'ls' again and compare the output
from the first 'ls' with the new output. There should be a new
device possibly called sd?? (the '?' means some other character)
or usb? or mmc????. You may have to run 'ls' several times if
the computer is slow at noticing new devices.

Then you need to mount it to the filesystem.
% man mount

Barry Margolin

unread,
Jan 21, 2011, 8:54:26 AM1/21/11
to
In article
<b38546eb-96f1-4c5c...@o11g2000prf.googlegroups.com>,
sonali sarange <sonalis...@gmail.com> wrote:

> Hello everbody I have an task to write a c code for writing a file
> into USB drive via that code...
> Please help me out ASAP...

I think what you're not understanding from the earlier responses is that
there's nothing special about a USB drive on Unix. It's just another
filesystem that gets mounted on some directory, although it may be done
automatically when the drive is connected.

There's no special API for it, you just read and write files just like
any other directory.

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***

0 new messages