C++ txt 文件讀取,并寫入結構體中的操作
更新時間:2020年12月10日 09:27:14 作者:QT-Neal
這篇文章主要介紹了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文件行數(shù)未知
附加功能:采用 直接插入排序 方法 按總成績進行了降序排序
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、子函數(shù)代碼
獲取txt文件行數(shù):
char *fname="student_info.txt"; ifstream in(fname); if (!in){ cout << "No such a file" << endl; return NULL; } //獲取文件的行數(shù)--------------------------begin in.seekg(0, 2);//定位文件指針到文件末尾 student s; len = in.tellg() / sizeof(s);//獲得文件行數(shù) len += 2;//自己動手加上2行,目前不知道為什么,得到的行數(shù)總是比實際行數(shù)少兩行?? //獲取文件的行數(shù)--------------------------end
3.1、C++版本代碼如下:
思路:參考C++ txt 文件讀取,并寫入結構體中
//利用 C++,將文件中的student類型的數(shù)據結構信息 取出來,放在一個student類型的結構指針中,并將student* 返回 int len;//文件行數(shù) 全局變量 student* CreateStudentFromFile(char *fname) { ifstream in(fname); if (!in){ cout << "No such a file" << endl; return NULL; } //獲取文件的行數(shù)--------------------------begin in.seekg(0, 2);//定位文件指針到文件末尾 student s; len = in.tellg() / sizeof(s);//獲得文件行數(shù) len += 2;//自己動手加上2行,目前不知道為什么,得到的行數(shù)總是比實際行數(shù)少兩行?? //獲取文件的行數(shù)--------------------------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);//獲得文件行數(shù)//不知道為什么,這樣得到的文件行數(shù)總是少兩行?? 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 9 struct student { long int number; char name[size]; int Chinese; int math; int English; int totalScore; }; //利用 C++,將文件中的student類型的數(shù)據結構信息 取出來,放在一個student類型的結構指針中,并將student* 返回 int len;//文件行數(shù) 全局變量 student* CreateStudentFromFile(char *fname) { ifstream in(fname); if (!in){ cout << "No such a file" << endl; return NULL; } //獲取文件的行數(shù)--------------------------begin in.seekg(0, 2);//定位文件指針到文件末尾 student s; len = in.tellg() / sizeof(s);//獲得文件行數(shù) in.seekg(0, 0);//再重新定位文件指針到文件頭 len += 2; //獲取文件的行數(shù)--------------------------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);//獲得文件行數(shù)//不知道為什么,這樣得到的文件行數(shù)總是少兩行?? 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:保證三位數(shù)右對齊 } //直接插入排序 按總成績降序排列 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數(shù)學\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數(shù)學\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 文件讀取,并寫入結構體中的操作就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。