?对象:文件,控制台,特定数据类型stringstream
(写数据输出流out,读数据输入流in)
ofstream outfile;//输出流:从键盘输出数据,写入到文件
????????//文件打开默认位ios::out//字节覆盖写
????????//可以截断设置为:ios::out | ios::trunc//将之前文件全部截断为0,重新开始写//cout也是一种标准输出流
????????cout << "请输入姓名: ";
//cin是标准输入流,将控制台的数据读取,输入到变量name中
????????cin >> name;
#include <iostream>;
#include <fstream>
using namespace std;
int main(void) {
string name;
int age = 0;
ofstream outfile;//输出流:从键盘输出数据,写入到文件
//文件打开默认位ios::out//字节覆盖写
//可以截断设置为:ios::out | ios::trunc//将之前文件全部截断为0,重新开始写
outfile.open("user.txt",ios::out | ios::trunc);
while (true){
//cout也是一种标准输出流
cout << "请输入姓名: ";
//cin是标准输入流,将控制台的数据读取,输入到变量name中
cin >> name;
if (cin.eof())
{
break;
}
outfile << name << "\t";
cout << "请输入年龄: ";
cin >> age;
outfile << age<<endl;
}
outfile.close();
system("pause");
return 0;
}
?ifstream infile;//输入流:从文件读取数据,再由cout输出流,写到控制台?
#include <iostream>;
#include <fstream>
using namespace std;
int main(void) {
string name;
int age;
ifstream infile;
infile.open("user.txt");
while (true) {
infile >> name;
if (infile.eof())
{
break;
}
cout << name << "\t";
infile >> age;
cout << age << endl;
}
infile.close();
system("pause");
return 0;
}
note++查看二进制文件的插件:
?
?
#include <iostream>
#include <fstream>
using namespace std;
int main(void) {
string name;
int age;
ofstream outfile;
outfile.open("user.dat", ios::out | ios::trunc | ios::binary);
while (1){
cout << "请输入姓名:";
cin >> name;
if (cin.eof()) {
break;
}
outfile << name<<"\t";
//这里的age是整形的,在输出流之后自动被转换成字符串了
//这里写age需要指定格式
cout << "请输入年龄:";
cin >> age;
outfile.write((char*)&age, sizeof(int));
cout << endl;
}
outfile.close();
return 0;
}
?
#include <iostream>
#include <fstream>
using namespace std;
int main(void) {
string name;
int age;
ifstream infile;
infile.open("user.dat", ios::in | ios::binary);
while (1) {
infile >> name;
if (infile.eof()) {
break;
}
cout << "姓名:" << name << "\t";
char tmp;
infile.read(&tmp, sizeof(tmp));
infile.read((char*)&age, sizeof(age));
cout << "年龄:" << age << endl;
}
infile.close();
return 0;
}
?
#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;
int main(void) {
string name;
int age;
ofstream outfile;
outfile.open("user.txt", ios::out | ios::trunc);
while (1){
cout << "请输入姓名:";
cin >> name;
if (cin.eof()) {
break;
}
cout << "请输入年龄:";
cin >> age;
stringstream res;
res << "姓名:" << name << "\t\t\t年龄:" << age << endl;
//将数据输入字符流,转成字符串
outfile << res.str();
cout << endl;
}
outfile.close();
return 0;
}
用C语言的sscanf_s(对象,"指定格式",数据);
#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;
int main(void) {
string line;
int age;
char name[32];
ifstream infile;
infile.open("user.txt", ios::in);
while (1) {
getline(infile,line);
if (infile.eof()) {
break;
}
//用空格去取代制表符,这个格式和文件一行的格式一样
sscanf_s(line.c_str(), "姓名:%s 年龄:%d", name,sizeof(name), &age,sizeof(int));
cout << "姓名: " << name << "\t\t\t年龄: " << age << endl;
}
infile.close();
return 0;
}
?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
//返回文件最后五十个字节的数据
ifstream infile;
infile.open("偏移量.cpp");
if (!infile.is_open()) {
cout << "文件打开失败!" << endl;
return -1;
}
infile.seekg(-50, infile.end);
while (!infile.eof()) {
string name;
getline(infile, name);
cout << name << endl;
}
infile.close();
return 0;
}
??
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string name;
ofstream outfile;
outfile.open("偏移量.cpp",ios::app);
if (!outfile.is_open()) {
cout << "文件打开失败!" << endl;
return -1;
}
cout << "请输入一段话:";
cin >> name;
outfile.seekp(0, outfile.end);
outfile << name;
outfile.close();
return 0;
}//爱上大额女权
?距离文件起始位置的偏移量,计算大小
?
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream infile;
infile.open("偏移量.cpp");
if (!infile.is_open()) {
cout << "文件打开失败!" << endl;
return -1;
}
infile.seekg(0, infile.end);
cout <<"文件的大小:"<< infile.tellg() << endl;
infile.close();
return 0;
}
输入任意多个整数 , 把这些数据保存到文件 data.txt 中 .如果在输入的过程中 , 输入错误 , 则提示用户重新输入 .指导用户输入结束 ( 按 ctrl + z)[ 每行最多保存 10 个整数 ]
//将标准输入缓冲区cin的所有数据都清空,同时也会清除判定符号'\n'
cin.ignore(std::numeric_limits<streamsize>::max(), '\n');?
#include <iostream>
#include <fstream>
using namespace std;
/*
输入任意多个整数, 把这些数据保存到文件 data.txt 中.
如果在输入的过程中, 输入错误, 则提示用户重新输入.
指导用户输入结束(按 ctrl + z)
[每行最多保存 10 个整数]
*/
int main(void) {
int num;//s输入的数据
int n = 0;//用来计数
ofstream stream;
stream.open("data.txt",ios::out|ios::trunc);
if (!stream.is_open()) {
cout << "文件打开失败!" << endl;
exit(1);
}
//开始输入数据
while(1){
cout << "请输入一个整数:" ;
cin >> num;
if (cin.eof())
{
break;
}
if (cin.fail()){
cout << "输入错误请重新输入:";
cin.clear();
//将标准输入缓冲区cin的所有数据都清空,同时也会清除判定符号'\n'
cin.ignore(std::numeric_limits<streamsize>::max(), '\n');
cin >> num;
}
stream << num <<" ";
if ((++n)%10==0)
{
stream<< endl;
}
}
stream.close();
}
?从练习 2 中的 num.txt 文件读取各个整数, 打印出最大值和最小值, 以及平均值.
?
#include<iostream>;
#include <fstream>
using namespace std;
int main(void) {
int max, min, n = 0, sum = 0;
int num;
//使用输入流读取文件
ifstream stream;
stream.open("data.txt");
if (!stream.is_open()){
cout << "文件打开失败!" << endl;
exit(1);
}
stream >> num;
max = num;
min = num;
sum += num;
n++;
while (1){
stream >> num;
if (stream.eof()){
break;
}
if (num>max){
max = num;
}else if(num<min){
min = num;
}
sum += num;
n++;
}
cout << "max:" << max << endl;
cout << "min:" << min << endl;
cout << "avg:" << sum / n << endl;
cout << "n=" << n << endl;
}