C++ txt 文件读取,并写入结构体中的操作

如下所示:

wang 18 001

li 19 002

zhao 20 003

代码如下:

#include <string>#include <iostream>#include <fstream> using namespace std; struct people{ string name; int age; string id;}p[20]; int main(){ int n = 0; ifstream in( "a.txt" , ios::in); if (!in.is_open()) {  cout << "Error: opening file fail" << endl;  exit (1); } while (!in.eof() && n < 20) {  in >> p[n].name >> p[n].age >> p[n].id;  n++; }  //test for ( int i = 0; i < n; ++i)  cout << "name:" << p[i].name << " age:" << p[i].age << " id:" << p[i].id << endl;   in.close(); return 0;}

补充知识:

C语言 C++两个版本 txt 文件读取结构体信息,写入结构体指针中,并以结构体指针形式返回 txt文件行数未知

附加功能:采用 直接插入排序 方法 按总成绩进行了降序排序

1、结构体信息如下:

#define size 9struct student//学生信息{ long int number; char name[size]; int Chinese; int math; int English; int totalScore;};

2、txt文件(student_info.txt)中存储信息如下:

179328 何芳芳 89 100 98179325 陈红 86 100 88179326 陆华 75 80 90179324 张小仪 85 57 94179327 张平 80 98 78179320 木子 100 96 89179329 海子 93 95 88

3、子函数代码

获取txt文件行数:

 char *fname="student_info.txt"; ifstream in(fname); if (!in){ cout << "No such a file" << endl; return NULL; } //获取文件的行数--------------------------begin in.seekg(0, 2);//定位文件指针到文件末尾 student s; len = in.tellg() / sizeof(s);//获得文件行数 len += 2;//自己动手加上2行,目前不知道为什么,得到的行数总是比实际行数少两行?? //获取文件的行数--------------------------end

3.1、C++版本代码如下:

思路:参考C++ txt 文件读取,并写入结构体中

//利用 C++,将文件中的student类型的数据结构信息 取出来,放在一个student类型的结构指针中,并将student* 返回int len;//文件行数 全局变量student* CreateStudentFromFile(char *fname){ ifstream in(fname); if (!in){ cout << "No such a file" << endl; return NULL; } //获取文件的行数--------------------------begin in.seekg(0, 2);//定位文件指针到文件末尾 student s; len = in.tellg() / sizeof(s);//获得文件行数 len += 2;//自己动手加上2行,目前不知道为什么,得到的行数总是比实际行数少两行?? //获取文件的行数--------------------------end in.seekg(0, 0);//再重新定位文件指针到文件头 //---------将文件中的结构体写入到 结构体指针中---- student *stu = new student[len]; int i = 0; while (in >> s.number >> s.name >> s.Chinese >> s.math >> s.English)//之前一直错误的原因是写成了cin>>就是从键盘输入了!! { s.totalScore = s.Chinese + s.math + s.English; stu[i] = s; ++i; // *stu++ = s;//错误,这样代替前两行 一定错误!! 暂时还不知道为什么?? } in.close(); //----------------------------------------------- return stu;}

3.1、C语言版本代码如下:

//将*.txt文件中的学生信息 存放到 学生结构体指针中,并返回该结构体指针student* CreateStudentFromFile2(char *fname)//C语言的文件就可以 Okay!!{ FILE *f; f = fopen(fname, "r"); if (!f){ cout << "No such a file" << endl; return NULL; } student s; fseek(f, 0, 2);//定位文件指针到文件末尾 len = ftell(f) / sizeof(s);//获得文件行数//不知道为什么,这样得到的文件行数总是少两行?? rewind(f);// 指针重新回到文件开始 len += 2; student *stu = (student *)malloc(len*sizeof(student)); int i = 0; for (int i = 0; i < len; ++i) { fscanf(f, "%ld%s%d%d%d", &s.number, &s.name, &s.Chinese, &s.math, &s.English); s.totalScore = s.Chinese + s.math + s.English; // *stu++ = s;//错误 stu[i] = s; } fclose(f); return stu;}

4、测试代码

#include<iostream>#include<fstream>#include<sstream>#include<string>using namespace std;#define size 9struct student{ long int number; char name[size]; int Chinese; int math; int English; int totalScore;};//利用 C++,将文件中的student类型的数据结构信息 取出来,放在一个student类型的结构指针中,并将student* 返回int len;//文件行数 全局变量student* CreateStudentFromFile(char *fname){ ifstream in(fname); if (!in){ cout << "No such a file" << endl; return NULL; } //获取文件的行数--------------------------begin in.seekg(0, 2);//定位文件指针到文件末尾 student s; len = in.tellg() / sizeof(s);//获得文件行数 in.seekg(0, 0);//再重新定位文件指针到文件头 len += 2; //获取文件的行数--------------------------end //C++ txt 文件读取,并写入结构体中 //---------将文件中的结构体写入到 结构体指针中---- student *stu = new student[len]; int i = 0; while (in >> s.number >> s.name >> s.Chinese >> s.math >> s.English)//之前一直错误的原因是写成了cin>>就是从键盘输入了!! { s.totalScore = s.Chinese + s.math + s.English; stu[i] = s; ++i; // *stu++ = s;//错误,这样代替前两行 一定错误!! 暂时还不知道为什么?? } in.close(); //----------------------------------------------- return stu;}//将*.txt文件中的学生信息 存放到 学生结构体指针中,并返回该结构体指针student* CreateStudentFromFile2(char *fname)//C语言的文件就可以 Okay!!{ FILE *f; f = fopen(fname, "r"); if (!f){ cout << "No such a file" << endl; return NULL; } student s; fseek(f, 0, 2);//定位文件指针到文件末尾 len = ftell(f) / sizeof(s);//获得文件行数//不知道为什么,这样得到的文件行数总是少两行?? rewind(f);// 指针重新回到文件开始 len += 2;//自己动手加上2行 student *stu = (student *)malloc(len*sizeof(student)); int i = 0; for (int i = 0; i < len; ++i) { fscanf(f, "%ld%s%d%d%d", &s.number, &s.name, &s.Chinese, &s.math, &s.English); s.totalScore = s.Chinese + s.math + s.English; // *stu++ = s;//错误 stu[i] = s; } fclose(f); return stu;}void DestroyStudentStruct(student *&s){ if (s==NULL){ cout << "无信息" << endl; return; } delete[] s; s = NULL;}void disp(const student* s, int len){ if (s == NULL){ cout << "该学生尚未登记,暂无信息。" << endl; return; } for (int i = 0; i < len; ++i) printf_s("%ld\t%s\t%3d\t%3d\t%3d\t%3d\n", s[i].number, s[i].name, s[i].Chinese, s[i].math, s[i].English, s[i].totalScore);//%3d:保证三位数右对齐}//直接插入排序 按总成绩降序排列void InsertionSort(student* s, int len){ for (int i = 1; i < len; ++i) { for (int j = 0; j < i; ++j) {  if (s[j].totalScore < s[i].totalScore)  {  student temp = s[i];//这样的话,根据学号,调整学号所在对象的位置,整个Student对象 都会随着学号的升序而跟着改变  for (int k = i; k>j; --k)   s[k] = s[k - 1];  s[j] = temp;  } } }}void test0(){ cout << "------C++版本---test0()---将txt中的结构体信息写入到 结构体指针中--------" << endl; student *s = CreateStudentFromFile("student_info.txt"); cout << "学号\t姓名\t语文\t数学\t外语\t总成绩" << endl; cout << "before insertion sort: " << endl; disp(s, len); InsertionSort(s, len);//插入法排序成功 //根据成绩排序 cout << "after insertion sort: " << endl; disp(s, len); DestroyStudentStruct(s); cout << s << endl; disp(s, len);}void test(){ cout << "------C语言版本---test()---将txt中的结构体信息写入到 结构体指针中--------" << endl; student *s = CreateStudentFromFile2("student_info.txt"); cout << "学号\t姓名\t语文\t数学\t外语\t总成绩" << endl; cout << "before insertion sort: " << endl; disp(s, len); InsertionSort(s, len);//插入法排序成功 //根据成绩排序 cout << "after insertion sort: " << endl; disp(s, len); DestroyStudentStruct(s); cout << s << endl; disp(s, len);}int main(){ test0(); test(); return 0;}

以上这篇C++ txt 文件读取,并写入结构体中的操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

哪里会顾得上这些。等到时间将矛盾一层层降解为流言是非误解过结

C++ txt 文件读取,并写入结构体中的操作

相关文章:

你感兴趣的文章:

标签云: