fclose()使c程序崩溃
我有一个相对简单的程序,它在从文件中读取整数后不断崩溃。它在执行fclose行时崩溃。我已将错误定位在此函数中fclose()使c程序崩溃,c,fclose,C,Fclose,我有一个相对简单的程序,它在从文件中读取整数后不断崩溃。它在执行fclose行时崩溃。我已将错误定位在此函数中 // Read array from text file int * fileRead() { FILE *file; file = fopen("test.txt", "r"); // Check if the file exists if(file == NULL){ printf("There was a problem open
// Read array from text file
int * fileRead() {
FILE *file;
file = fopen("test.txt", "r");
// Check if the file exists
if(file == NULL){
printf("There was a problem opening the file");
exit(1);
}
// Count number of lines in file
int count = 0;
char c;
for (c = getc(file); c != EOF; c = getc(file)){
if (c == '\n') {
count = count + 1;
}
}
// Reset to top of file
int t = fseek(file, 0, SEEK_SET);
// Read each line and save it to temp
int *temp = malloc(sizeof(int)*count);
int num, i;
for (i = 0; i<=count; i++){
fscanf(file, "%d\n", &temp[i]);
printf("%d\n", temp[i]);
}
fclose(file);
printf("Hello World\n");
return temp;
}
//从文本文件读取数组
int*fileRead(){
文件*文件;
file=fopen(“test.txt”、“r”);
//检查文件是否存在
if(file==NULL){
printf(“打开文件时出现问题”);
出口(1);
}
//计算文件中的行数
整数计数=0;
字符c;
for(c=getc(文件);c!=EOF;c=getc(文件)){
如果(c=='\n'{
计数=计数+1;
}
}
//重置到文件的顶部
int t=fseek(文件,0,搜索集);
//读取每一行并将其保存到temp
int*temp=malloc(sizeof(int)*计数);
int num,i;
对于(i=0;i注释:
452
55
542
55
c中的索引从0开始。因此,如果要保存count
整数,必须迭代到count-1
即
i
i您的代码中存在多个问题:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int *fileRead(int *countp) {
// Check if the file exists
FILE *file = fopen("PATH.txt", "r");
if (file == NULL) {
fprintf(stderr, "Cannot open input file PATH.txt: %s\n",
strerror(errno));
exit(1);
}
// Count number of lines in file
int num, count = 0;
while (fscanf(file, "%d", &num) == 1) {
count++;
}
// Reset to top of file
fseek(file, 0, SEEK_SET);
// Read each line and save it to temp
int *temp = calloc(sizeof(int), count);
if (temp != NULL) {
int i;
for (i = 0; i < count; i++) {
if (fscanf(file, "%d", &temp[i]) != 1) {
fprintf(stderr, "error reading element number %d\n", i);
break;
}
}
*countp = i; // number of entries successfully converted
}
fclose(file);
return temp;
}
int main(void) {
int count;
int *t = fileRead(&count);
if (t != NULL) {
for (int i = 0; i < count; i++) {
printf("%d\n", t[i]);
}
free(t);
}
return 0;
}
fgetc(fp)
返回一个int
值,该值可以包含类型为unsigned char
的所有值和特殊值EOF
。您必须将其存储到类型为int
的变量中,以确保文件结束测试的可靠性
- 在循环中,
for(i=0;我做了以下修复吗?;iIt修复了崩溃,这很好,但指针输出仍然出错。这是一个开始,谢谢!尝试fscanf(文件,“%d\n”,temp[i]);
@TonyTannous:不,不要尝试。您需要将指针传递到fscanf()
@JonathanLefflerint*temp
?建议用于计算行数的方法与OPs发布的代码具有相同的缺陷,即如果最后一行不包含“\n”,则行数将过低1此行:int I;
未初始化I
。此行:for(int I=0;I
屏蔽先前的i
变量,当执行for
代码块时,所包含的i
不存在:当执行此行时:*countp=i;
i
仍未初始化,因此调用方的countp
将被设置为垃圾。@user3629249:捕获良好!缺少 第一个fscanf()
参数的file
参数…看起来我在发布之前没有尝试编译;-)
for (i = 0; i < count; i++) ...
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int *fileRead(int *countp) {
// Check if the file exists
FILE *file = fopen("PATH.txt", "r");
if (file == NULL) {
fprintf(stderr, "Cannot open input file PATH.txt: %s\n",
strerror(errno));
exit(1);
}
// Count number of lines in file
int num, count = 0;
while (fscanf(file, "%d", &num) == 1) {
count++;
}
// Reset to top of file
fseek(file, 0, SEEK_SET);
// Read each line and save it to temp
int *temp = calloc(sizeof(int), count);
if (temp != NULL) {
int i;
for (i = 0; i < count; i++) {
if (fscanf(file, "%d", &temp[i]) != 1) {
fprintf(stderr, "error reading element number %d\n", i);
break;
}
}
*countp = i; // number of entries successfully converted
}
fclose(file);
return temp;
}
int main(void) {
int count;
int *t = fileRead(&count);
if (t != NULL) {
for (int i = 0; i < count; i++) {
printf("%d\n", t[i]);
}
free(t);
}
return 0;
}