写入文件
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
ifstream ifile;
ifile.open("file.txt");
char temp[30];
ifile>>temp;
cout<<temp;
return 0;
}
读取文件:
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
ofstream file;
file.open("file.txt");
file<<"this is a test string!";
file.close();
return 0;
}
file对象调用提取运算符>>从文件中一次读取每个字符并写入到字符数组temp中。由于提取运算符遇到空格即停止,空格和空格后面的字符均被存放到输入缓冲区中,因此指挥在屏幕中输出this。
读取空格及空格后面的字符:
将file>>temp换成file.getline(temp,24);