如何把数据写入文件中?
在C++中如何把文字数据写入TXT文件中?
[解决办法]
ofstream ( );
explicit ofstream ( const char * filename, ios_base::openmode mode = ios_base::out );
Construct object and optionally open file
Constructs an object of the ofstream class. This implies the initialization of the associated filebuf object and the call to the constructor of its base class with the filebuf object as parameter.
Additionally, when the second constructor version is used, the stream is associated with a physical file as if a call to the member function open with the same parameters was made.
If the constructor is not successful in opening the file, the object is still created although no file is associated to the stream buffer and the stream 's failbit is set (which can be checked with inherited member fail).
Parameters
filename
C-string containing the name of the file to be opened.
mode
Flags describing the requested i/o mode for the file. This is an object of type ios_base::openmode, which consists on a combination of one or more of the following flags defined as member constants:
flag valueopening mode
app(append) Set the stream 's position indicator to the end of the stream before each output operation.
ate(at end) Set the stream 's position indicator to the end of the stream on opening.
binary(binary) Consider stream as binary rather than text.
in(input) Allow input operations on the stream.
out(output) Allow output operations on the stream.
trunc(truncate) Any current content is discarded, assuming a length of zero on opening.
Return Value
none
Example
// using ofstream constructors.
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream outfile ( "test.txt ");
// > > i/o operations here < <
outfile.close();
return 0;
}
This example simply opens and closes a file.
[解决办法]
ifstream is( "c:\\1.txt ");
is> > 123;
-------
应该是
ofstream os( "c:\\1.txt ");
is < <123;
[解决办法]
#include <iotream>
#include <fstream>
using namespace std;
int main()
{
ofstream outfile( "D:\\test.txt ");
outfile < < "helloworld ";
outfile.close();
return 0;
}
[解决办法]
#include <fstream>
ofstream outfile;
outfile.open( "e:\\text.txt ");
outfile < <str < <endl;
[解决办法]
写汉字也是一样的楼主。一个道理。
outfile < < "我们 ";
这样就可以了。
[解决办法]
#include <fstream>
ifstream in;
ifstream( "filename.txt ");
[解决办法]
还有就是要把中文字写入文件中,不是英文和数字数据
=====================
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream outfile ( "test.txt ");
outfile < < "This is a test!! " < <endl < <10 < <endl; // < <输入到文件中
outfile < < "还有就是要把中文字写入文件中,不是英文和数字数据!! " < <endl;
outfile.close();
return 0;
}
