簡體   English   中英

讀寫文件流C ++

[英]Reading and Writing Filestreams C++

我在將用戶輸入保存到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;

在這段代碼中

             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;

使用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;
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM