简体   繁体   English

读写文件流C ++

[英]Reading and Writing Filestreams C++

I'm having a problem getting the user input to be saved into a Txt file. 我在将用户输入保存到Txt文件中时遇到问题。 Not sure what I am doing wrong; 不知道我在做什么错; it is not writing to the file: 它没有写入文件:

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;

In this block of code 在这段代码中

             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;

You are not writing the first userName and password to the output file. 您没有将第一个userNamepassword写入输出文件。

It's not clear whether you wanted to really have the while loop in there. 尚不清楚您是否真的想在那里使用while循环。 If you want to write out just the first username and password, you need to change it to: 如果您只想写出第一个用户名和密码,则需要将其更改为:

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

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

Reading or Writing to a txt using C++ can be done in a simple way like this. 使用C ++读取或写入txt可以通过这种简单方式完成。

// writing on a text file //在文本文件上写

 #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;
}

// reading a text file //读取文本文件

 #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