服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - C++ txt 文件读取,并写入结构体中的操作

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

2021-10-11 11:04QT-Neal C/C++

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

如下所示:

wang 18 001

li 19 002

zhao 20 003

代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#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、结构体信息如下:

?
1
2
3
4
5
6
7
8
9
10
#define size 9
struct student//学生信息
{
 long int number;
 char name[size];
 int Chinese;
 int math;
 int English;
 int totalScore;
};

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

?
1
2
3
4
5
6
7
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文件行数:

?
1
2
3
4
5
6
7
8
9
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 文件读取,并写入结构体中

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
//利用 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语言版本代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//将*.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、测试代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#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, "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 文件读取,并写入结构体中的操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/yuyefuxiao521/article/details/77183345

延伸 · 阅读

精彩推荐