LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-03-2012, 01:22 PM   #1
abhishekgit
Member
 
Registered: Jan 2012
Location: India
Distribution: Ubuntu, Gentoo, Fedora, Rhel5,openSUSE
Posts: 165

Rep: Reputation: 12
Regarding a system call read()


Hello everyone,
I am trying to understand read() using a small piece of code
Code:
unsigned long word;
ssize_t nr;
/* read a couple bytes into 'word' from 'fd' */
nr = read (fd, &word, sizeof (unsigned long));
if (nr == -1)
        /* error */
Here 'len' i.e sizeof(unsigned long) is read from the file descriptor fd into word. What exactly does this mean? Will the read data be present in the file referenced by fd after the input given by the user? How to print the read data? Kindly help. Thank you.
Regards...
 
Old 02-03-2012, 01:35 PM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 840

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Quote:
Originally Posted by abhishekgit View Post
Here len' i.e sizeof(unsigned long) is read from the file descriptor fd into word. What exactly does this mean? Will the read data be present in the file referenced by fd after the input given by the user? How to print the read data? Kindly help. Thank you.
Regards...
Hi, I am not sure I understand what you mean. The read call will retrieve sizeof( unsigned long ) bytes from the file referenced by the fd file descriptor and store it in the variable word.
So in your example:
fd is a file to read from,
word is the variable to store the data
sizeof(unsigned long) is the number of bytes to be read.
By reading, the contents of the file will remain unchanged. The position in the file, however, will be updated, so the consequent calls to read will retrieve different data from the file. You can print the data simply by

Code:
printf("data: %lu\n", word);

Last edited by millgates; 02-03-2012 at 01:38 PM.
 
Old 02-03-2012, 01:44 PM   #3
abhishekgit
Member
 
Registered: Jan 2012
Location: India
Distribution: Ubuntu, Gentoo, Fedora, Rhel5,openSUSE
Posts: 165

Original Poster
Rep: Reputation: 12
I wasn't clear.

@millgates
If i enter the data to be read, It will be stored in the variable word as you said. But then, what about the file referenced by the file descriptor fd?, Is the data entered stored in the file? I am not understanding the name of the function read() here which generally means to read data from a file which is already present in the file referenced by fd despite me entering the data to be read.
 
Old 02-03-2012, 01:51 PM   #4
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 840

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Ok. Now I am totally confused. What are you trying to do?
If you want to store data in a file, you should be using write(), rather than read().
If, on the other hand, you want to get data from the file, what do you mean by "entering data to be read"?
Perhaps if you explained what are you trying to achieve in the first place, it would make your questions more clear.
 
1 members found this post helpful.
Old 02-03-2012, 02:05 PM   #5
abhishekgit
Member
 
Registered: Jan 2012
Location: India
Distribution: Ubuntu, Gentoo, Fedora, Rhel5,openSUSE
Posts: 165

Original Poster
Rep: Reputation: 12
@millgates
Alright. No i am just studying about system programming. "entering data" i mean when i call read(), it waits for the user to enter data. Thats what is getting me confused. When i input a string from the keyboard, The same is displayed using the printf("%lu", word). What is the job of the file referenced by fd in that case. And when i call write() the program waiting for the user to input data makes more sense than it does when you call read().
 
Old 02-03-2012, 02:31 PM   #6
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 840

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Quote:
Originally Posted by abhishekgit View Post
"entering data" i mean when i call read(), it waits for the user to enter data. Thats what is getting me confused.
It shouldn't. read() doesn't do that, as far as I know. Isn't the problem in a different part of the program?
Oh, wait -- doesn't by accident the file descriptor you pass to read() happen to be stdin (0)?
 
Old 02-05-2012, 08:05 AM   #7
abhishekgit
Member
 
Registered: Jan 2012
Location: India
Distribution: Ubuntu, Gentoo, Fedora, Rhel5,openSUSE
Posts: 165

Original Poster
Rep: Reputation: 12
Okay...This modification?

@millgates
I am pretty convinced but to ensure it is working correctly i modified it as

Code:
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<unistd.h>
#include<stdio.h>

main()
{
	int fd;
	ssize_t nr;
	unsigned long word;
	fd = open ("/home/abhishek/pirate.txt", O_RDONLY);
	nr = read(fd, &word, sizeof(unsigned long);
	printf("Data read is %lu", word);
	
	
	
}
Where pirate.txt is the existing file i edited to check this exapmle, it contains an integer as 7733. When i compile the above code using
Code:
$ gcc read.c -o read
$ ./read
The output is some garbage value
Code:
Data read is 926037815
I expect 7733 as the output(if i am correct).
Kindly help. Thanks
 
Old 02-05-2012, 09:34 AM   #8
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 840

Rep: Reputation: 380Reputation: 380Reputation: 380Reputation: 380
Quote:
Originally Posted by abhishekgit View Post
Where pirate.txt is the existing file i edited to check this exapmle, it contains an integer as 7733.
By that you mean "The file contains binary data 7733 (dec? hex?)" or "The file contains an ascii text '7733'"?
Also, when dealing with binary data, be careful about the order of bytes in your file. I tried various combinations of text/binary files and byte orders, with both the 32 and 64 bit executable, but haven't found the one that gives your particular number 926037815.

Last edited by millgates; 02-05-2012 at 09:36 AM.
 
1 members found this post helpful.
Old 02-05-2012, 01:43 PM   #9
abhishekgit
Member
 
Registered: Jan 2012
Location: India
Distribution: Ubuntu, Gentoo, Fedora, Rhel5,openSUSE
Posts: 165

Original Poster
Rep: Reputation: 12
I just punched in 7733

@millgates
Sorry i am completely new to all this, so its a little hard to me to grasp about binary data of which you are talking about and also the order of bytes. What i am trying to do is, I edited a text file pirate.txt, a text file where i just typed 7733 and saved it. I opened it using open() and i am reading data from the file pirate into the variable word and trying to re-display it by the printf statement. The data being read is 926037815 instead of 7733 which is the actual content. If you could please give a small example so as to read the content from a text file
to a variable and print the same using read(), That would be helpful. Thanks
 
Old 02-05-2012, 02:01 PM   #10
abhishekgit
Member
 
Registered: Jan 2012
Location: India
Distribution: Ubuntu, Gentoo, Fedora, Rhel5,openSUSE
Posts: 165

Original Poster
Rep: Reputation: 12
Thanks so much!!

@millgates
Hey, thanks so much for your support! I finally got it. I was supposed to use the variable as an array of characters. I was declaring it as unsigned long type and looking for it to print character data. The data was read perfectly. Thanks again :-)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Read System Call is getting blocked when tried to read the data from CDC device sanju.lnt Linux - Embedded & Single-board computer 0 09-12-2011 12:48 AM
The READ System Call In UNIX balaIT Linux - General 6 10-30-2010 04:57 PM
Trying to modify read() system call Anduril07 Linux - Newbie 1 05-07-2009 08:01 AM
Problem using 'read' system call buckles Programming 6 05-10-2005 12:45 PM
Problem w/ 'read' system call - RH 7.3 buckles Linux - Newbie 3 05-07-2005 01:03 AM


All times are GMT -5. The time now is 07:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration