LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-25-2006, 07:09 PM   #1
smoothdogg00
Member
 
Registered: Feb 2006
Location: Maine
Distribution: Ubuntu
Posts: 44

Rep: Reputation: 15
Help with the read() function in C


I need help with the read() function in C. I am trying to read a file one character at a time using the open() and read() functions, but I am getting an infinite loop. The open() function I have below returns a "3", meaning the file is opened successfully. However, the read() function returns -1 all the time, sending it into an endless loop when it shoudl be returning the number of bytes read at any given moment until the EOF.
Code:
#include <stdio.h>
#include <fcntl.h>

main(int argc, char * argv[])
{
  char *buf;
  int bytesRead;
  int fp;
  fp = open("test", O_RDWR, "rb");
  do{
    bytesRead = read(fp, buf, 1);
    printf("%d", bytesRead);
    }
  while(bytesRead != 0);
  close(fp);

  exit(0);
}
 
Old 09-25-2006, 07:19 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
"-1" indicates an error condition, or EOF.

You should definitely check for "-1" in your read call, and you should definitely #include <errno.h> and check the value of "errno". You might also wish to call "perror()" for bona fide error conditions (but not, of course, for run-of-the-mill EOF conditions).
 
Old 09-25-2006, 07:21 PM   #3
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
buff is a pointer to a char but you dont point it to anything. Remeber in c you cannot have varible length strings.


you should inilize it like this

buff = malloc(sizeof(char));

but thats just silly defining it as
char buff;
would make more sense if your only trying to get one byte of information.
 
Old 09-25-2006, 07:41 PM   #4
smoothdogg00
Member
 
Registered: Feb 2006
Location: Maine
Distribution: Ubuntu
Posts: 44

Original Poster
Rep: Reputation: 15
The description of read():
Code:
read()
	#include  <fcntl.h>
	int  read(  int  handle,  void  *buffer,  int  nbyte );
Can someone please explain the arguments for the read function? What should *buffer be declared as and what is the int handle (I do understand the use of nbyte.)?
 
Old 09-25-2006, 08:44 PM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Exvor's right (as was I). You need to:

1. Allocate a buffer: either "malloc()", or declare some "char myarray[BUFSIZE]" on the stack.

2. Open the file.
Make sure the file is opened successfully.
Print error status (errno and/or perror) if it fails.

3. Read from the file; again checking the return value and again checking error status.
This time, however, you need to distinguish between
a) a successful read (#/bytes read == #/bytes expected)
b) a partial read (#/bytes read < #/bytes expected)
c) EOF (#/bytes == -1, but errno == 0)
d) An I/O error (#/bytes == -1, errno is nonzero)

4. Close the file

STYLE POINT:
I would use the variable name "fd" (integer "file descriptor") for open/read/close, vs the name "fp" (FILE "file pointer"), which I would use for "fopen/fread/fclose".

As far as the arguments: it should be pretty self-explanatory:

- fd: file descriptor (of some file you opened)
- buffer: (array of bytes, or a struct, you allocated)
- nbytes: (the #/bytes you'd like to read (there may or may not be that many bytes available)


'Hope that helps .. PSM
 
  


Reply



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
Bad data in socket read function nhydra Programming 2 05-08-2006 03:16 AM
source code for read() function ? Mike Davies Linux - Software 1 11-03-2004 02:10 PM
read and pipe function dummyagain Programming 13 09-24-2003 10:36 PM
Read() function In "socket.h" RAW_STREAM Penguinizer Programming 3 02-22-2003 01:14 AM
file function in linux: read() minh2003 Programming 1 10-08-2002 01:23 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:58 PM.

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
Open Source Consulting | Domain Registration