C++程序设计文件输入和输出
1. 文本输入和输出
可以通过文本编辑器查看文本文件的内容
1.1向文件写入数据
可以用ofstream类向一个文本文件中写入基本数据类型值,数组,字符串和对象
#include <iostream>
#include <fstream>//头文件
using namespace std;
int main(){
ofstream output;//创建文件对象output
output.open("scores.txt");//如果文件已存在,会删除,不会报错
//以上两句可以写成ofstream output("sores.txt");
//写入两行文件
output << "ZhangSan" << " " << "Z" << " " << "S" << " " << "15" << endl;
output << "LiSi" << " " << "L" << " " << "S" << " " << "18" << endl;
//关闭输出流
output.close();//如果不调用此函数,数据不会真的写入磁盘
cout << "Done" << endl;
return 0;
//反斜线需要“\\”,(转义字符的使用)
}
1.2从文件读取数据
#include <iostream>
#include <fstream>//头文件
#include <string>
using namespace std;
int main(){
ifstream input("scores.txt");//输入流对象,等价于
//ifstream input;
//input.open("scores.txt");
//读取数据
string firstName;
char mi;
string lastName;
int score;
input >> firstName >> mi >> lastName >> score;
cout << firstName << " " << mi << " " << lastName << " " << score << " " << endl;
input >> firstName >> mi >> lastName >> score;
cout << firstName << " " << mi << " " << lastName << " " << score << " " << endl;
input.close();
cout << "Done" << endl
return 0;
//为了正确读写数据,要精确了解数据存储方式
}
1.3检测文件是否存在
检查文件是否存在,使用fail()函数,如果返回true,则不存在
`input.fail();`
1.4检测文件结束
调用eof()函数检测文件末尾
ifstream input("scores.txt");
double sum = 0;
double number;
while(!input.eof()){
input >> number;
if(input.eof()) break;//防止最后的数字后面有额外的字符,是程序不能正常工作
cout <<number << " " ;
sum += number;
}
1.5让用户输入文件名
有些编译器的open函数参数是c字符串,所以需要string.c_str()函数可将string转换为c_string
2. 格式化输出
流控制符可用于格式化控制台及文件输出
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main(){
ofstream output;
output.open("formattedscores.txt");
output << setw(6) << "ZhangSan" << setw(2) << "Z" << setw(6) << "S" << " " << setw(4) << "15" << endl;
output << setw(6) << "LiSi" << setw(2) << "L" << setw(6) << "S" << " " << setw(4) << "18" << endl;
//关闭输出流
output.close();
cout << "Done" << endl;
return 0;
//反斜线需要“\\”,(转义字符的使用)
}
3. 函数getline,get,put
函数getline可用来读入包含空格的字符串,函数get/put可用来读写单个字符
getline(ifstream& input,int string s ,char delimitChar)
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//将New York#New Mexico
//#Texas#Indiana
int mian(){
ifstream input("state.txt");
if(input.fail())
{
cout << "File does not exist" << endl;
return 0;
}
string city;
while(!input.eof()){
getline(input,city,'#');
cou << city << endl;
}
input.close();
cout << "Done" << endl;
return 0;
}
//输出每个单词
//get和put的用法
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
//将New York#New Mexico
//#Texas#Indiana
int mian(){
cout << "请输入被复制文件路径" << endl;
string inputFilename;
cin >> inputFilename;
cout << "请输入复制文件路径" << endl;
string outputFilename;
cin >> outputFilename;
ifstream input(inputFilename.c_str());
otstream output(outputFilename.c_str());
ifstream input("state.txt");
if(input.fail())
{
cout << "File does not exist" << endl;
return 0;
}
char ch = input.get();
while(!input.eof()){
output.put(ch);
ch = input.get();
}
input.close();
output.close();
cout << "\nCopy Done" << endl;
return 0;
}
4. fstream和文件打开模式
使用fstream创建既能输入又能输出的文件对象
//stream.open("city.txt",ios::out | ios::app)//指定多个文件的打开模式
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
fstream inout;
inout.open("city.txt",ios::out);//写入文件
inout << "Dallas" << " " << "Houston" << " " << "Atlanta" << " " ;
inout.close();
inout.open("city.txt",ios::out | ios::app);//追加内容至文件末尾
inout << "Savannah" << " " << "Austin" << " " << "Chicagp" << ;
inout.close();
string city;
inout.open("city.txt",ios::in);//读入文件内容
while(!inout.eof()){
inout >> city;
vcout << city << " ";
}
inout.close();
return 0;
}
5. 检测流状态
函数eof(),fail();good();bad();可用来检测流操作的状态
eof,到达文件末尾
fail,操作失败
bad,非法操作
good,操作成功
6. 二进制输入和输出
文件模式ios::binary可用于二进制输入输出方式打开文件
为了读写,所以需要使用write和read函数
write函数
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
fstream binary("temp.dat",ios::out|ios::binary);
int value =119;
binary.write(reinterpret_cast<char*>(&value),sizeof(value));//将int型地址转换为char*
//reinterpret_cast将任意数据类型转换为二进制字节,反之亦然
binary.close();
cout << "Done" ;
return 0;
}
read()函数
读和写类似,不过要注意c字符串需要在最后手动加入‘\0’
7. 随机访问文件
随机访问文件时,可用函数seekg()和seekp()移动文件指针到任意位置
seekg用于输入流
seekg(100,ios::beg) 文件开始第100个字节处
seekp(-100,ios:end) 文件末尾向后100个字节处
seekp用于输出流
seekp(42,ios::cur) 当前位置向前移动42个字节
seekp(-42,ios::cur) 当前位置向后移动42个字节
seekp(100) 文件第100个字节处
可以使用tellp和tellg函数返回文件指针的当前位置
8. 更新文件
为更新二进制文件,可用组合模式 ios::in | ios::out | ios::binary 打开该文件
就是 输入+输出 = 更新