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文件和用于读取的文本文件。