-1

I would like to create a function to read file line by line. One every line is one name.

int readConfig(char ** path, FILES ** files )
{
    FILE* f;
    f = fopen("file", "r");   
    int ch;
    while ((ch=fgetc(f)) != EOF )
    {

    }
return 0;
}

How to use the fgetc function to parse the file? And how to get the results to the files[count].name?

John Boe
  • 3,501
  • 10
  • 37
  • 71

2 Answers2

3

Right off the bat:

char configFile [11] = "kernels.cfg";

[11] is too small. Try:

char configFile [12] = "kernels.cfg";

or

char configFile [] = "kernels.cfg";          /* let the compiler do the counting */

Also char is too small for ch -- use:

int ch;

You may also find fgets() -- which reads a whole line at at time -- simpler to use than fgetc().

John Hascall
  • 9,176
  • 6
  • 48
  • 72
2

You are getting SIGSEGV because of modifying string literal and that causes an undefined behavior (e.g. your SIGSEGV). I am not sure what should be stored in filename and name variables. If by line:

strcpy(files[count].filename,'.bin');

you've meant to add a '.bin' to filename variable, then this approach is wrong. You should use strcat. strcpy would write to filename from beginning of this variable, so some chars previously saved there would be overwritten. strcpy also adds a null termination char, so if you wanted to print it out, printf would stop on that \0 char and won't go further. However, the real problem is that you should allocate with malloc some space for your variables in struct. Then you will be able to modify them.

Consider simple example:

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

struct file {
    char* name;
    char* filename;
};

int main(void)
{
    struct file x;
    x.name = malloc(30);
    x.filename = malloc(40);

    strncpy(x.name, "copied_string", 13);
    printf("%s\n", x.name);

    strcat(x.name, "_suffix"); 
    printf("%s\n", x.name);

    strcpy(x.name, "erased");
    printf("%s\n", x.name);

    free(x.name);
    free(x.filename);
    return 0;
}

output:

copied_string
copied_string_suffix
erased

This should make it a little more clear what's the origin of your crash. You should also consider using fgets or getline. Remember to free what you've malloc'd.

EDIT:

Calling readConfig(&path, &files); results in passing to readConfig a pointer of type FILES (*)[256]. Consider changing FILES files[256]; to

FILES* files = malloc(sizeof(FILES)*256);

and later call your function like readConfig(&path, files);

Then you would pass to readConfig function a compatible type of files.

macfij
  • 3,093
  • 1
  • 19
  • 24
  • Thank you. But the strncpy is not necessary is it? I use strcpy to copy name to .name and to copy .name to .filename. However still I am hitting the error when I try to copy '.bin'. Ah, this is the problem. There should be ".bin" , not single quotes. But how should I deallocate the memory, should I use free(files) or create a function with a loop do deallocate all members of its elements ... free(files[count],name);free(files[count],filename); I believe the second is correct. Really thanks for the free() notice! – John Boe Sep 11 '14 at 11:52
  • 1
    strncpy is not necessary, but it is safer :) You should deallocate explicitly what you have allocated. If member of struct was allocated with malloc, freeing the struct won't free the memory that belongs to that member (if even the struct was also allocated with malloc, it could be just a variable on stack). – macfij Sep 11 '14 at 11:58
  • I've given some hints for you. – macfij Sep 11 '14 at 21:28