0

我想从 txt 文件中读取数据,但我无法获取它。我是 C++ 新手。这是我的代码,但它不起作用。我用 getline(),

 ifstream inFile;
 string sPassWord;
 inFile.open("QdatPassWordconfig.config");
 inFile.seekg(0,ios::end);
 int length=inFile.tellg();

  if (inFile.is_open())
  {
      while (!inFile.eof())
      {
              getline(inFile,sPassWord);
               cout<<sPassWord<<endl;
      }

         cout<<"get data from txt file"<<endl;
         // here ,I cannot read data from file
         cout<<sPassWord<<endl;
  }

 if(!inFile.is_open() || length==0)
  {
      cout<<"file is create or write"<<endl;
      sPassWord="BdsWUjT26";
      ofstream outFile;
      outFile.open("QdatPassWordconfig.config");
      outFile<<sPassWord<<endl;
      outFile.close();
  }
   inFile.close();
  cout<<sPassWord<<endl;
4

4 回答 4

2

不清楚您是在尝试读取文件的第一行、文件的最后一行还是文件的所有行。这里是每种可能性的程序片段:

读取文件的第一行:

// UNTESTED
{
  ifstream inFile("QdatPassWordconfig.config");
  string sPassWord;
  if(std::getline(inFile, sPassWord)) {
    std::cout << "Password is: " << sPassWord << "\n";
  } else {
    std::cout << "No password available.\n"
  }
}

要读取文件的所有行:

// TESTED
#include <iostream>
#include <fstream>
#include <string>

int main ()
{
  std::ifstream inFile("QdatPassWordconfig.config");
  std::string sPassWord;
  while(std::getline(inFile, sPassWord)) {
    std::cout << "Password is: " << sPassWord << "\n";
  }
}

要读取文件的最后一行:

// UNTESTED
{
  ifstream inFile("QdatPassWordconfig.config");
  string sPassWord;
  int lineCount = 0;
  while(std::getline(inFile, sPassWord)) {
    lineCount++;
  }
  if(lineCount) {
    std::cout << "Password is: " << sPassWord << "\n";
  } else {
    std::cout << "No password available.\n";
  }
}
于 2013-02-26T03:19:39.140 回答
2
inFile.seekg(0,ios::end);
int length=inFile.tellg();

1.你忘了寻找回到起点。像这样:

inFile.seekg(0,ios::end);
int length=inFile.tellg();
inFile.seekg(0,ios::beg);

2.你需要练习你的if和else语句。

3.不要使用std::ifstream::eof。使用 std::getline。

于 2013-02-26T03:14:05.350 回答
1

做这样的事情:

// Declare local variables
std::ifstream inFile;
std::string sPassword = "";
::UINT length = 0;

// Attempt to open file
inFile.open( "QdatPassWordconfig.config" );

// Use your if and else statement like this:
// Is inFile open?
if( inFile.is_open( ) )
{
    // Read file line by line using std::getline
    while( std::getline( inFile, sPassword ) ) {
        // Print sPassword
        std::cout << sPassword << std::endl;
    }
    // Done with inFile, close it
    inFile.close( );
}
else
{
    // Do whatever if inFile can't be open
}
于 2013-02-26T03:23:25.277 回答
0

您的代码有很多错误,所以我决定向您展示我将如何做到这一点(请阅读评论):

void Example( void )
{
    // DECLARATION
    bool bInputMode = true;
    std::fstream ioFile;
    ::UINT nFileSize = 0;
    std::string strPassword = "";

    // INITIALIZATION
    // *Open or create ioFile
    // ioFile can now do both input and output operations
    ioFile.open( "Passwords.pw",
                 std::fstream::in |std::fstream::out | std::fstream::app );

    // *Calculate/set the value of bInputMode
    // first, calculate the size of the file
    // if the size of the file is = 0,
    // bInputMode = false - which means to be in output mode
    ioFile.seekg( 0, std::ios::end );
    if( ( nFileSize = ioFile.tellg( ) ) = 0 )
        bInputMode = false;
    ioFile.seekg( 0, std::ios::beg );

    // DO WHATEVER
    // *Since bInputMode == true,
    // we shall read each line from ioFile by using std::getline
    if( bInputMode )
    {
    // *Get each line within ioFile and "copy" it to strPassword
    // and then print strPassword
    // *With std::getline, we could get the spaces
        while( std::getline( ioFile, strPassword ) )
            std::cout << strPassword << std::endl;
    }
    // *Since bInputMode == false,
    // we shall create a new from ioFile and then write to it
    else
    {
        std::cout << "Creating/writing a new file..." << std::endl;
        strPassword = "Password123";
        ioFile << strPassword << std::endl;
    }

    // CLEAN-UP
    // We are done with ioFile, close it.
    ioFile.close( );
};

请指出任何错误!一些反馈和建议也会很棒。

于 2013-02-26T04:23:09.337 回答