简体   繁体   English

C++ I/O 在 EOF 后重新读取文件(原为:解析逗号分隔的文本文件)

[英]C++ I/O Rereading a file after EOF (was: Parsing a Comma Delimited Text File)

Lets take a look at the code:让我们来看看代码:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <cstdlib>


using namespace std;


int main()
{
    string usrFileStr,
    fileStr = "airNames.txt",  // declaring string literal
    sLine;                        // declaring a string obj

    fstream inFile;                  // declaring a fstream obj
    char ch;

    cout << "Enter a file: ";
    cin >> usrFileStr;


    inFile.open( usrFileStr.c_str(), ios::in ); 
    // at this point the file is open and we may parse the contents of it


    while ( !inFile.eof() )
    {
          getline ( inFile, sLine ); // store contents of txt file into str Obj
          for ( int x = 0; x < sLine.length(); x++ )
          {         
              if ( sLine[ x ] == ',' )break; // when we hit a comma stop reading 
              //cout << sLine[ x ];
          }
          cout << endl;
    }      



        while ( !inFile.eof() )  //read the file again until we reach end of file
        {
                // we will always want to start at this current postion;
              inFile.seekp( 6L, ios::cur );

              getline( inFile, sLine ); // overwrite the contents of sLine
              for ( int y = 0; y < sLine.length(); y++ )
              {
                  if ( sLine[ y ] == ',' )break; // hit a comma then goto seekp until eof
                  cout << sLine[ y ];
              }
              cout << endl;
        }

    inFile.clear();
    inFile.close();



    fgetc( stdin );
    return 0;
}

My text file format is similar to this:我的文本文件格式类似于:

string, string andthenspace, numbers, morenumbers

It doesn't look like I'm able to read the file twice.看起来我无法读取文件两次。 Checking for EOF each time..每次检查EOF ..

The first while condition works, and it gives me what I need, the first field before the comma, and not including the comma.第一个 while 条件有效,它给了我我需要的东西,逗号前的第一个字段,不包括逗号。

So the second time I thought, ok just the seekp(X, ios::cur) function to start there on each iteration of the second while..所以我第二次想,好吧,在第二次的每次迭代中,只用seekp(X, ios::cur)函数从那里开始。

Unfortunately, it's not reading the file a second time..不幸的是,它不会第二次读取文件..

You probably need to clear the error bit on the stream after reading EOF the first time.第一次读取 EOF 后,您可能需要清除流上的错误位。 That is, as well as doing the seek operation.即,以及执行查找操作。

First, never test eof in your while loops, the flag will be true after you hit the end of the file, ie too late, the test must be done just after the read, and before using what you expect to have read.首先,永远不要在你的 while 循环中测试 eof,在你到达文件末尾该标志将为真,即为时已晚,测试必须在读取之后和使用你期望读取的内容之前完成。 -> ->

while (std::getline(......))

Then, Jonathan Leffler gave you the solution , clear the state bits before the call to seekg.然后,Jonathan Leffler 为您提供了解决方案,在调用 seekg 之前清除状态位。 However, your approach seems somewhat complex as Jamie pointed out .但是,正如杰米指出的那样,您的方法似乎有些复杂。

You seem to be going the wrong way about it:你似乎走错了路:

FILE * file;
file = fopen ("file.txt","r");


if (file!=NULL){
        while (!feof(file)) {           
                fscanf(file,"%s ,%s ,%s ,%s",str1,str2,str3,str4);
        }
        fclose (file);
};

You can obviously expand the above example, I haven't had time to check it up it should work.您显然可以扩展上面的示例,我没有时间检查它应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM