2

I still consider myself as a new webby in C.I am trying to read a file in, file is not binary but its size varies from small size of feb kbs to big size files of feb Mbs.I am using fgets function, i had taken reference from this link but after compilation, i am getting segmentation fault.i tried to debug it with gdb and found that i am able to open file but unable to read.Here is my code.

    #include<stdio.h>
    #define MAX_LENGTH 1048576
    int main()
    {
    FILE *fp;
    char *result;
    char line[MAX_LENGTH];
    fp =fopen("/home/shailendra/sampleprograms/C/shail1.txt","r");
    if(result=fgets(line,MAX_LENGTH,fp) != NULL)
    printf("The string is %s \n",result);

    else
    printf("Error opening the file");

    if(fclose(fp))
    printf("fclose error");
    }

This Segmentation fault really sucks your blood.I understand that it is due to insufficient memory allocation but i had used MAX_LENGTH 1048576, so i don't think that it must create any problem.I had tried it with both small files with only one line and a big file with multiple lines but i am unable to figure out why i am getting segmentation fault.

I also looked at this and this but got no help.

Community
  • 1
  • 1
shailendra
  • 271
  • 2
  • 6
  • 18
  • If you're on a POSIX-compliant system then use `stat` to get the file size and allocate exactly that amount of memory as a buffer (that's if you must read all the file in one go). – trojanfoe Aug 30 '13 at 11:57

2 Answers2

3

Try some parentheses:

if((result=fgets(line,MAX_LENGTH,fp)) != NULL)
   ^                                ^

Without those you'll store the result of the comparison instead of a pointer to the string.


Side note: you don't need result at all. When it succeeds, fgets returns the pointer you passed in; in other words you can just printf line:

if(fgets(line, MAX_LENGTH, fp))
    printf("The string is %s \n", line);

Second side note: before trying to fgets from the file, you should check whether fopen succeeded:

if (!fp)
    perror("fopen");
cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • yes,after putting () and giving a space between #include , my code works.It may be another question, my code is still reading a string means after it gets "\n", it stops, so is there any function which can remove "\n" as there is a function in perl "chomp".I know i am aksing this without even trying. – shailendra Aug 30 '13 at 12:56
  • @shailendra There's no builtin `chomp` function but you could try a simple `line[strlen(line) - 1] = 0;`. If you want a real chomp, there's a [famous question](http://stackoverflow.com/questions/122616/how-do-i-trim-leading-trailing-whitespace-in-a-standard-way). – cnicutar Aug 30 '13 at 13:25
0

Couple of things in addition to cnicutar's answer:

#include <stdio.h>
        ^ Please make sure you have a space between your includes and the headers

fgets will read until it's encountered a new line or EOF (whichever comes first), so to read the whole file you'd have to do it in a loop.

I would recommend doing it like this (no need for the pointer assignment as pointed out):

while(fgets(line, MAX_LENGTH, fp))
    printf("%s", line);
Nobilis
  • 7,310
  • 1
  • 33
  • 67