【问题标题】:Reading and Writing Filestreams C++读取和写入文件流 C++
【发布时间】:2014-04-25 05:52:11
【问题描述】:

我在将用户输入保存到 Txt 文件时遇到问题。不知道我做错了什么;它没有写入文件:

void Menu::nowShowingDecisions()
{
    switch (userInput)
    {
    case 1:
        system("cls");
        xyPosition(0, 0);
        CAtitleHeader();
        CAtitleMenu();
        getUserInput();
        CAtitleDecisions();

        break;
        return;

    case 2:
    {
        string userName;
        string password;
        {
            ofstream outFile;
            {
                outFile.open("C:\\Test\\Test.txt");
                if (!outFile.good())
                    cout << "File Could Not Be Opened." << endl;
                else
                {
                    cout << "Enter Username:";
                    cin >> userName;

                    cout << "Enter Password:";
                    cin >> password;
                    while (cin >> userName >> password)
                        outFile << userName << password << endl;

                    outFile.close();
                }
            }
        }
        return;
        {
            const int COL_SZ = 10;
            ifstream inFile;
            inFile.open("C:\\Test\\Test.txt");
            if (!inFile.good())
                cout << "File could not be opened" << endl;
            else
            {
                cout << left;
                cout << "Movie Ticket Accounts" << endl;
                cout << setw(COL_SZ) << "User Name" << setw(COL_SZ) << "Password" << endl;
                while (inFile >> userName >> password)
                {
                    cout << setw(COL_SZ) << userName << setw(COL_SZ) <<
                    password << setw(COL_SZ) << endl;
                }

                inFile.close();
                return;
            }
        }
    }
    break;

【问题讨论】:

  • 请尽量保持标签一致。混沌标签的代码很难阅读。

标签: c++ text file-io filestream


【解决方案1】:

在这段代码中

             cout << "Enter Username:";
              cin >> userName;             // BEING IGNORED

              cout << "Enter Password:";
              cin >> password;             // BEING IGNORED
              while (cin >> userName >> password) // THIS IS BEING SAVED>
                  outFile << userName << password << endl;

您没有将第一个 userNamepassword 写入输出文件。

尚不清楚您是否真的想要在其中包含while 循环。如果您只想写出第一个用户名和密码,则需要将其更改为:

              cout << "Enter Username:";
              cin >> userName;

              cout << "Enter Password:";
              cin >> password;
              if (cin) 
                  outFile << userName << password << endl;

【讨论】:

  • 非常感谢!对 while 循环感到困惑
【解决方案2】:

使用 C++ 读取或写入 txt 可以像这样简单的方式完成。

// 写入文本文件

 #include <iostream>
 #include <fstream>

 using namespace std;

 int main () 

 {
   ofstream myfile ("example.txt");

    if (myfile.is_open())
 {
    myfile << "This is a line.\n";
    myfile << "This is another line.\n";
   myfile.close();
 }
   else cout << "Unable to open file";
    return 0;
}

// 读取文本文件

 #include <iostream>
 #include <fstream>
 #include <string>
 using namespace std;

 int main () 

 {
   string line;
   ifstream myfile ("example.txt");
  if (myfile.is_open())
  {
    while ( getline (myfile,line) )
   {
      cout << line << '\n';
    }
  myfile.close();
 }

  else cout << "Unable to open file"; 

 return 0;
 }

【讨论】:

    猜你喜欢
    • 2019-01-24
    • 2015-07-26
    • 2019-04-25
    • 1970-01-01
    • 2011-11-21
    • 2013-03-17
    • 2020-04-16
    • 2014-12-29
    • 2016-02-11
    相关资源
    最近更新 更多