英文:
reading a line into three variables
问题
我想从文本文件中读取行,并将信息存储在三个变量中。例如,如果文件中的行是
A Tale of Two Cities Charles Dickens 1859
我想将其存储到两个字符数组和一个整数变量中。因此,我想从该行中提取以下信息。该行中没有逗号或连字符等分隔符。
title[] = "A Tale of Two Cities"
author[] = "Charles Dickens"
year = 1859
如何在C中完成这个任务?问题在于title
和author
中的单词数量可能会变化。如果它们中的任何一个有固定数量的单词,那么问题就很容易解决。那么,在这里应该采用什么解决方案?
英文:
I want to read lines from a text file and store the information in three variables. For example, if the line from a file is
A Tale of Two Cities Charles Dickens 1859
I want to store this into two char arrays and one int variable. So, I want to extract the following information from the line. There is no delimiter like comma or hyphen in the line.
title[] = "A Tale of Two Cities"
author[] = "Charles Dickens"
year = 1859
How can this be done in C
? Problem is that title
could have variable number of words in it. Same for the author
. If there was fixed number of words in either of them, then problem is easy. So, what is the desirable solution here ?
答案1
得分: 2
以下是代码部分的翻译:
// Find the separation between title and author
char * sep1 = strstr(line, " ");
if (!sep1) fooey();
// Find the separation between the author and the year
char * sep2 = strrchr(sep1 + 2, ' ');
if (!sep2) fooey();
// Parse out the data
*sep1 = *sep2 = '// Find the separation between title and author
char * sep1 = strstr(line, " ");
if (!sep1) fooey();
// Find the separation between the author and the year
char * sep2 = strrchr(sep1 + 2, ' ');
if (!sep2) fooey();
// Parse out the data
*sep1 = *sep2 = '\0';
strcpy(title, line);
strcpy(author, sep1 + 2);
year = strotol(sep2 + 1, NULL);
';
strcpy(title, line);
strcpy(author, sep1 + 2);
year = strotol(sep2 + 1, NULL);
英文:
The fact that there are two spaces between the title and the author is a delimiter itself. Split on that:
// Find the separation between title and author
char * sep1 = strstr( line, " " );
if (!sep1) fooey();
// Find the separation between the author and the year
char * sep2 = strrchr( sep1+2, ' ' );
if (!sep2) fooey();
// Parse out the data
*sep1 = *sep2 = '\0';
strcpy( title, line );
strcpy( author, sep1+2 );
year = strotol( sep2+1, NULL );
答案2
得分: 1
如果这些文件具有预定义的文件格式,您最好找出该格式是什么;只有在这样做之后,您才能开始编写一个程序来读取这些文件。
另一方面,如果这些文件是您个人制定的,那么格式完全取决于您;但无论您选择什么格式,您都必须确保每个文件都符合它。在确定了格式后,您可以开始设计一个解析器来读取这些文件。
如果您事先不知道字段之间的空格数,那么您必须使用分隔符,这是一个明确标记该字段结束的符号。例如:
A Tale of Two Cities Charles Dickens 1859
可以是:
A Tale of Two Cities |Charles Dickens| 1859|
其中竖线符号是分隔符。然后,您可以通过查找分隔符来分隔每个字段,在忽略前导和尾随空格后。
英文:
If these files have a predefined file format, your best bet is to find out what that format is; only then will you be able to even begin writing a program that reads the files.
If, on the other hand, these files are something you personally made up, then the format is completely up to you; but whatever format you choose, you have to ensure that each file conforms to it. After you have decided this format, you can begin to design a parser that reads the files.
If you do not know in advance the number of spaces between fields, then you have to use a delimiter, which is a symbol that definitively marks the end of that field. For example:
A Tale of Two Cities Charles Dickens 1859
Could be:
A Tale of Two Cities |Charles Dickens| 1859|
Where the pipe symbol is the delimiter. Then, you can separate each field by looking for the pipe, ignoring leading and trailing spaces afterwards.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论