簡體   English   中英

C ++文件輸出僅接受第一個單詞

[英]C++ file output only accepts the first word

我正在嘗試將字符串輸出到基本的.txt文件。 而且它僅部分起作用,並且我的意思是說它只接受我鍵入的所有內容的第一個單詞。 我還需要沒有字符數限制(用戶可以輸入任意數量的文字)

#include <fstream>
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{

    ifstream in_stream;
    ofstream out_stream;

    in_stream.open("advice.txt");
    if (in_stream.fail())
    {
     cout << "input file opening failed.\n";
     exit(1);                     
    }


    char next;

    in_stream.get(next);
    while (! in_stream.eof())
    {
     cout << next;
     in_stream.get(next);      
    }


    out_stream.open("advice.txt", ios::app); //Append data to file
    if (out_stream.fail())
    {
     cout << "output file opening failed.\n";
     exit(1);                     
    }


    //Output text into the file (Problem is in here)

    string mystring;

    cin >> mystring, "\n\n";
    out_stream << " - " << mystring << "\n\n";

    in_stream.close();
    out_stream.close();

    cin.get();
    cin.get();
    return 0;

}

任何未來的幫助將不勝感激:)預先感謝!

提取運算符在空白處截斷。 使用std :: getline()讀取整行。

代替:

cin >> mystring, "\n\n";

做了:

std::getline( std::cin, mystring );

暫無
暫無

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

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