1. 程式人生 > 程式設計 >C++ txt 檔案讀取,並寫入結構體中的操作

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 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" << 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,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 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" << 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,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,&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);
}
int main()
{
 test0();
 test();
 return 0;
}

以上這篇C++ txt 檔案讀取,並寫入結構體中的操作就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。