c ++在txt文件中获取一行数据

时间:2013-02-26 03:06:10

标签: c++

我想从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 个答案:

答案 0 :(得分: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。

答案 1 :(得分: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";
  }
}

答案 2 :(得分: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
}

答案 3 :(得分: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( );
};

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