0

このコードは、c++ でファイルに書き込むために作成しました。

string Text;
ofstream Write_Test;("Write_Test.txt"); //Creating the file

Write_Test.open("Write_Test");  //Opening it for writing to, the ios::trunc at the end means it will open the file if it already exists, and overwrite any existing data with the new data.
while (Write_Test.is_open())
{
     Write_Test << Text;    //Write a message to the file.
}

Write_Test.close(); //Close the stream, meaning the file will be saved.
cout << "Done!";

しかし問題は、文字列の最初の単語だけをファイルに書き込んでいることです...

変数 'Text' を自分の名前に割り当てて 'Callum Holden' とすると、テキスト ファイルに Callum が書き込まれるだけですか?

誰かが私を正しい方向に向けることができますか?

4

1 に答える 1

1

それはそれと同じくらい簡単です:

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

int main()
{
    string Text = "text";
    ofstream Write_Test("Write_Test.txt");//Creating the file

    Write_Test << Text;   //Write a message to the file.
    Write_Test.close();   //not necessary in this case   
    if ( Write_Test.good() )
        cout << "Done!" << endl;
    else            
        cerr << "Not done!";
}
于 2013-09-20T15:38:49.547 に答える