/*源程式change_I/O*/
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAXSIZE
int main(void)
{
FILE *fd;
int i,size,k,num,len;
char *buf;
char buf_r[10],a[100];
len = strlen(buf);
printf("***Welcome to the system of students' scores!***\n\n");
printf("************************************************\n");
printf("********************Design by cbp***************\n");
/*首先呼叫 open 函式,并指定相應的權限*/
if((fd = fopen("/home/cbp/score.c", "rw" ))<0){
perror("open:");
exit(1);
}
else
printf("open file:score.c");
if((size =fread(buf_r,sizeof(char),1,fd))<0){
perror("read:");
exit(1);
}
else
printf("number:%s\n",buf_r);
num=atoi(buf_r);
for(i=0;i<num;i++){
a[i]=-1;}
for(i=0;i<num;i++){
if((size=fread(buf_r,sizeof(8),1,fd))<0)
{
perror("read:");
exit(1);
}
else{
a[i]=atoi(buf_r);
k=1;
printf("read from file:%d\n",a[i]);
int num1=atoi(buf_r);
if(num1>=90)
printf("youxiu:%d\n",num1);
else { if(num1>=80)
printf("lianghao:%d\n",num1);
else {if(num1>=70)
printf("zhongdeng:%d\n",num1);
else {if(num1>=60)
printf("jige:%d\n",num1);
else printf("bujige:%d\n",num1);}}}}
}
if(fclose(fd) < 0 ){
perror("close:");
exit(1);
}
else
printf("Close score.c\n");
exit(0);
}


uj5u.com熱心網友回復:
/home/cbp/score.c這個在windows有這個路徑和檔案嗎?if((fd = fopen("/home/cbp/score.c", "rw" ))<0){
perror("open:");
exit(1);
}這個有問題呢,fd應該是和NULL比較。
原因是這個地方沒有執行到exit,其實是因為找不到檔案fd是NULL了,但是NULL < 0又不滿足,所以沒有exit,后面直接用fd出現段錯誤了。
解決辦法:
if((fd = fopen("/home/cbp/score.c", "rw" ))== NULL){
perror("open:");
exit(1);
}在Windows下試試,看看能不能直接運行perror和exit(windows下沒有/home/目錄)
樓主需要了解windows 和LINUX系統的區別,不能拿到代碼就用,因為會有移植的問題~
uj5u.com熱心網友回復:
希望對您有幫助:https://blog.csdn.net/it_xiangqiang/category_10581430.html轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/283640.html
標籤:C語言
