您的位置:首页 > 其它

文件输入输出

2020-04-05 07:25 120 查看

ASIIC文件的操作:
从键盘读取10个整数 到磁盘f1.dat,再从磁盘f1.dat读取10个整数并输出

// ConsoleApplication11.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include<fstream>
#include <iostream>
#include<algorithm>
using namespace std;
int in[10], out[10];
//从键盘输入10个整数,然后存放到磁盘中
void input(char *filename)
{
ofstream outfile("f1.dat", ios::out);
if (!outfile)
{
cerr << "打开错误!" << endl;
exit(1);
}
cout << "读入10个整数到磁盘文件:" << endl;
for (int i = 0; i < 10; i++)
{
cin >> in[i];
outfile << in[i] << " " << endl;
}
outfile.close();
}
//从磁盘读取文件并输出
void output(char *filename)
{
ifstream infile("f1.dat", ios::in);
if (!infile)
{
cerr << "打开错误" << endl;
exit(1);
}
cout << "磁盘文件读取10个整数:" << endl;
for (int i = 0; i < 10; i++)
{
infile >> out[i];
cout << out[i] << " ";
}
cout << endl;
infile.close();
}
int main()
{
char a[20] = "f1.dat";
input(a);
output(a);
return 0;
}

从键盘读取一行字符,把其中的字母字符依次存放在磁盘文件f2.dat中,再把它从磁盘文件读入程序,将其中的小写字母依次改为大写字母,再存入f3.dat中。

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

//读入一行字符并将其中的字母存入磁盘文件
void save(char *filename)
{
char s[80];
ofstream outfile(filename, ios::out);
if (!outfile)
{
cerr << "打开错误!" << endl;
exit(1);
}
cout << "输入字符串保存到磁盘f2.dat" << endl;
cin.getline(s, 80);
for (int i = 0; i < strlen(s); i++)
{
if (s[i] >= 'a'&&s[i] <= 'z' || s[i] >= 'A'&&s[i] <= 'Z')
outfile << s[i];
cout << s[i];
}
cout << endl;
outfile.close();
}
//从磁盘f2.dat读取字符串到程序,将其中的大写改为小写再存入f3.dat中
void get(char *filename1,char *filename2)
{
char s;
ifstream infile(filename1, ios::in);
if (!infile)
{
cerr << "打开f2.dat错误!" << endl;
exit(1);
}
ofstream outfile(filename2, ios::out);
if (!outfile)
{
cerr << "打开f3.dat错误!" << endl;
exit(1);
}
while (infile >> s)
{
if (s>= 'a'&&s <= 'z')
{
s = s - 32;
outfile << s;
}

}
cout << endl;
infile.close();
outfile.close();
}
//输出f3.dat中的大写字母
void put(char *filename)
{
char a;
ifstream infile(filename, ios::out);
if (!infile)
{
cerr << "打开文件失败!" << endl;
exit(1);
}
cout << "磁盘中输出" << endl;
while (infile >> a)
{
cout << a;
}
cout << endl;
infile.close();
}
int main()
{
char a[100]="f2.dat", b[100]="f3.dat";
save(a);
put(a);
get(a,b);
put(b);
return 0;
}

将一批数据以二进制形式存放在磁盘文件中

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

struct Student {
string name;
int num;
int age;
char sex;
};
int main()
{
Student stu[3] = { "lI",1001,18,'f',"Fang",1002,19,'m',"Wang",1004,17,'f' };
Student stu1[3];
ofstream outfile("stud.dat", ios::binary|ios::out);
if (!outfile)
{
cerr << "打开文件错误!" << endl;
exit(1);
}
cout << "将学生信息输入磁盘当中。。。" << endl;
outfile.write((char*)stu, sizeof(stu));
outfile.close();
ifstream infile("stud.dat", ios::binary|ios::in);
if (!infile)
{
cerr << "打开文件错误!" << endl;
exit(1);
}
cout << "将磁盘学生信息输入程序。。。" << endl;
infile.read((char *)stu1, sizeof(stu1));
infile.close();
for (int i = 0; i < 3; i++)
{
cout << "NO." << i + 1 << endl;
cout << "num: " << stu1[i].num << " name: " << stu1[i].name << " age: " << stu1[i].age << " sex: " << stu1[i].sex << endl;
}
return 0;
}


vs容易出现乱码 最好用其他ide
方法一:带参数n

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

class Student {

public:
int   num;
char name[20];
float score[3];
};
void addToFile(Student stu)
{
ofstream outfile("student.dat", ios::binary|ios::app);
if (!outfile)
{
cerr << "打开文件错误!" << endl;
exit(1);
}
outfile.write((char *)&stu, sizeof(Student));
outfile.close();
}
void readFromFile(int n)
{
Student stu[10];
ifstream infile("student.dat", ios::binary);
if (!infile)
{
cerr << "打开文件错误!" << endl;
exit(1);
}
infile.read((char*)&stu[0], sizeof(Student)*n);
for(int i=0;i<n;i++)
cout << "num: " << stu[i].num << " name: " << stu[i].name << " score:" << stu[i].score[0] << ' ' << stu[i].score[1] << ' ' << stu[i].score[2] << endl;
infile.close();
}
void copyFile(int n)
{
Student stu[10];
ifstream infile("student.dat", ios::binary);
if (!infile)
{
cerr << "打开错误!" << endl;
exit(1);
}
ofstream outfile("student.bak", ios::binary);
if (!outfile)
{
cerr << "打开错误!" << endl;
exit(1);
}
infile.read((char *)&stu[0], sizeof(Student)*n);
outfile.write((char *)&stu[0], sizeof(Student)*n);
infile.close();
outfile.close();
}
void put(int n)
{
Student stu;
ifstream infile("student.bak", ios::binary);
if (!infile)
{
cerr << "打开文件失败!" << endl;
exit(1);
}
for (int i = 0; i < n; i++)
{
infile.read((char *)&stu, sizeof(Student));
cout << "num: " << stu.num << " name: " << stu.name << " score: " << stu.score[0] << ' ' << stu.score[1] << ' ' << stu.score[2] << endl;

}
infile.close();
}
int main()
{
int n;
cin >> n;
Student *stu = new Student[n];
Student *p = stu;
for (int i = 0; i < n; i++, stu++)
{
cin >> stu->num >> stu->name >> stu->score[0] >> stu->score[1] >> stu->score[2];
}
stu = p;
cout << "将学生信息内容写入student.bat" << endl;
for (int j = 0; j < n; j++, p++)
{
addToFile(*p);
}
cout << "读取student.bat中的内容并输出" << endl;
readFromFile(n);
cout << "进行备份。。。" << endl;
copyFile(n);
cout << "输出备份中的信息" << endl;
put(n);

return 0;
}

方法二不带参数n:

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

//结构体
struct Student{
char name[20];
int id;
float score[3];
};

//读入磁盘文件Student.dat中
void writeToFile(Student s)
{

ofstream outfile("student.dat",ios::out|ios::app);
if(!outfile)
{
cerr<<"打开文件错误!"<<endl;
exit(1);
}
outfile.write((char *)&s,sizeof(Student));
outfile.close();
}

//从磁盘文件读出内容并输出
void readFile()
{
ifstream infile("Student.dat",ios::in);
if(!infile)
{
cerr<<"打开文件错误!"<<endl;
exit(1);
}
Student s;
//循环读入
while(infile.read((char *)&s,sizeof(Student))){
cout<<"id: "<<s.id<<endl;
cout<<"name:"<<s.name<<endl;
cout<<"score:"<<s.score[0]<<" "<<s.score[1]<<" "<<s.score[2]<<endl;
}
infile.close();

}
//备份student.dat到student.bak文件中
void copyFile()
{
ifstream infile("student.dat",ios::in);
if(!infile)
{
cerr<<"打开文件错误!"<<endl;
exit(1);
}
ofstream outfile("student.bak",ios::out);
if(!outfile)
{
cerr<<"打开文件错误!"<<endl;
exit(1);
}
Student s;
while(infile.read((char *)&s,sizeof(Student)))
{
outfile.write((char *)&s,sizeof(Student));
}
infile.close();
outfile.close();
}
void putFile()
{
ifstream infile("student.bak",ios::in);
if(!infile)
{
cerr<<"打开文件错误!"<<endl;
exit(1);
}
Student s;
while(infile.read((char *)&s,sizeof(Student)))
{
cout<<"id:"<<s.id<<endl;
cout<<"name:"<<s.name<<endl;
cout<<"score:"<<s.score[0]<<" "<<s.score[1]<<" "<<s.score[2]<<endl;
}
infile.close();
}

int main()
{
cout<<"输入学生人数:";
int n;
cin>>n;

Student *stu=new Student[n];
Student *p=stu;
cout<<"input the message of students:"<<endl;
for(int j=0;j<n;j++,p++){
cout<<"id:";
cin>>p->id;
cout<<"name:";
cin>>p->name;
cout<<"scores:";
cin>>p->score[0]>>p->score[1]>>p->score[2];
}
p=stu;
cout<<"读入磁盘文件Student.dat中"<<endl;
for(int i=0;i<n;i++,p++)
writeToFile(*p);
cout<<"从磁盘文件Student.dat中读出内容并输出"<<endl;
readFile();
cout<<"备份student.dat到student.bak文件中"<<endl;
copyFile();
cout<<"读取备份文件的内容"<<endl;
putFile();
return 0;
}
  • 点赞
  • 收藏
  • 分享
  • 文章举报
一只很菜但是好学的猪 发布了38 篇原创文章 · 获赞 1 · 访问量 1550 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: