【C++中输入输出流ifstream/ofstream用法总结】

 字符串数据的输入

数据多的时候读写速度比较快,输入时以整行字符串加上换行符号一次写入。读取的时候以语句getline(buffer,max),来读取整行数据,直到遇到换行符,每行结尾的\n并不读入,所以在 输出的时候需要加上换行符号,否则数据会连接在一起。

例子:

  1. ofstream outfile("strdata.txt");
  2. outfile<< "use your vote\n"
  3. outfile<< "ouse your weise\n";


读取:

  1. const MAX= 80;
  2. char buffer[MAX];
  3. ifstream infile("strdata.txt");
  4. while(infile)
  5. {
  6. infile.getline(buffer,MAX);
  7. cout<<buffer<< endl;
  8. }

文件流是以外存文件为输入输出对象的数据流。输出文件流是从内存流向外存文件的数据,输入文件流是从外存文件流向内存的数据。每一个文件流都有一个内存缓冲区与之对应。

请区分文件流与文件的概念。文件流本身不是文件,而只是以文件为输入输出对象的流。若要对磁盘文件输入输出,就必须通过文件流来实现。

在C++的I/O类库中定义了几种文件类,专门用于对磁盘文件的输入输出操作除了标准输入输出流类istream,ostream和iostream类外,还有3个用于文件操作的文件类:

(1) ifstream类,它是从istream类派生的。 用来支持从磁盘文件的输入。(外存到程序的输入)

(2) ofstream类,它是从ostream类派生的。 用来支持向磁盘文件的输出。(程序向外存的输出)

4. 从键盘输入 4 个学生的数据( 包括姓名、年龄和成绩),并存放在文件 sf1 上。从该文件读出这些数据,按成绩从高到底排序,并输出其中成绩次高者的所有数据。

  1. #include<iostream>
  2. #include<string>
  3. #include<fstream>
  4. #include<algorithm>
  5. using namespace std;
  6. struct Stu{
  7. string name;
  8. int age;
  9. double score;
  10. };
  11. bool cmp(Stu A,Stu B){
  12. return A.score>B.score;
  13. }
  14. int main(){
  15. int i,k;
  16. string str;
  17. ofstream outfile("E:\\sf1.txt");// 建立一个输出文件流对象;定义ofstream类(输出文件流类)对象outfile
  18. for(i= 0;i< 4;i++){
  19. cin>>str;
  20. outfile<<str<< " ";
  21. cin>>k;
  22. outfile<<k<< " ";
  23. cin>>k;
  24. outfile<<k<< endl;
  25. }
  26. Stu s[ 4];
  27. i= 0;
  28. ifstream infile("E:\\sf1.txt");
  29. while(infile>>s[i].name){
  30. infile>>s[i].age;
  31. infile>>s[i++].score;
  32. }
  33. sort(s,s+i,cmp);
  34. for( int j= 0;j<i;j++)
  35. cout<<s[j].name<< " "<<s[j].age<< " "<<s[j].score<< endl;
  36. cout<< "成绩次高的学生信息为:";
  37. cout<<s[ 1].name<< " "<<s[ 1].age<< " "<<s[ 1].score<< endl;
  38. return 0;
  39. }

 字符串数据的输入

数据多的时候读写速度比较快,输入时以整行字符串加上换行符号一次写入。读取的时候以语句getline(buffer,max),来读取整行数据,直到遇到换行符,每行结尾的\n并不读入,所以在 输出的时候需要加上换行符号,否则数据会连接在一起。

例子:

  1. ofstream outfile("strdata.txt");
  2. outfile<< "use your vote\n"
  3. outfile<< "ouse your weise\n";


读取:

  1. const MAX= 80;
  2. char buffer[MAX];
  3. ifstream infile("strdata.txt");
  4. while(infile)
  5. {
  6. infile.getline(buffer,MAX);
  7. cout<<buffer<< endl;
  8. }

文件流是以外存文件为输入输出对象的数据流。输出文件流是从内存流向外存文件的数据,输入文件流是从外存文件流向内存的数据。每一个文件流都有一个内存缓冲区与之对应。

请区分文件流与文件的概念。文件流本身不是文件,而只是以文件为输入输出对象的流。若要对磁盘文件输入输出,就必须通过文件流来实现。

在C++的I/O类库中定义了几种文件类,专门用于对磁盘文件的输入输出操作除了标准输入输出流类istream,ostream和iostream类外,还有3个用于文件操作的文件类:

(1) ifstream类,它是从istream类派生的。 用来支持从磁盘文件的输入。(外存到程序的输入)

(2) ofstream类,它是从ostream类派生的。 用来支持向磁盘文件的输出。(程序向外存的输出)

4. 从键盘输入 4 个学生的数据( 包括姓名、年龄和成绩),并存放在文件 sf1 上。从该文件读出这些数据,按成绩从高到底排序,并输出其中成绩次高者的所有数据。

  1. #include<iostream>
  2. #include<string>
  3. #include<fstream>
  4. #include<algorithm>
  5. using namespace std;
  6. struct Stu{
  7. string name;
  8. int age;
  9. double score;
  10. };
  11. bool cmp(Stu A,Stu B){
  12. return A.score>B.score;
  13. }
  14. int main(){
  15. int i,k;
  16. string str;
  17. ofstream outfile("E:\\sf1.txt");// 建立一个输出文件流对象;定义ofstream类(输出文件流类)对象outfile
  18. for(i= 0;i< 4;i++){
  19. cin>>str;
  20. outfile<<str<< " ";
  21. cin>>k;
  22. outfile<<k<< " ";
  23. cin>>k;
  24. outfile<<k<< endl;
  25. }
  26. Stu s[ 4];
  27. i= 0;
  28. ifstream infile("E:\\sf1.txt");
  29. while(infile>>s[i].name){
  30. infile>>s[i].age;
  31. infile>>s[i++].score;
  32. }
  33. sort(s,s+i,cmp);
  34. for( int j= 0;j<i;j++)
  35. cout<<s[j].name<< " "<<s[j].age<< " "<<s[j].score<< endl;
  36. cout<< "成绩次高的学生信息为:";
  37. cout<<s[ 1].name<< " "<<s[ 1].age<< " "<<s[ 1].score<< endl;
  38. return 0;
  39. }

猜你喜欢

转载自blog.csdn.net/momo_mo520/article/details/80833917