/*
@Filename: ex804.c
@Author: Ju Chengdong
@Version: 1.0
@Date: 2021-03-18
@Description: Format Input and Output
*/
#include <stdio.h>
struct stu{
int id; //學號
char name[20]; //姓名
int age; //年齡
int score; //成績
};
int main(){
/*宣告函式及變數*/
void readFileForStu(struct stu *p, int num, char filename[],char mode[]);
void stdoutForStu(struct stu *p, int num);
const int NUM = 2; //學生數量
struct stu students[NUM]; /×u型陣列
struct stu *p = students; /×u型指標
char filename[] = "ex804.txt"; //待讀取的檔案路徑及名稱
/*從檔案獲取輸入*/
readFileForStu(p, NUM, filename, "r");
/*向螢屏輸出顯示*/
stdoutForStu(p, NUM);
return 0;
}
/*
* 函式名稱:readFileForStu
* 函式功能:從檔案格式化讀取資料
* 寫入格式:共讀取 num 行;每行讀出一個 stu 型資料,每個成員變數之間用空格分隔
* 形式引數:struct stu * p,指向 stu 型一維陣列首地址。該陣列用于保存檔案讀取資料
* 形式引數:int num,一維陣列元素個數,也即讀取檔案的行數
* 形式引數:char filename[],待讀取的檔案路徑及名稱
* 形式引數:char mode[],檔案使用方式
* 返 回 值:無
*/
void readFileForStu(struct stu *p, int num, char filename[],char mode[]){
// 請編程實作本函式
int i;
for(i=0;i<num;i++)
{
scanf("%d %s %d %d",&p[i].id,p[i].name,&p[i].age,&p[i].score);
}
FILE *fp;
fp=fopen("ex804.txt","mode");
for(i=0;i<num;i++)
{
fwrite(&p[i],sizeof(struct stu),1,fp);
printf("\n");
}
fclose(fp);
}
/*
* 函式名稱:stdoutForStu
* 函式功能:向顯示幕輸出顯示資料
* 輸出格式:共輸出 num 行;每行輸出一個 stu 型資料,每個成員變數之間用空格分隔
* 形式引數:struct stu * p,指向 stu 型一維陣列首地址
* 形式引數:int num,一維陣列元素個數
* 返 回 值:無
*/
void stdoutForStu(struct stu *p, int num){
// 請編程實作本函式
int i;
FILE *fp;
fp=fopen("ex804.txt","rb");
for(i=0;i<num;i++)
{
fread(&p[i],sizeof(struct stu),1,fp);
printf("%d %s %d %d\n",p[i].id,p[i].name,p[i].age,p[i].score);
}
fclose(fp);
}
請問代碼是哪里出錯了?


uj5u.com熱心網友回復:
fp=fopen("ex804.txt","mode"); 這條陳述句不會,應該是fp=fopen("ex804.txt",mode);這條陳述句的錯誤會導致檔案無法打開,導致fp為NULL,后面就可能會引發例外。
uj5u.com熱心網友回復:
修改如下,供參考:#include <stdio.h>
struct stu{
int id; //學號
char name[20];//姓名
int age; //年齡
int score; //成績
};
int main(){
/*宣告函式及變數*/
void readFileForStu(struct stu *p, int num, char filename[],char mode[]);
void stdoutForStu(struct stu *p, int num);
const int NUM = 2; //學生數量
struct stu students[NUM]; //stu型陣列
struct stu *p = students; //指向stu型一維陣列型指標
char filename[] = "ex804.txt"; //待讀取的檔案路徑及名稱
/*從檔案獲取輸入*/
readFileForStu(p,NUM,filename,"r");
/*向螢屏輸出顯示*/
stdoutForStu(p,NUM);
return 0;
}
/*
* 函式名稱:readFileForStu
* 函式功能:從檔案格式化讀取資料
* 寫入格式:共讀取 num 行;每行讀出一個 stu 型資料,每個成員變數之間用空格分隔
* 形式引數:struct stu * p,指向 stu 型一維陣列首地址。該陣列用于保存檔案讀取資料
* 形式引數:int num,一維陣列元素個數,也即讀取檔案的行數
* 形式引數:char filename[],待讀取的檔案路徑及名稱
* 形式引數:char mode[],檔案使用方式
* 返 回 值:無
*/
void readFileForStu(struct stu *p, int num, char filename[],char mode[])
{
// 請編程實作本函式
int i;
FILE *fp;
fp=fopen(filename,mode);
if(fp==NULL){
printf("Open file fail!\n");
return;
}
for(i=0;i<num;i++)
{
fread(&p[i],sizeof(struct stu),1,fp);
}
fclose(fp);
}
/*
* 函式名稱:stdoutForStu
* 函式功能:向顯示幕輸出顯示資料
* 輸出格式:共輸出 num 行;每行輸出一個 stu 型資料,每個成員變數之間用空格分隔
* 形式引數:struct stu * p,指向 stu 型一維陣列首地址
* 形式引數:int num,一維陣列元素個數
* 返 回 值:無
*/
void stdoutForStu(struct stu *p, int num)
{
// 請編程實作本函式
int i;
for(i=0;i<num;i++)
printf("%d %s %d %d\n",p[i].id,p[i].name,p[i].age,p[i].score);
}
//1 jjjj 18 90
//2 kkkk 19 91
//請按任意鍵繼續. . .
uj5u.com熱心網友回復:
大佬,我把你的程式放在測驗平臺上跑了一遍。輸出結果不正確

測驗資料1輸出錯誤
錯誤輸出:
1768693809 bai 100 1002 lihei 200 200 540028978 31580664196240 0 0
期望輸出:
1 libai 100 1002 lihei 200 200
測驗資料2輸出錯誤
錯誤輸出:
808464433 1 ZhangSan 25 9910002 ZhaoLiu 18 100@ 1512059440 1282367848824210793 8 100@ 0 0
期望輸出:
10001 ZhangSan 25 9910002 ZhaoLiu 18 100
uj5u.com熱心網友回復:
改這樣試試:#include <stdio.h>
struct stu{
int id; //學號
char name[20];//姓名
int age; //年齡
int score; //成績
};
int main(){
//宣告函式及變數
void readFileForStu(struct stu *p, int num, char filename[],char mode[]);
void stdoutForStu(struct stu *p, int num);
const int NUM = 2; //學生數量
struct stu students[NUM]; //stu型陣列
struct stu *p = students; //指向stu型一維陣列型指標
char filename[] = "ex804.txt"; //待讀取的檔案路徑及名稱
///檔案獲取輸入
readFileForStu(p,NUM,filename,"r");
//向螢屏輸出顯示
stdoutForStu(p,NUM);
return 0;
}
//
// * 函式名稱:readFileForStu
// * 函式功能:從檔案格式化讀取資料
// * 寫入格式:共讀取 num 行;每行讀出一個 stu 型資料,每個成員變數之間用空格分隔
// * 形式引數:struct stu * p,指向 stu 型一維陣列首地址。該陣列用于保存檔案讀取資料
// * 形式引數:int num,一維陣列元素個數,也即讀取檔案的行數
// * 形式引數:char filename[],待讀取的檔案路徑及名稱
// * 形式引數:char mode[],檔案使用方式
// * 返 回 值:無
void readFileForStu(struct stu *p, int num, char filename[],char mode[])
{
// 請編程實作本函式
int i;
FILE *fp;
fp=fopen(filename,mode);
if(fp==NULL){
printf("Open file fail!\n");
return;
}
for(i=0;i<num;i++)
{
//fread(&p[i],sizeof(struct stu),1,fp);
fscanf(fp,"%d%s%d%d",&p[i].id,p[i].name,&p[i].age,&p[i].score);
}
fclose(fp);
}
//
// * 函式名稱:stdoutForStu
// * 函式功能:向顯示幕輸出顯示資料
// * 輸出格式:共輸出 num 行;每行輸出一個 stu 型資料,每個成員變數之間用空格分隔
// * 形式引數:struct stu * p,指向 stu 型一維陣列首地址
// * 形式引數:int num,一維陣列元素個數
// * 返 回 值:無
void stdoutForStu(struct stu *p, int num)
{
// 請編程實作本函式
int i;
for(i=0;i<num;i++)
printf("%d %s %d %d\n",p[i].id,p[i].name,p[i].age,p[i].score);
}
//1 libai 100 100
//2 lihei 200 200
//請按任意鍵繼續. . .
//10001 ZhangSan 25 99
//10002 ZhaoLiu 18 100
//請按任意鍵繼續. . .
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/284807.html
標籤:C語言
下一篇:C語言試題改錯