解析具有多个公共分隔符的文件 C
Parse a file with multiple common delimiters C
我正在使用 C 编写一个基本的命令行音乐库,它允许您通过命令行打开文件,并添加艺术家、歌曲名称和出版年份等信息。一旦退出,它就会将该信息写回到同一个文件中。
我遇到的问题是试图找到正确解析文本文件的解决方案。
例如,输入文件如下所示:
Title: Heirloom, Artist: Basenji, Year Published: 2014
Title: With Me, Artist: Cashmere Cat, Year Published: 2014
我正在从事的项目指定(与惯例相反)我们将一行信息存储在 struct Song
中,如下所示:
struct Song {
char title[250];
char artist[250];
int year_published;
};
每个 Song
存储在 struct Song
类型的数组中,称为 music_lib[]
。
我知道如何将每一行分成一个特定的struct Song
,方法是:
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);
我不知道如何正确解析文本文件,以便当我有已知格式时:
Title: Heirloom, Artist: Basenji, Year Published: 2014
对于我的标题变量,我得到 "Heirloom"(并且标题:被删除),对于我的艺术家变量,我得到 "Basenji"(艺术家:被删除),以及我的年份我得到 2014 年的变量(发布年份:删除)。
有没有简单的方法来做到这一点?
你需要改变
while(fscanf(input_file, "%s %s %ld", *temp_title, *temp_artist, *temp_year) != EOF)
到
while(fscanf(input_file, "Title: %s, Artist: %s, Year Published: %ld", *temp_title, *temp_artist, *temp_year) != EOF)
此外,您需要检查 fscanf()
的 return 值以确保正确读取。
来自fscanf()
的man page
. . . 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.
一些相关参考:
这个(和家族)函数的签名是
int fscanf(FILE *stream, const char *format, ...);
其中,const char *format
被描述为
The format string consists of a sequence of directives which describe how to process the sequence of input characters.
format
的预期格式是 [强调我的]
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.
注意:
但是,为了使其更通用,我建议使用 fgets()
获取输入,然后使用 strtok()
对输入进行标记并使用。
我正在使用 C 编写一个基本的命令行音乐库,它允许您通过命令行打开文件,并添加艺术家、歌曲名称和出版年份等信息。一旦退出,它就会将该信息写回到同一个文件中。
我遇到的问题是试图找到正确解析文本文件的解决方案。
例如,输入文件如下所示:
Title: Heirloom, Artist: Basenji, Year Published: 2014
Title: With Me, Artist: Cashmere Cat, Year Published: 2014
我正在从事的项目指定(与惯例相反)我们将一行信息存储在 struct Song
中,如下所示:
struct Song {
char title[250];
char artist[250];
int year_published;
};
每个 Song
存储在 struct Song
类型的数组中,称为 music_lib[]
。
我知道如何将每一行分成一个特定的struct Song
,方法是:
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);
我不知道如何正确解析文本文件,以便当我有已知格式时:
Title: Heirloom, Artist: Basenji, Year Published: 2014
对于我的标题变量,我得到 "Heirloom"(并且标题:被删除),对于我的艺术家变量,我得到 "Basenji"(艺术家:被删除),以及我的年份我得到 2014 年的变量(发布年份:删除)。
有没有简单的方法来做到这一点?
你需要改变
while(fscanf(input_file, "%s %s %ld", *temp_title, *temp_artist, *temp_year) != EOF)
到
while(fscanf(input_file, "Title: %s, Artist: %s, Year Published: %ld", *temp_title, *temp_artist, *temp_year) != EOF)
此外,您需要检查 fscanf()
的 return 值以确保正确读取。
来自fscanf()
的man page
. . . 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.
一些相关参考:
这个(和家族)函数的签名是
int fscanf(FILE *stream, const char *format, ...);
其中,const char *format
被描述为
The format string consists of a sequence of directives which describe how to process the sequence of input characters.
format
的预期格式是 [强调我的]
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.
注意:
但是,为了使其更通用,我建议使用 fgets()
获取输入,然后使用 strtok()
对输入进行标记并使用。