读取“;”将分隔文件转换为字符数组结构

发布于 2024-11-08 17:12:29 字数 1221 浏览 3 评论 0原文

我正在尝试将文本文件中的一组值读取到数组结构的数组中。每个条目均由“\n”分隔,每个条目由 3 个值组成,由“;”分隔。

问题是,在正确读取文件数据的第一行后,程序从第二行读取第一个值,然后似乎无法读取剩余的值。你能指出我的语法或逻辑错误吗?

测试数据如下所示。

CS162;Finish Lab 2;9/26/2009
CS201;Take Quiz 1;9/28/2009

读入测试数据后,我的程序的输出如下。

Your tasks are:
Finish Lab 2 for CS162 is due 9/26/2009
CS201
 for  is due

将文件读入数组并输出数组内容的循环如下。我的完整代码将在问题的末尾。

for ( ; InputFile.peek() != EOF; ListSize++ )
{
      InputFile.get(TaskList[ListSize].Course, BUFFERSIZE, ';');
      InputFile.ignore(BUFFERSIZE, ';');
      InputFile.get(TaskList[ListSize].Assignment, BUFFERSIZE, ';');
      InputFile.ignore(BUFFERSIZE, ';');
      InputFile.get(TaskList[ListSize].DueDate, BUFFERSIZE, ';');
      InputFile.ignore(BUFFERSIZE, '\n');
}

cout << "Your tasks are:" << endl;
for ( int Iteration = 0; Iteration <= ListSize; Iteration++ )
{
    cout << TaskList[Iteration].Assignment << " for " << TaskList[Iteration].Course << " is due " << TaskList[Iteration].DueDate << endl;
}

完全公开,这是计算机科学课的内容。这就是为什么我不要求完整的代码解决方案,只是帮助解决逻辑或语法错误。如果我以完全错误的方式执行此操作,请指出我的文档来帮助我。但这确实对我的代码造成了限制。程序必须使用字符数组,而不是字符串。

I am trying to read a set of values from a text file into an array of structures of arrays. The entries are each separated by a '\n', and each entry consists of 3 values, separated by a ';'.

The problem is that after correctly reading the first line of file data the program reads the first value from the second line, then seems to fail to read the remaining values. Can you point out the error in my syntax or logic?

The test data appears below.

CS162;Finish Lab 2;9/26/2009
CS201;Take Quiz 1;9/28/2009

After reading in the test data my program's output is below.

Your tasks are:
Finish Lab 2 for CS162 is due 9/26/2009
CS201
 for  is due

The loops that read the file into the array and outputs the array contents are below. My complete code will be at the end of the question.

for ( ; InputFile.peek() != EOF; ListSize++ )
{
      InputFile.get(TaskList[ListSize].Course, BUFFERSIZE, ';');
      InputFile.ignore(BUFFERSIZE, ';');
      InputFile.get(TaskList[ListSize].Assignment, BUFFERSIZE, ';');
      InputFile.ignore(BUFFERSIZE, ';');
      InputFile.get(TaskList[ListSize].DueDate, BUFFERSIZE, ';');
      InputFile.ignore(BUFFERSIZE, '\n');
}

cout << "Your tasks are:" << endl;
for ( int Iteration = 0; Iteration <= ListSize; Iteration++ )
{
    cout << TaskList[Iteration].Assignment << " for " << TaskList[Iteration].Course << " is due " << TaskList[Iteration].DueDate << endl;
}

Full disclosure, this is for a computer science class. This is why I am not asking for complete code solutions, just help with logic or syntax errors. If I am doing this in completely the wrong way, please point me to documentation to help me. But this does put limitations on my code. The program must use character arrays, not strings.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

维持三分热 2024-11-15 17:12:29

也许最后的获取应该是:

InputFile.get(TaskList[ListSize].DueDate, BUFFERSIZE, '\n');

而不是

InputFile.get(TaskList[ListSize].DueDate, BUFFERSIZE, ';');

Your Last field (due date) 末尾没有分号,只有换行符。

更新:我建议您也考虑使用getline而不是get。它们具有类似的功能,但 getline 也会使用分隔符,这意味着您不需要使用ignore()。

Perhaps the last get should be:

InputFile.get(TaskList[ListSize].DueDate, BUFFERSIZE, '\n');

instead of

InputFile.get(TaskList[ListSize].DueDate, BUFFERSIZE, ';');

Your last field (due date) does not have a semicolon at the end, only a newline.

Update: I suggest you also look into using getline instead of get. They have similar functionality, but getline will consume the delimiter also, meaning that you won't need to use the ignore().

好倦 2024-11-15 17:12:29

在不考虑您编写的代码的情况下,我只想说我解决此类问题的正常模式是:

while (readline) { processline; 如果您没有完全正确的话,

增量文件处理更有可能遇到问题。

Without thinking about the code you have written, I'll just say that my normal pattern for this type of problem is:

while (readline) { processline; }

Incremental file processing is more likely to run into problems if you don't have everything exactly correct.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文