开发者

Reading a ";" delimited file into a character array structure

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.


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().


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜