samedi 27 juin 2015

Reading text file of unknown size

I am trying to read in a text file of unknown size into an array of characters. This is what I have so far.

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

    int main()
    {
            FILE *ptr_file;
            char buf[1000];
        char output[];
            ptr_file =fopen("CodeSV.txt","r");
            if (!ptr_file)
                return 1;   

        while (fgets(buf,1000, ptr_file)!=NULL)
            strcat(output, buf);
        printf("%s",output);

    fclose(ptr_file);

    printf("%s",output);
        return 0;
}

But I do not know how to allocate a size for the output array when I am reading in a file of unknown size. Also when I put in a size for the output say n=1000, I get segmentation fault. I am a very inexperienced programmer any guidance is appreciated :)

The textfile itself is technically a .csv file so the contents look like the following : "0,0,0,1,0,1,0,1,1,0,1..."

Aucun commentaire:

Enregistrer un commentaire