Linux - NewbieThis 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!
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
I am trying to understand read() using a small piece of code
unsigned long word;
/* 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.
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.
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
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.
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.
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().
"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)?
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.
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
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 :-)