Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between read() and fgets() in C

Tags:

c

fgets

I want to read from a stdin stream. Is there any difference in using read() or fgets() to read from the stdin stream.

I am attaching the following two pieces of code with fgets and read. With fgets I can use a java program to write and read from the c program easily. With read and write my java program hangs waiting for the output from C program which does not come.

I am just reading a line keeping it in buf and appending A to it.

Java program is able to talk to the following program which works with fgets and puts.

#include <stdio.h>
#include <string.h>
#define SIZE  200000
main()
{
int rc;
int df;
int i;
char buf[SIZE];
for(i=0;i<=120000;i++) {
      memset(buf,'\0',SIZE);
      if(!fgets(buf,SIZE-1,stdin))
        continue;
      strcat(buf,"A_A_A_A_A_A_A");
      puts(buf);
}

}

but not with read() and write()

main()
{
int rc;
int df;
int i;
char buf[32768];
rc = fcntl(fileno(stdin), F_SETFL, O_NONBLOCK);
//rc = fcntl(fileno(stdout), F_SETFL, O_NONBLOCK);
FILE *fp;
for (;;) 
{
    int rc=-1;
    memset(buf,'\0',32768);
    //rc = fread(buf,5, 1, stdin);
    rc = read(fileno(stdin),buf,32768); 
    if (rc > 0)
    {
        strcat(buf,"B_B_B_B_B_B_B_B_B");
        write(fileno(stdout),buf,strlen(buf));

    }
}

}

Could some one tell the reason. I am still finding it hard to figure out

like image 336
ssD Avatar asked Jun 02 '11 20:06

ssD


People also ask

What is the difference between gets () and fgets ()?

gets() keeps reading input until newline character or end-of-file(EOF) shows up. This can lead to buffer overflow as it doesn't check array bounds of variable. While in case of fgets() it will also stop when maximum limit of input characters is reached.

What is the difference between functions fgets () and fgetc ()?

fgets() will read the whole string upto the size specified in argument list but when end of line occurs fgetc() returns EOF while fgets() returns NULL .

What is difference between scanf and fgets?

fgets() can read from any open file, but scanf() only reads standard input. fgets() reads 'a line of text' from a file; scanf() can be used for that but also handles conversions from string to built in numeric types.

What is the use of fgets in C?

Description. The fgets() function reads characters from the current stream position up to and including the first new-line character (\n), up to the end of the stream, or until the number of characters read is equal to n-1, whichever comes first.


2 Answers

  • fgets is a function, read is a system call
  • fgets is standard C, read is not
  • fgets is stdio buffered, read is not
  • fgets works with a FILE *, read works with a file descriptor
  • fgets reads until newline, read reads how much you tell it to

Need more ?

like image 112
cnicutar Avatar answered Oct 13 '22 18:10

cnicutar


There is an important alternative (fread) which sits somewhat in the middle, so the question should really be broken into two parts - and both are already well answered in SO:

What is the difference between fread and read?

What is the difference between fgets and fread?

Quick rule of thumb: use fgets if you intend to read textual data line by line, use fread elsewhere.

like image 42
leonbloy Avatar answered Oct 13 '22 20:10

leonbloy