LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Regarding a system call read() (https://www.linuxquestions.org/questions/linux-newbie-8/regarding-a-system-call-read-927442/)

abhishekgit 02-03-2012 12:22 PM

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...

millgates 02-03-2012 12:35 PM

Quote:

Originally Posted by abhishekgit (Post 4593039)
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);

abhishekgit 02-03-2012 12:44 PM

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.

millgates 02-03-2012 12:51 PM

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.

abhishekgit 02-03-2012 01:05 PM

@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().

millgates 02-03-2012 01:31 PM

Quote:

Originally Posted by abhishekgit (Post 4593065)
"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)?

abhishekgit 02-05-2012 07:05 AM

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

millgates 02-05-2012 08:34 AM

Quote:

Originally Posted by abhishekgit (Post 4594291)
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.

abhishekgit 02-05-2012 12:43 PM

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

abhishekgit 02-05-2012 01:01 PM

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. :D The data was read perfectly. Thanks again :-)


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