当前位置:首页 » 办公资讯 » 怎样将文件写入结构体

怎样将文件写入结构体

发布时间: 2022-11-19 23:17:03

㈠ C语言中怎么样把文件里面的数据读入到结构体中

文本文件可以一个一个的读在分别赋值给结构,如果是二进制数据可以读取一个结构整体,

struct
data
{
int
x;
doub
y;
}
d;
fin>>d.x>>d.y;

fread((char*)(&d),
sizeof(data),
1,
fin);

㈡ 怎么把一个结构体写入文件

C语言把一个结构体数组写入文件分三步:
1、以二进制写方式(wb)打开文件
2、调用写入函数fwrite()将结构体数据写入文件
3、关闭文件指针
相应的,读文件也要与之匹配:
1、以二进制读方式(rb)打开文件
2、调用读文件函数fread()读取文件中的数据到结构体变量
3、关闭文件指针
参考代码如下:

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

#include<stdio.h>
struct stu {
char name[30];
int age;
double score;
};
int read_file();
int write_file();
int main()
{
if ( write_file() < 0 ) //将结构体数据写入文件
return -1;
read_file(); //读文件,并显示数据
return 0;
}

int write_file()
{
FILE *fp=NULL;
struct stu student={"zhang san", 18, 99.5};
fp=fopen( "stu.dat", "wb" ); //b表示以二进制方式打开文件
if( fp == NULL ) //打开文件失败,返回错误信息
{
printf("open file for write error\n");
return -1;
}
fwrite( &student, sizeof(struct stu), 1, fp ); //向文件中写入数据
fclose(fp);//关闭文件
return 0;
}

int read_file()
{
FILE *fp=NULL;
struct stu student;
fp=fopen( "stu.dat", "rb" );//b表示以二进制方式打开文件
if( fp == NULL ) //打开文件失败,返回错误信息
{
printf("open file for read error\n");
return -1;
}
fread( &student, sizeof(struct stu), 1, fp ); //读文件中数据到结构体
printf("name=\"%s\" age=%d score=%.2lf\n", student.name, student.age, student.score ); //显示结构体中的数据
fclose(fp);//关闭文件
return 0;
}

fwrite(const void*buffer,size_t size,size_t count,FILE*stream);
(1)buffer:指向结构体的指针(数据首地址)
(2)size:一个数据项的大小(一般为结构体大小)
(3)count: 要写入的数据项的个数,即size的个数
(4)stream:文件指针。

㈢ 如何将文本文件存放到结构体中

typedef struct{int ID;int TYPE;char *name;}Student; Student myStu; fread(&myStu,sizeof(myStu),1,fp); fwrite(myStu,sizeof(myStu),1,fp2);简单的大体思想你试试吧

㈣ C语言怎样将.txt文件中的数据写入到结构体中去

txt文件中的数据写入到结构体中去的源代码如下:

#include<stdio.h>

#include <string.h>

//可以退出的头文件

#include <stdlib.h>

//结构体的长度

#define DATALEN 15

//函数声明

//定义结构数组

struct wordUnit{

int id; //id

char word[10]; //词语

char depId[10]; //依存词语的id

char pos[10]; //词性

char depRel[10]; //依存目标的关系

};

int main(){

FILE *data;//要读取的文件指针

int i=0;//结构题数组移动

struct wordUnit words[DATALEN];

if((data=fopen("data3.txt","r"))==NULL){

printf("Can not open file ");

return 0;

}

while(!feof(data)){

//原txt文档的数据之间是以空格隔开的

}

fclose(data);

for(int j=0;j<i;j++){

}

return 0;

}

(4)怎样将文件写入结构体扩展阅读

1、使用关键字struct,它表示接下来是一个结构体。

2、后面是一个可选的标志(book),它是用来引用该结构体的快速标记。

㈤ c语言怎么把txt格式的文件读到结构体里

1.你得先弄会流文件的读取http://blog.csdn.net/sky101010ws/article/details/6744062 这里是流文件的相关函数
2.读取流文件之后,获取的文件中的字符串信息也就是a a a a a 1 1 1 1 b b b b b 2 2 2 2 2 c c c c c 3 3 3 3。然后判断字符串中的空格和换行符,截取其中的a 1 b 2 c 3等字符(都是char型),接着判断1 2 3的ascii码范围,将其转换为整形(利用函数 int atoi(const char *nptr);)。当然,如果你TXT文件中的字符信息都是固定格式的,那就可以省略1 2 3 整形信息的判断,直接将其转换为整形。
3.获取文件中需要的信息a 1 b 2 c 3的同时,将其输入结构体中就可以了。

㈥ C语言 怎么把文件中的信息储存到结构体数组中

总体写得不错,问题出在你的

fscanf和fprintf函数参数传递错误了

#include"stdio.h"
#include"stdlib.h"
structs
{
intid;
charname[10];
intco1;
intco2;
intco3;
intco4;
};
intmain()
{
inti=0,count;
structsst[10];
charfname[10],ch;
FILE*infile,*outfile;
printf("pleaseinputdatafilename: ");
scanf("%s",fname);
infile=fopen(fname,"r");
outfile=fopen("output.txt","w");
if(infile==NULL)
{
printf(" Failedtoopenthefile");
exit(1);
}
fscanf(infile,"%d",&count);
while(i<count)
{
fscanf(infile,"%d%s%d%d%d%d ",&(st[i].id),st[i].name,&(st[i].co1),&(st[i].co2),&(st[i].co3),&(st[i].co4));
fprintf(outfile,"%d%s%d%d%d%d ",st[i].id,st[i].name,st[i].co1,st[i].co2,st[i].co3,st[i].co4);
i++;
}
fclose(infile);
fclose(outfile);
}

首先,你的name是结构体中的字符数组,fscanf要传入的应该是存储字符的地址,所以直接是数组名name就行


第二,fprintf你要写入文件的数据,应该是真正的数据本身,不是数据的地址,所以应该将变量前的取地址符全去掉就好,


第三,注意加好换行符


结果:

㈦ c++怎样把文件内容写入结构体数组求解

首先你将文件读取到缓冲区中,最好文件内容之间的信息用空格隔开了。然后你根据空格提取各种数据(也就是for循环一类的),然后依次赋值给结构体数组

㈧ 关于C语言的文件写入结构体数据

结构体写入文件是有前提的,你的结构体中不能出现指针,如果确定结构体中没有指针的话,就可以进行如下操作
typedef struct
{
//中间不能定义指针;
}a;
a var //结构体定义的变量

fwrite(&var, sizeof(a), sizeof(a), hfile);

㈨ C语言 如何把文件里面的内容存到结构体数组里面

需要使用文件操作函数fwrite和fread。

读物文件并写入结构体,同时保存文件的代码如下:

1、写

FILE*fp=fopen("card.dat","wb");
for(inti=0;i<counts;i++)
{
fwrite(&card[i],sizeof(structcard),1,fp);
}
fclose(fp);


2、读

FILE*fp=fopen("card.dat","rb");
intindex=0;
while(!feof(fp))
{
fread(&card[index++],sizeof(structcard),1,fp);
if(index>=1000)
break;
}
fclose(fp);
热点内容
马路上汽车的噪音在多少分贝 发布:2023-08-31 22:08:23 浏览:2127
应孕棒多少钱一盒 发布:2023-08-31 22:08:21 浏览:1595
标准养老金一年能领多少钱 发布:2023-08-31 22:05:05 浏览:1895
湖北通城接网线多少钱一个月 发布:2023-08-31 21:59:51 浏览:1966
开随车吊车多少钱一个月 发布:2023-08-31 21:55:06 浏览:1718
京东付尾款怎么知道前多少名 发布:2023-08-31 21:52:58 浏览:2046
在学校租铺面一个月要多少钱 发布:2023-08-31 21:52:09 浏览:2190
2寸有多少厘米 发布:2023-08-31 21:50:34 浏览:1825
知道电压如何算一小时多少电 发布:2023-08-31 21:46:20 浏览:1824
金手镯54号圈周长是多少厘米 发布:2023-08-31 21:44:28 浏览:1970