从fopen取得的写入句柄总是NULL
从fopen获得的写入句柄总是NULL
有下面代码:
调试运行时总是出现 Debug Assertion Failed 错误(vc++ 6.0)
下断点发现,fin 句柄总是NULL 。
可是尝试简单代码时,形如:
却又是正常的。。。
保证确实存在gift1.in 文件
这是怎么回事?
------解决方案--------------------
FILE * fin = fopen("gift1.in","r+");
FILE * fout = fopen("gift1.out","w+");
------解决方案--------------------
"r"
Opens for reading. If the file does not exist or cannot be found, the fopen call fails.
"w"
Opens an empty file for writing. If the file exists, its contents are destroyed.
有下面代码:
/*
ID: wangkai8
LANG: C
TASK: gift1
*/
#include <stdio.h>
#include <malloc.h>
#define NAMESIZE 14
typedef struct PERSON {
char name[NAMESIZE + 1];
int account;
} person, *pPerson;
int inParser(FILE*, pPerson);
void doCalcu(FILE*, pPerson, int);
void outParser(FILE*, pPerson, int);
void readln(FILE*, char*);
int nameIndexer(char*, pPerson, int);
int main() {
FILE* fin = fopen("gift1.in", "r");
FILE* fout = fopen("gift1.out", "w");
pPerson people;
int num ;
int numC;
if(fin== NULL) printf("%s","File cannot open...");
num = inParser(fin, people);
for (numC = 0; numC < num; numC++) {
doCalcu(fin, people, num);
}
outParser(fout, people, num);
free(people);
exit(0);
}
int inParser(FILE* fin, pPerson people) {
// read the first line of file ,determining the
// size of struct array .
int num = 0;
int i;
char ch;
fscanf(fin, "%d", &num);
fscanf(fin, "%c", &ch); //get the newline char .
// if(ch!='\n') stderr
// load in struct array
people = (pPerson) malloc(num * sizeof(person));
if (people == NULL )
return 0;
for (i = 0; i < num; ++i) {
person another;
another.account = 0;
// read line loop
readln(fin, another.name);
people[i] = another;
}
return num;
}
void doCalcu(FILE* fin, pPerson people, int Pnum) {
// keep reading file...
//read giver name
char giver[NAMESIZE + 1];
int account, num, giverInx;
char ch;
int i;
int index;
readln(fin, giver);
//read giver account and num of recipient
fscanf(fin, "%d", &account);
fscanf(fin, "%c", &ch);
fscanf(fin, "%d", &num);
fscanf(fin, "%c", &ch);
giverInx = nameIndexer(giver, people, Pnum);
people[giverInx].account += -account + (account % num);
// calculate account
for (i = 0; i < num; ++i) {
char recvr[NAMESIZE + 1];
readln(fin, recvr);
index = nameIndexer(recvr, people, Pnum);
people[index].account += (int) account / num;
}
}
void readln(FILE* fin, char* line) {
char rChar = '\0';
int i = 0;
fscanf(fin, "%c", &rChar); //read in the first char of this line
while (rChar != '\n') {
line[i++] = rChar;
fscanf(fin, "%c", &rChar);
}
line[i] = '\0';
}
int nameIndexer(char* name, pPerson people, int num) {
int i = 0;
for (i = 0; i < num; ++i) {
int j = 0;
while (name[j] == people[i].name[j]) {
if (name[j] == '\0')
return i;
j++;
}
}
return -1; //never reach in this case
}
void outParser(FILE* fout, pPerson people, int Pnum) {
int i = 0;
while (i < Pnum) {
fprintf(fout, "%s %d\n", people[i].name, people[i].account);
}
}
调试运行时总是出现 Debug Assertion Failed 错误(vc++ 6.0)
下断点发现,fin 句柄总是NULL 。
可是尝试简单代码时,形如:
#include <stdio.h>
typedef struct PERSON {
char name[NAMESIZE + 1];
int account;
} person, *pPerson;
int main(){
char buff[14];
FILE * fin = fopen("gift1.in","r");
FILE * fout = fopen("gift1.out","w");
fscanf(fin,"%s",buff);
printf("%s",buff);
return 0;
}
却又是正常的。。。
保证确实存在gift1.in 文件
这是怎么回事?
------解决方案--------------------
FILE * fin = fopen("gift1.in","r+");
FILE * fout = fopen("gift1.out","w+");
------解决方案--------------------
"r"
Opens for reading. If the file does not exist or cannot be found, the fopen call fails.
"w"
Opens an empty file for writing. If the file exists, its contents are destroyed.