当前位置:首页 » 编程语言 » c语言交叉输入数据与文件
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言交叉输入数据与文件

发布时间: 2022-08-12 19:13:06

A. 一个关于c语言文件数据输入与输出的问题

fprintf(fp,"%d%s%d\n",stud[i].xh,stud[i].xm,stud[i].cj);
这句在%d和%s之间加上空格就行了
fprintf(fp,"%d %s %d\n",stud[i].xh,stud[i].xm,stud[i].cj);
不加空格的话,成绩数据会被%s连带读到xm里面去

B. C语言 用文件输入数据然后对数据进行处理 再输出另一个文件。

*********************************************
123.txt文本内容:
AAA 89 96 35
BBB 74 62 85
CCC 85 93 74
**********************************************
#include <stdio.h>
int main()
{
FILE *fp1,*fp2;

char name[8]="";
int ch=0,eng=0,math=0;
if((fp1=fopen("123.txt","rt"))==NULL)
{
printf("\nerror on open fp1!");
exit(1);
}

if((fp2=fopen("124.txt","wt+"))==NULL)
{
printf("\nerror on open fp2!");
exit(1);
}

while(!feof(fp1))
{
fscanf(fp1,"%s %d %d %d\n",name,&ch,&eng,&math);

ch=ch+eng+math;
fprintf(fp2,"%s %d\n",name,ch);

}
fclose (fp1);
fclose (fp2);

getchar();
return 0;
}

C. C语言怎么将输入的数据写入文件中和从文件中读书文件

用C语言怎么将输入的数据写入文件中和从文件中读书文件
...int
main()
{
//首先,得打开这个文件吧
FILE
*fpr;
fpr
=
fopen("test.txt",
"rw");...

D. C语言如何进行文件输入与输出(急求样例程序)

#include<stdio.h>
main()
{
int i,flag;
char str[80];
FILE *fp;
fp=open("text","w");
flag=1;
while(flag==1)
{printf("\nInput string:\n");
gets(str);
fprintf(fp,"%s",str);
printf("\nCountiune?");
c=getchar();
if((c=='N')||(c=='n'))
flag=0;
getchar();
}
fclose(fp);
fp=fopen("text","r");
while(fscanf(fp,"%s",str)!=EOF)
{for(i=0;str[i]!='\0';i++)
if((str[i]>='a')&&(str[i]<='z'))
str[i]-=32;
printf("\n%s\n",str);
}
fclose(fp);
}

希望对你有帮助

E. c语言中如何在结构体中输入数据,并将结构体储存到文件之中。比方说输入影片的信息 struct N

#include "stdio.h"
#include "stdlib.h"
struct s
{
int id;
char name[10];
int co1;
int co2;
int co3;
int co4;
};
int main()
{
int i=0,count;
struct s st[10];
char fname[10],ch;
FILE *infile,*outfile;
printf("please input data file name:\n");
scanf("%s",fname);
infile=fopen(fname,"r");
outfile=fopen("output.txt","w");
if(infile==NULL)
{
printf("\nFailed to open the file");
exit(1);
}
fscanf(infile,"%d",&count);
while(i<count)
{
fscanf(infile,"%d %s %d %d %d %d\n",&(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\n",st[i].id,st[i].name,st[i].co1,st[i].co2,st[i].co3,st[i].co4);
i++;
}
fclose(infile);
fclose(outfile);
}

F. c语言当交叉输入数值数据和字符数据时应当注意些什么

/*书上说的不一定对啊。我的编译器就可以用多种方式输入:
10A 20B
10A 20B
10A 20 B
10 A 20 B
以上的输入格式,我的编译器都能接受。
你可以用以下例子试验下,看你的编译器能接受什么格式。*/
#include<stdio.h>
#include<stdlib.h>

int main(int argc,char *argv[])
{
int a,b;
char c1,c2;
scanf("%d %c %d %c",&a,&c1,&b,&c2);
printf("%d %c %d %c\n",a,c1,b,c2);
system("PAUSE");
return 0;
}

G. C语言 关于从文件输入数据

1)
所谓输入输出是以计算机为主体而言的。
2)
在c语言中,所有的数据输入/输出都是由库函数完成的。因此都是函数语句。
3)
在使用c语言库函数时,要用预编译命令

#include
将有关“头文件”包括到源文件中。
使用标准输入输出库函数时要用到
“stdio.h”文件,因此源文件开头应有以下预编译命令:
复制纯文本新窗口1.
#include<stdio.h>#include<stdio.h>或
复制纯文本新窗口1.
#include
"stdio.h"#include "stdio.h"stdio是standard
input&outupt的意思。
4)
考虑到printf和scanf函数使用频繁,系统允许在使用这两个函数时可不加
复制纯文本新窗口1.
#include<stdio.h>#include<stdio.h>或
复制纯文本新窗口1.
#include
"stdio.h"

H. c语言舞伴配对问题,要求将输入数据及结果数据保存到文件中,代码没问题,就是不知道为什么无法输出数据

//你的代码很多错误,我改了之后,可以正常运行
#include<stdio.h>
#include<malloc.h>
#defineNULL0

typedefstructQnode//定义链表的节点
{
intdata;
structQnode*next;
}Qnode,*Queueptr;
typedefstruct//定义链表整体
{
Queueptrfront;
Queueptrrear;
}Linkqueue;
voidInitqueue(Linkqueue&Q)//初始化队列
{
Q.front=Q.rear=(Queueptr)malloc(sizeof(Qnode));//申请动态内存空间
if(Q.front)
Q.front->next=NULL;
}

voidEnqueue(Linkqueue&Q,intm)//入队
{
Queueptrp;
p=(Queueptr)malloc(sizeof(Qnode));
if(p)
{
p->data=m;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
}
else
{
printf("error");
}
}
intDequeue(Linkqueue&Q)//出列
{
intm;
Queueptrp;
if(Q.rear!=Q.front)
{
p=Q.front->next;
m=p->data;
Q.front->next=p->next;
}
if(Q.front==p)
Q.rear=Q.front;
free(p);
returnm;
}
intDeleteQueue(Linkqueue*Q)
{Queueptrp;
for(;Q->front!=NULL;free(p))
{p=Q->front;
Q->front=Q->front->next;
}
return0;
}

voidDance(LinkqueueM,LinkqueueW)
{
FILE*fp;
fp=fopen("ytj.txt","w");
inti,m,n,k,a,b,c,d,num=0,r=0;
printf("请输入男生的数量:");
fprintf(fp,"请输入男生的数量:");
scanf("%d",&n);
fprintf(fp,"%d",n);
printf("请输入女生的数量:");
fprintf(fp,"请输入女生的数量:");
scanf("%d",&m);
fprintf(fp,"%d",m);
printf("请输入曲子的数量:");
fprintf(fp,"请输入曲子的数量:");
scanf("%d",&k);
fprintf(fp,"%d",k);
printf("请输入您要查找的男生编号:");
fprintf(fp,"请输入您要查找的男生编号:");
scanf("%d",&a);
fprintf(fp,"%d",a);
printf("请输入您要查找的女生编号:");
fprintf(fp,"请输入您要查找的女生编号:");
scanf("%d",&b);
fprintf(fp,"%d",b);
for(i=1;i<=m;i++)
Enqueue(W,i);
for(i=1;i<=n;i++)
Enqueue(M,i);
for(i=1;i<=k;i++)
{
printf(" 现在正在播放第%d首曲子! ",i);
fprintf(fp," 现在正在播放第%d首曲子! ",i);
c=Dequeue(M);
d=Dequeue(W);
printf("现在正在跳舞的是第%d号男生和第%d号女生! ",c,d);
fprintf(fp,"现在正在跳舞的是第%d号男生和第%d号女生! ",c,d);
if(c==a&&b==d)
{
r=1;
num++;
printf("第%d号男生和%d号女生在第%d号曲子一起跳舞! ",a,b,i);
fprintf(fp,"第%d号男生和%d号女生在第%d号曲子一起跳舞! ",a,b,i);
}
Enqueue(M,c);
Enqueue(W,d);
}
if(r==0)
printf(" 您要查找的%d号男生和%d号女生没有在一起搭配跳舞! ",a,b);
fprintf(fp," 您要查找的%d号男生和%d号女生没有在一起搭配跳舞! ",a,b);
if(r==1)
printf(" 您要查找的%d号男生和%d号女生一起跳舞的次数为:%d次! ",a,b,num);
fprintf(fp," 您要查找的%d号男生和%d号女生一起跳舞的次数为:%d次! ",a,b,num);
if(fp)
{
fclose(fp);
}
fp=NULL;
}
intmain()
{
LinkqueueM,W;
printf("学生搭配问题! ");
Initqueue(M);
Initqueue(W);
Dance(M,W);
return0;
}