打开一个文件到一个结构 [英] Opening a file into a struct

查看:124
本文介绍了打开一个文件到一个结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎有与打开一个文件,其中包含的信息例如学生的记录,并使用我的结构进行打印的问题。

说我有学生姓名,他们的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 of char
  • Use fopen to open the file
  • Use fgets in a while 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 by sscanf. Alternatively you can use strtok and convert tokens as needed.

这篇关于打开一个文件到一个结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆