Reputation: 2299
I'm working on a basic command line music library in C that lets you open a file via command line, and add information like an artist, song title, and year published. Once it exits, it then writes that information back into the same file.
The problem I'm having is trying to find a solution to correctly parse the text file.
For example, the input file will look something like this:
Title: Heirloom, Artist: Basenji, Year Published: 2014
Title: With Me, Artist: Cashmere Cat, Year Published: 2014
The project I'm working on specifies (against common practice) that we store one line of information in a struct Song
that looks like this:
struct Song {
char title[250];
char artist[250];
int year_published;
};
Each Song
is stored in array of type struct Song
, called music_lib[]
.
I know how to separate each line into one specific struct Song
by doing:
while(fscanf(input_file, "%s %s %ld", *temp_title, *temp_artist, *temp_year) != EOF)
copy_song_to_music_library(temp_title, temp_artist, temp_year);
What I don't know how to do is how to properly parse the text file so that when I have a known format:
Title: Heirloom, Artist: Basenji, Year Published: 2014
For my title variable, I get "Heirloom" (and Title: is stripped out), for my artist variable, I get "Basenji" (with artist: stripped out), and for my year variable I get 2014 (with year published: stripped out).
Is there an easy way to do this?
Upvotes: 0
Views: 90
Reputation: 134356
You need to change
while(fscanf(input_file, "%s %s %ld", *temp_title, *temp_artist, *temp_year) != EOF)
to
while(fscanf(input_file, "Title: %s, Artist: %s, Year Published: %ld", *temp_title, *temp_artist, *temp_year) != EOF)
Also, you need to check for the return value of fscanf()
to ensure proper reading.
From the man page of fscanf()
. . . return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure.
Some related reference:
The signature of this (and family) function is
int fscanf(FILE *stream, const char *format, ...);
Where, const char *format
is described as
The format string consists of a sequence of directives which describe how to process the sequence of input characters.
and the expected format for format
is [emphasis mine]
A directive is one of the following:
• A sequence of white-space characters (space, tab, newline, etc.; see isspace(3)). This directive matches any amount of white space, including none, in the input.
• An ordinary character (i.e., one other than white space or '%'). This character must exactly match the next character of input.
• A conversion specification, which commences with a '%' (percent) character. A sequence of characters from the input is converted according to this specification, and the result is placed in the corresponding pointer argument. If the next item of input does not match the conversion specification, the conversion fails-this is a matching failure.
NOTE:
However, to make it more generalized, I'll recommend using fgets()
to take the input, then using strtok()
to tokenize the input and use.
Upvotes: 4