• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

c++ 搞代码 4年前 (2022-01-06) 75次浏览 已收录 0个评论

这篇文章主要介绍了C++ txt 文件读取,并写入结构体中的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

如下所示:

wang 18 001

li 19 002

zhao 20 003

代码如下:

 #include  #include  #include  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 > 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 9 struct student//学生信息 { long int number; char name[size]; int Chinese; int math; int English; int totalScore; };

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

 179328 何芳芳 89 100 98 179325 陈红 86 100 88 179326 陆华 75 80 90 179324 张小仪 85 57 94 179327 张平 80 98 78 179320 木子 100 96 89 179329 海子 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" <> 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.Chi<b style="color:transparent">来源gao@!dai!ma.com搞$$代^@码!网</b>nese, &s.math, &s.English); s.totalScore = s.Chinese + s.math + s.English; // *stu++ = s;//错误 stu[i] = s; } fclose(f); return stu; } 

4、测试代码

 #include #include #include #include using namespace std; #define size 9 struct 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" <> 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>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 文件读取,并写入结构体中的操作的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:C++ txt 文件读取,并写入结构体中的操作

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址