怎樣將文件寫入結構體
㈠ 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);