1. c語言如何使用結構體讀取TXT文件並且輸入序號輸出對應內容
#include<stdio.h>
#include<stdlib.h>
#defineN120
structshangpin{
intxuhao;
charriqi[42];
charmingcheng[51];
floatchengben;
intshoujia;
floatlirun;
};
voidreaddata(structshangpina[],int*n,FILE*fp){
inti=0;
if(fp==NULL){
printf("沒有可供讀取的數據文件。 ");
exit(2);
}
*n=0;
while(!feof(fp)&&i<N){
fscanf(fp,"%d%s%s",&a[i].xuhao,a[i].riqi,a[i].mingcheng);
fscanf(fp,"%f%d%f",&a[i].chengben,&a[i].shoujia,&a[i].lirun);
++i;
++(*n);
}
}
voidshow(structshangpina[],intn){
inti;
for(i=0;i<n;++i){
printf("%d %s %s ",a[i].xuhao,a[i].riqi,a[i].mingcheng);
printf("%.2f %d %.2f ",a[i].chengben,a[i].shoujia,a[i].lirun);
}
}
intmain(){
intn=0;
FILE*fp;
structshangpina[N];
chardatafile[]="indata.txt";//假設的數據文件名
if((fp=fopen(datafile,"rt"))==NULL){
printf("無法打開數據文件:%s ",datafile);
exit(3);
}
readdata(a,&n,fp);
show(a,n);
return0;
}
2. 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的同時,將其輸入結構體中就可以了。
3. C語言 關於結構體向txt文件輸出的操作
FILE *p=fopen("student.txt","w");
fprintf(p,"%s %3d %d",name,num1,num2);
4. C語言如何才能讀取txt文件然後寫入結構體 要做一個成績系統,導出成績
//將以下兩個函數的定義插入到你的成績系統中,並在適當的位置調用它們就行了。
//將s中的成績保存到你輸入的文件中
void write()
{
char f[128];
FILE *outf=NULL;
printf("請輸入要保存學生成績的文件的文件名:\n");
gets(f);
//以二進制形式保存學生成績
outf=fopen(f,"wb");
if(outf)
{
fwrite(s,sizeof(struct student),N,outf);
fclose(outf);
}
}
//從你輸入的文件中讀入成績到s
void read()
{
char f[128];
FILE *inf=NULL;
printf("請輸入要從中讀取學生成績的文件的文件名:\n");
gets(f);
//以二進制形式讀取學生成績
inf=fopen(f,"rb");
if(inf)
{
fread(s,sizeof(struct student),N,inf);
fclose(inf);
}
}
5. c語言 將txt文件導入結構體數組
txt格式
20170043556875 張某 機械工程1414
12334545654677 李某 有機化學1313
code:
#include<stdio.h>
#defineRECORDNUMBER100
typedefstructSTUDENT{
charname[50];
unsignedlongid;
charclass[50];
}stu;
intmain(){
studata[RECORDNUMBER];
FILE*fp=fopen("a.txt","r");
if(!fp)return-1;
inti=0;
while(fscanf(fp,"%ld%s%s",&data[i].id,data[i].name,data[i].class)!=EOF){
printf("%ld,%s,%s ",data[i].id,data[i].name,data[i].class);
i++;
}
fclose(fp);
}
6. 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;
}
(6)c語言結構體調用txt表格擴展閱讀
1、使用關鍵字struct,它表示接下來是一個結構體。
2、後面是一個可選的標志(book),它是用來引用該結構體的快速標記。
7. 如何用C語言將結構體寫入讀出TXT文件,int型可以正常列印,可是char類型
#include<stdio.h>
#define N 5
struct SS { int number; char name[20]; int age; };
void main()
{ struct SS stu[N]={ {1,"趙明",17},{2,"李廣",16},{3,"錢兵",17},{5,"吳俊傑",18},{4,"孫菲",15} };
struct SS stu1[N];
int i; FILE *fp; char fnm[]={ "student.txt" };
if ( fp=fopen(fnm,"w+") )
{ for ( i=0;i<N;i++ ) fprintf(fp,"%d %s %d ",stu[i].number,stu[i].name,stu[i].age);
fclose(fp);
if ( fp=fopen(fnm,"r") )
{ for ( i=0;i<N;i++ ) fscanf(fp,"%d %s %d",&stu1[i].number,&stu1[i].name,&stu1[i].age);
fclose(fp);
for ( i=0;i<N;i++ ) printf("%d %s %d ",stu1[i].number,stu1[i].name,stu1[i].age);
} else printf("無法打開文件讀取。 ");
} else printf("無法建立文件。 ");
MEND: printf(" "); system("pause");
}
8. C語言結構體讀取txt文件中內容,有逗號
用字元讀出,判斷是否為『,』,是的話就轉化為結構體中的一個變數值,再讀取判斷,直到都讀出來。
寫入文件的時候每個數據的位元組數都是定好的,直接讀取一行,然後用memcpy(char* des,char* str,int n)讀取,memcpy(des,str+n,m);從第n個位元組讀m個位元組。
兩種都可以,第二種讀字元串的時候有點問題,需要再做處理,因為寫入文件時字元串是靠後寫的,如%10s,你寫入abc,存入文件的是「 abc」,而我們需要的是"abc",前面多了空格,所以你要處理下,要不然比較時字元串是不等的。
9. C語言 用結構體指針 fscanf讀取txt文件, 再寫入另一個txt文件
#include<stdio.h>
#include<stdlib.h>
typedefstructperson
{
char*last;
char*first;
char*year;
}person_t;
intmain()
{
inti;
FILE*fp1,*fp2;
person_t*list=(person_t*)malloc(100*sizeof(person_t));
fp1=fopen("d.txt","r");
fp2=fopen("detail.txt","w");
if((fp1=fopen("d.txt","r"))==NULL)
{
printf("Cannotopend.txt! ");
exit(0);
}
for(i=0;i<5;i++)
{
(list+i)->first=(char*)malloc(sizeof(char)*20);//為你的指針分配空間
(list+i)->last=(char*)malloc(sizeof(char)*20);
(list+i)->year=(char*)malloc(sizeof(char)*20);
fscanf(fp1,"%s%s%s ",(list+i)->first,(list+i)->last,(list+i)->year);
}
for(i=0;i<5;i++)
{
fprintf(fp2,"%s%s%s ",(list+i)->first,(list+i)->last,(list+i)->year);
}
for(i=0;i<5;i++)
{
free((list+i)->first);//釋放空間
free((list+i)->last);
free((list+i)->year);
}
fclose(fp1);
fclose(fp2);
return0;
}
如果你不想指定分配的內存長度,你可以按照下面的方法:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedefstructperson
{
char*last;
char*first;
char*year;
}person_t;
intmain()
{
inti;
FILE*fp1,*fp2;
charstr[20];
person_t*list=(person_t*)malloc(100*sizeof(person_t));
fp1=fopen("d.txt","r");
fp2=fopen("detail.txt","w");
if((fp1=fopen("d.txt","r"))==NULL)
{
printf("Cannotopend.txt! ");
exit(0);
}
for(i=0;i<5;i++)
{
fscanf(fp1,"%s",str);
(list+i)->first=(char*)malloc(sizeof(char)*(strlen(str)+1));//每次讓str存放讀取的數據,然後根據str長度malloc內存長度
strcpy((list+i)->first,str);
fscanf(fp1,"%s",str);
(list+i)->last=(char*)malloc(sizeof(char)*(strlen(str)+1));
strcpy((list+i)->last,str);
fscanf(fp1,"%s",str);
(list+i)->year=(char*)malloc(sizeof(char)*(strlen(str)+1));
strcpy((list+i)->year,str);
}
for(i=0;i<5;i++)
{
fprintf(fp2,"%s%s%s ",(list+i)->first,(list+i)->last,(list+i)->year);
}
for(i=0;i<5;i++)
{
free((list+i)->first);//釋放空間
free((list+i)->last);
free((list+i)->year);
(list+i)->first=NULL;//防止野指針出現
(list+i)->last=NULL;
(list+i)->year=NULL;
}
fclose(fp1);
fclose(fp2);
return0;
}
10. 如何用C語言讀取txt文件中的數據到結構體數組中
1、在vscode裡面添加了Python文件和用於讀取的文本文件。