希望有大佬指導一下,輸入的資料寫不進檔案中。


uj5u.com熱心網友回復:
直接貼代碼不好嗎,報錯資訊是啥,波浪線的提示是什么,是不是沒有包含相應的頭檔案?uj5u.com熱心網友回復:
供參考:#include <stdio.h>
#include <stdlib.h>
typedef struct staffmessages{
long worknum;
char name[10];
int award;//基本工資
int award1;//職務工資
int award2;//崗位津貼
int award3;//特殊津貼
int award4;//工資
}STA;
int printdata(STA sta1[])
{
int i=1,a=0;
for(;;){
printf("請輸入工號:");
fflush(stdout);
rewind(stdin);
scanf("%ld",&sta1[a].worknum);//scanf_s("%ld",&sta1[a].worknum);
printf("請輸入姓名:");
fflush(stdout);
rewind(stdin);
scanf("%s",sta1[a].name);//scanf_s("",sta1[a].name,sizoef(sta1.[a].name));
printf("請輸入基本工資:");
fflush(stdout);
rewind(stdin);
scanf("%d",&sta1[a].award);//scanf_s("%d",&sta1[a].award);
printf("請輸入職務工資:");
fflush(stdout);
rewind(stdin);
scanf("%d",&sta1[a].award1);//scsnf_s("%d",&sta1[a].award1);
printf("請輸入崗位津貼:");
fflush(stdout);
rewind(stdin);
scanf("%d",&sta1[a].award2);//scanf_s("%d",&sta12[a].award2);
printf("請輸入特殊津貼:");
fflush(stdout);
rewind(stdin);
scanf("%d",&sta1[a].award3);//scanf_s("%d",&sta1[a].award3);
sta1[a].award4=sta1[a].award + sta1[a].award1 + sta1[a].award2 + sta1[a].award3;
printf("工資:%d\n",sta1[a].award4);
a++;
printf("是否繼續錄入(如是則輸入1,否則輸入0):");
fflush(stdout);
rewind(stdin);
scanf("%d",&i);//scanf_s("%d",&i);
if(i==0) break;
}
return a;
}
void writetofile(STA sta1[],int a)
{
FILE *fp;
int i;
fp=fopen("cb.txt","w");
if(fp == NULL){//if(fopen_s(&fa,"cb.txt","w")==NULL){
printf("Failure to open text");
exit(0);
}
for(i=0;i<a;i++){
fprintf(fp,"%10ld%8s%5d%5d%5d%5d%5d",
sta1[i].worknum,sta1[i].name,sta1[i].award,sta1[i].award1,sta1[i].award2,sta1[i].award2,sta1[i].award3,sta1[i].award4);
}
fclose(fp);
}
int main(void)
{
//FILE *fp;
STA sta[100];
int b;
b=printdata(sta);
writetofile(sta,b);
return 0;
}
uj5u.com熱心網友回復:
vs里不支持scanf吧。
另外:fflush(stdout);這個是做什么?
uj5u.com熱心網友回復:
不知道怎么貼????
uj5u.com熱心網友回復:
沒有報錯,程式可以運行,但輸入后的資料存不到打開的檔案中
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/247763.html
標籤:C語言
上一篇:C語言撰寫
下一篇:新手請教一下多載運算子
