简体   繁体   中英

Using fgets to read through file in C

I am trying to read through the file given then tokenize it. The only problem im having is fgets.The file open recieves no errors. I have seen this elsewhere on the site however no matter how i set this up including setting fileLine to a set amount like (char fileline [200]) i get a segmentation fault. Thanks in advance for any help.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>

 int main(int argc, char *argv[]){
    char *fileName = "0";
    char *tokenize, *savePtr;
    struct Record *database= malloc(sizeof(database[0]));
    int recordNum =0;
    char *fileLine = malloc(sizeof(char *));//have replaced with fileline[200] still didnt work
    FILE *fd = open(fileName,O_RDWR);
    if(fd< 0){
        perror("ERROR OPENING FILE");
    }

    while(fgets(fileLine,200,fd) !=NULL){
        printf("%s\n", fileLine);
        tokenize = strtok_r(fileLine,",",&savePtr);
        while(tokenize != NULL){
         //TOKENIZING into a struct
        }
}

Why use open() with FILE ? Use fopen() instead.

#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[]) {
  char *fileName = "test.txt";
  char *tokenize, *savePtr;
  char fileLine[200] = {0}; // init this to be NULL terminated
  FILE *fd = fopen(fileName, "r");
  if (fd == 0) { // error check, equal to 0 as iharob said, not less than 0
    perror("ERROR OPENING FILE");
    return -1;
  }

  while (fgets(fileLine, 200, fd) != NULL) {
    printf("%s\n", fileLine);
    tokenize = strtok_r(fileLine, ",", &savePtr);
    while (tokenize != NULL) {
      tokenize = strtok_r(NULL, ",", &savePtr); // do not forget to pass NULL
      //TOKENIZING into a struct
    }
  }
  return 0;
}

As Weather Vane said, fd < 0 would work if you used open() . However, with fopen() , you should check to see if the pointer is NULL , ecquivalently fd == 0 .


A comparison between this functions that open a file can be found in:

  1. open and fopen function
  2. C fopen vs open

The way I have it in mind is that fopen() is of higher level.

This line

char *fileLine = malloc(sizeof(char *));

allocates memory for a char * type, 4 or 8 bytes (depending on the platform).

So when you do

fgets(fileLine,200,fd)

it expects there to be 200 bytes of memory available.

Try this:

char *fileLine = malloc(200);
if (fileLine == NULL) { ... }   // check for error

which will allocate the memory required.

You are using open() instead of fopen() .

You can't be sure that the file did open correctly because fopen() does not return an integer, but a pointer to a FILE * object, on failure it returns NULL , so the right codition is

FILE *file;

file = fopen(filename, "r");
if (file == NULL)
 {
    perror("fopen()");
    return -1;
 }

In your code, you still go and use fgets() even when the fopen() fails, you should abort the program in that case.

Also, malloc() takes the number of bytes as the size parameter, so if you want fgets() to be limited to read just count bytes, then malloc() should be

char  *buffer;
size_t count;

count  = 200; /* or a value obtained someway */
buffer = malloc(count);
if (buffer == NULL)
 {
    fclose(file);
    perror("malloc()");
    return -1;
 }

All the problems in your code would be pointed out by the compiler if you enable compilation warnings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM