#Reading lines from file with whitespace

7 messages · Page 1 of 1 (latest)

tired oar
#

The program needs to:
Store the first 9 lines into a struct array at position 0
Store the following 9 lines into a struct array at position 1
Continue in that pattern^ until the end of the file

The issue I am having is with including whitespaces

An example file would look like this:

Line 2
Line 3```

An example struct would look like:
```struct example
{
char One[50];
char Two[50];
char Three[50];
}```
prime marshBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

tired oar
#
{
    struct frame
    {
        char lineOne[78];
        char lineTwo[78];
        char lineThree[78];
    };

    FILE *fp = fopen("AnimationFrames.txt", "r");

    // Program will run if the file is missing
    if (fp == NULL)
    {
        printf("Error: could not open file");
        mainMenu("\033[0;");
    }

    // Count the number of lines in the file
    char ch;
    int count = 0;
    do
    {
        ch = fgetc(fp);
        if (ch == '\n')
            count++;
    } while (ch != EOF);
    rewind(fp);

    unsigned short line = 1;
    unsigned short frameNumber = 1;
    struct frame Frames[60];
    // scan all of the lines inside the txt
    for (int i = 0; i < count; i++)
    {
        switch (line)
        {
        case 1:
            fscanf(fp, "%77[^\n]s", Frames[frameNumber].lineOne);
            line++;
            printf("case1\n");
            break;
        case 2:
            fscanf(fp, "%77[^\n]s", Frames[frameNumber].lineTwo);
            line++;
            printf("case2\n");
            break;
        case 3:
            fscanf(fp, "%77[^\n]s", Frames[frameNumber].lineThree);
            line = 1;
            frameNumber++;
            break;
        }

        printf("Line just read = %hu\n", line - 1);

        // printf("%s %s %lld %lld\n", cur_member[i].a, cur_member[i].b, cur_member[i].c, cur_member[i].d);
    }
    printf("Number of frames %hu\n", frameNumber);

    fclose(fp);

    for (int i = 1; i <= frameNumber; i++)
    {
        printf("\nFrame number = %d\n", i);
        fprintf(stdout, "%s\n", Frames[i].lineOne);
        fprintf(stdout, "%s\n", Frames[i].lineTwo);
        printf("%s\n", Frames[i].lineThree);
        printf("----------------------------------------------------------------------------\n");
    }
}```
#

The issue will be in this line:
fscanf(fp, "%77[^\n]s", Frames[frameNumber].lineOne);
I have tried a few variations of the format specifier
%77[^\n]s %77[^,]s %77[^,]\n %s %77[^\n]

#

Ultimately I just need to store an entire line in a single char array

#

Any help would be greatly appreciated 🙂

balmy badger
#

Is this meant to be C or C++?