打开一个文件到一个结构 [英] Opening a file into a struct
问题描述
我似乎有与打开一个文件,其中包含的信息例如学生的记录,并使用我的结构进行打印的问题。
说我有学生姓名,他们的ID和档次的txt文件,如:
casnova 195843一
等等...
我定义我的结构为:
结构学生{
焦炭名[20];
INT ID;
字符级;
};
我想用结构加载了这个文件,我有点失去了以什么我应该做的。
我的问题是不问如何做的问题,而是一个基本的出发点,因为我仍然相当新的C语言编程,任何提示/链接是有帮助的:)
我的问题是不问如何做的问题,而是一个基本
起点
块引用>
- 定义
结构的学生组成的数组
阵列字符
- 使用
的fopen
来打开文件- 使用
与fgets
在,而
循环来一次读取一行(这是字符数组是什么)
的- 使用
的sscanf
来提取成分委员说行。测试由的sscanf
返回的值。或者您可以使用strtok的
,并根据需要转换令牌。I seem to be having an issue with opening a file containing information e.g student records and printing it using my struct.
Say I have student names, their IDs and grades in the txt file such as:
casnova 195843 A and so on...
I defined my struct as:
struct student { char name[20]; int ID; char Grade; };
I want to load up this file using structs, and I am kind of lost as to what I should be doing.
My question is not asking how to do the question but basically a starting point as I am still fairly new to C programming, any tips/links are helpful :)
解决方案My question is not asking how to do the question but basically a starting point
- Define an array of
struct student
an array ofchar
- Use
fopen
to open the file- Use
fgets
in awhile
loop to read one line at a time (that's what the char array is for)- Use
sscanf
to extract constituent members of said line. Test the value returned bysscanf
. Alternatively you can usestrtok
and convert tokens as needed.这篇关于打开一个文件到一个结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!