tags:

views:

285

answers:

9

Which method can be used to read one line at a time from a file in C.

I have used fgets function also. but it's not working. it's reading space separated token only...

What to do ???

+1  A: 

fgets() should be the way to go …

Didier Trosset
as long as the buffer is big enough
Tom
Tom: That's what the second parameter is for, to avoid overrunning the buffer. Or do you mean not foreseeing a big enough buffer? IIRC, my C compiler has a 512 byte limit on individual lines.
Arthur Kalliokoski
Which C compiler is that?. And I'm thinking on the case where you dont know how long the line is, e.g stdin. Not trying to start a holy war on this.
Tom
+2  A: 

The fgets function will read a single line from a file or num characters where num is the second parameter passed to fgets. Are you passing a big enough number to read the line?

For Example

// Reads 500 characters or 1 line, whichever is shorter
char c[500];
fgets(c, 500, pFile);

Vs.

// Reads at most 1 character
char c;
fgets(&c,1,pFile);
JaredPar
yes.my buffer length is 100.So I guess its sufficient..
Mew 3.2
Incorrect declaration of array variable!
tommieb75
@JaredPar: Cool! You fixed it! :P Hey, I upvoted after downvoting from 0 to -1, then upvoted as you rectified it, to bring it back up to zero, instead it jumped to 1...is that my browser or a glitch?
tommieb75
+1  A: 

use either fgets if you know that your lines will fit into buffer or use fgetc for more control over reading

Andrey
+3  A: 

If you are coding for a platform that has the GNU C library available, you can use getline():

http://www.gnu.org/s/libc/manual/html_node/Line-Input.html

Ant
+1  A: 

This is more of a comment than a complete answer, but I don't have enough points to comment. :)

Here's the function prototype for fgets():

char *fgets(char *restrict s, int n, FILE *restrict stream);

It will read n-1 bytes or up to a newline or eof. For more info see:

http://www.opengroup.org/onlinepubs/009695399/functions/fgets.html
Mike Yam
+1  A: 

This should work, when you can't use fgets() for some reason.

int readline(FILE *f, char *buffer, size_t len)
{
   char c; 
   int i;

   memset(buffer, 0, len);

   for (i = 0; i < len; i++)
   {   
      int c = fgetc(f); 

      if (!feof(f)) 
      {   
         if (c == '\r')
            buffer[i] = 0;
         else if (c == '\n')
         {   
            buffer[i] = 0;

            return i+1;
         }   
         else
            buffer[i] = c; 
      }   
      else
      {   
         //fprintf(stderr, "read_line(): recv returned %d\n", c);
         return -1; 
      }   
   }   

   return -1; 
}
LukeN
thanx buddy its wokring but still I have a problem..My buffer is reading and storing entire linebut while printing its printing only first space separted token only
Mew 3.2
That's strange, I can just use `printf("%s\n", buffer);` and it outputs the whole buffer
LukeN
yeh,its strange for me also. because it was working for all previous programs... :'(
Mew 3.2
Are you sure that your file does not contain any string terminators?
LukeN
+1  A: 

Use fgets to read from the line, and then use getc(...) to chew up the newline or end-of-line to continue reading....here's an example of forever reading a line...

// Reads 500 characters or 1 line, whichever is shorter
char c[500], chewup;
while (true){
    fgets(c, sizeof(c), pFile);
    if (!feof(pFile)){
        chewup = getc(pFile); // To chew up the newline terminator
        // Do something with C
    }else{
        break; // End of File reached...
    }
}

Hope this helps, Best regards, Tom.

tommieb75
+2  A: 

Use the following program for getting the line by line from a file.

#include <stdio.h>
int main ( void )
{
char filename[] = "file.txt";
FILE *file = fopen ( filename, "r" );
if (file != NULL)
{
char line [1000];
while(fgets(line,sizeof line,file)!= NULL) /* read a line from a file */
{
fprintf(stdout,"%s",line); //print the file contents on stdout.
}
fclose(file);
}
else
{
perror(filename); //print the error message on stderr.
}
return 0;
}
rekha_sri
A: 

Source of Error:

Actually it was not my fault.. In this case . I was using strtok function and and accidently modified my original my original string. Hence while printing I was getting the error...

Thanks Everyone for Helping me.. :)

Mew 3.2