Home > OS >  `C`: String not being updated properly when reading from a CVS file
`C`: String not being updated properly when reading from a CVS file

Time:03-12

I am given a text file of movie showtime information. I have to format the information in a clean way. Right now I'm just trying to get all line's information saved into strings. However, when getting the movie's rating the array wont save the rating properly.

This is the main code.

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

int main(void) {
    const int MAX_TITLE_CHARS = 44;  // Maximum length of movie titles
    const int LINE_LIMIT = 100;   // Maximum length of each line in the text file
    char line[LINE_LIMIT];
    char inputFileName[25];


    FILE *file;
    file = fopen("D:\\movies.txt", "r");

    char currentLine[LINE_LIMIT];
    char movieTitle[MAX_TITLE_CHARS];
    char movieTime[10];
    char movieRating[10];

    fgets(currentLine, LINE_LIMIT, file); // Get first file
    while(!feof(file)){

        sscanf(currentLine, "%[^,],D[^,],%s", movieTime, movieTitle, movieRating);

        printf("%s\n", movieRating);
        fgets(currentLine, LINE_LIMIT, file); // Get next file

    }

    return 0;
}

This is the CVS file

16:40,Wonders of the World,G
20:00,Wonders of the World,G
19:00,Journey to Space ,PG-13
12:45,Buffalo Bill And The Indians or Sitting Bull's History Lesson,PG
15:00,Buffalo Bill And The Indians or Sitting Bull's History Lesson,PG
19:30,Buffalo Bill And The Indians or Sitting Bull's History Lesson,PG
10:00,Adventure of Lewis and Clark,PG-13
14:30,Adventure of Lewis and Clark,PG-13
19:00,Halloween,R

This prints out

G
G
PG-13
PG-13
PG-13
PG-13
PG-13
PG-13
R

I need it to be

G
G
PG-13
PG
PG
PG
PG-13
PG-13
R

I use Eclipse and when in the debugger, I see that when it encounters the first PG-13, it doesn't update at all until the R. I'm thinking maybe since PG and PG-13 have the same two starting characters perhaps it gets confused? I'm not sure. Any help is appreciated.

CodePudding user response:

Your string 'Buffalo Bill...' is more than 44 characters. thus the sccanf statement reads up to that limit, it then looks for a ',', which doesn't exist 44 characters into the string and exits.

Because your new movieRating isn't being set, it just prints the previous value.

Hint: If you are looking for a work around, you can parse your string with something like strsep(); You can also just increase the size of your movie title.

CodePudding user response:

You are converting the line using the following line:

sscanf(currentLine, "%[^,],D[^,],%s", movieTime, movieTitle, movieRating);

the function will read a string into movietTime until a ',' appears in the input, then it will read another string until either a ',' appears or 44 characters are read. This behavior is explained in the manual for sscanf:

...
An optional decimal integer which specifies the  maximum  field  width.
Reading of characters stops either when this maximum is reached or when
a nonmatching character is found, whichever happens first...

The lines with PG ratings have titles with 62 characters. Thus, it does not read the entire title, and does not find the comma. To fix this issue, you can either set MAX_TITLE_CHARS to a greater value or use the %m modifier to have sscanf dynamically allocate the string for you.

  • Related