我有以下代碼:C/C++流和文件
int checkCorrectness(int i,char *iStr)
{
if(atoi(iStr) == i)
return 1;
return 0;
}
void foo(int i)
{
printf("inside foo %d\n",i);
}
void print()
{
char mystring[100];
freopen("myfile.txt","w+",stdout);
for(int i =0;i < 100;++i)
{
foo(i);
FILE *f = fopen("myfile.txt","r");
if (f == NULL) perror ("Error opening file");
else {
while (fgets (mystring , 100 , f) != NULL);
if(!checkCorrectness(i,mystring);
break;
fclose (f);
}
}
fclose(stdout);
}
這段代碼保存我的意思是它確定的fopen調用freopen函數被調用其流沒有關閉後?謝謝
爲什麼你在C++中使用C文件函數...... 你也應該使用字符串對象,而不是直接作用於內存 – Prime