/*
@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;
if((fp=fopen("ex804.txt",mode))==NULL)
{
printf("cannot open this file\n");
return;
}
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;
if((fp=fopen("ex804.txt","rb"))==NULL)
{
printf("cannot open this file\n");
return;
}
for(i=0;i<num;i++)
{
fread(&p[i],sizeof(struct stu),1,fp);
printf("%4d %-10s %4d %4d\n",p[i].id,p[i].name,p[i].age,p[i].score);
}
fclose(fp);
}
請問是哪里出了錯誤才導致輸出這個結果?

uj5u.com熱心網友回復:
都成了作業幫uj5u.com熱心網友回復:
單步跟蹤一下,就能知道錯誤出在哪里。要想學好一門編程語言,用好開發環境的debug功能是起碼的技能。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/284808.html
標籤:C++ 語言
上一篇:改正錯誤
下一篇:新手發帖的建議