read and write

Write here if you have problems with your C source code

Moderator: Board moderators

Post Reply
Antonio Ocampo
Experienced poster
Posts: 131
Joined: Sat Jul 17, 2004 4:09 am
Location: Lima, Per

read and write

Post by Antonio Ocampo » Sun Jan 16, 2005 8:20 pm

Hi for all

Could someone give me a little example how to use read and write in C.

Thanks in advance :wink:

*luna*
New poster
Posts: 6
Joined: Tue Jan 11, 2005 9:12 pm
Location: Slovenia
Contact:

Post by *luna* » Sun Jan 16, 2005 8:55 pm

Hi Antonio!

here is an example how to read & write in c:

#include <stdio.h>
main()
{
int a; char b; float c;
scanf("%d %c %f",&a,&b,&c);
printf("%d %c %f \n", a, b, c);
}

with scanf you read
and with printf you write

if you need more explanation just tell. :)

Antonio Ocampo
Experienced poster
Posts: 131
Joined: Sat Jul 17, 2004 4:09 am
Location: Lima, Per

Re: read and write

Post by Antonio Ocampo » Sun Jan 16, 2005 11:21 pm

Antonio Ocampo wrote:Hi for all

Could someone give me a little example how to use read and write in C.
Well, obviously scanf is a way of read. I need read but with the function read() and write with the function write().

Thanks, anyway.

User avatar
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba » Fri Jan 21, 2005 1:11 pm

Please read the manual before posting to this forum. Use `man read 2' command on your system or google for it.

Antonio Ocampo
Experienced poster
Posts: 131
Joined: Sat Jul 17, 2004 4:09 am
Location: Lima, Per

Post by Antonio Ocampo » Sun Jan 23, 2005 4:28 am

I have read the header file, but I don't understand it :oops:
Please, help me. I'm a novice.

Thx in advance

User avatar
Krzysztof Duleba
Guru
Posts: 584
Joined: Thu Jun 19, 2003 3:48 am
Location: Sanok, Poland
Contact:

Post by Krzysztof Duleba » Sun Jan 23, 2005 5:37 am

This is the first hit from google:
http://unixhelp.ed.ac.uk/CGI/man-cgi?read+2
You can find there almost everything. Note that file descriptor number for stdin is 0 and 1 for stdout. A simple example:

Code: Select all

#include <unistd.h>

int main(){
     char buffer[1000];
     read(0, buffer, 1000);
     write(1, buffer, 1000);
     return 0;
}
If you're a novice, you'll be better off with scanf and fgets.

Antonio Ocampo
Experienced poster
Posts: 131
Joined: Sat Jul 17, 2004 4:09 am
Location: Lima, Per

Post by Antonio Ocampo » Sun Jan 23, 2005 7:28 pm

Thx for your reply Guru. :lol:
Bye

Post Reply

Return to “C”