尝试从C中的txt文件中读取数字时出错
我是C编程新手,我得到了一个线程1:EXC\u BAD\u访问(代码=1,地址0x68) 当我运行我的程序时。我的代码的目的是从包含正数和负数的txt文件中读取数据,并对其进行处理尝试从C中的txt文件中读取数字时出错,c,file-io,C,File Io,我是C编程新手,我得到了一个线程1:EXC\u BAD\u访问(代码=1,地址0x68) 当我运行我的程序时。我的代码的目的是从包含正数和负数的txt文件中读取数据,并对其进行处理 #include <stdio.h> int main (int argc, const char * argv[]) { FILE *file = fopen("data.txt", "r"); int array[100]; int i = 0; int num;
#include <stdio.h>
int main (int argc, const char * argv[]) {
FILE *file = fopen("data.txt", "r");
int array[100];
int i = 0;
int num;
while( fscanf(file, "%d" , &num) == 1) { // I RECEIVE THE ERROR HERE
array[i] = num;
printf("%d", array[i]);
i++;
}
fclose(file);
for(int j = 0; j < sizeof(array); j++){
printf("%d", array[j]);
}
}
#包括
int main(int argc,const char*argv[]{
FILE*FILE=fopen(“data.txt”、“r”);
整数数组[100];
int i=0;
int-num;
虽然(fscanf(文件,“%d”,&num)==1){//我在这里收到了错误
数组[i]=num;
printf(“%d”,数组[i]);
i++;
}
fclose(文件);
对于(int j=0;j
之后
FILE *file = fopen("data.txt", "r");
说
只是猜测一下,其余的代码看起来还可以,所以很可能这就是问题所在。之后
FILE *file = fopen("data.txt", "r");
说
只是猜测一下,代码的其余部分看起来还可以,所以很可能这就是问题所在。还值得注意的是,您的文件中可能有100多个数字,在这种情况下,您将超出数组的大小。尝试用以下代码替换while循环:
for (int i = 0; i < 100 && ( fscanf(file, "%d" , &num) == 1); ++i)
{
array[i] = num;
printf("%d", array[i]);
}
for(inti=0;i<100&&(fscanf(文件,“%d”,&num)==1);+i)
{
数组[i]=num;
printf(“%d”,数组[i]);
}
另外值得注意的是,您的文件中可能有100多个数字,在这种情况下,您将超出数组的大小。尝试用以下代码替换while循环:
for (int i = 0; i < 100 && ( fscanf(file, "%d" , &num) == 1); ++i)
{
array[i] = num;
printf("%d", array[i]);
}
for(inti=0;i<100&&(fscanf(文件,“%d”,&num)==1);+i)
{
数组[i]=num;
printf(“%d”,数组[i]);
}
您是否创建了文件“data.txt”并将其放在本地
touch data.txt
echo 111 222 333 444 555 > data.txt
检查文件打开是否成功
这是一个工作版本
#include <stdio.h>
#include <stdlib.h> //for exit
int main (int argc, const char * argv[])
{
FILE *fh; //reminder that you have a file handle, not a file name
if( ! (fh= fopen("data.txt", "r") ) )
{
printf("open %s failed\n", "data.txt"); exit(1);
}
int array[100];
int idx = 0; //never use 'i', too hard to find
int num;
while( fscanf(fh, "%d" , &num) == 1) { // I RECEIVE THE ERROR HERE
array[idx] = num;
printf("%d,", array[idx]);
idx++;
}
printf("\n");
fclose(fh);
//you only have idx numbers (0..idx-1)
int jdx;
for(jdx = 0; jdx<idx; jdx++)
{
printf("%d,", array[jdx]);
}
printf("\n");
}
#包括
#包含//以退出
int main(int argc,const char*argv[]
{
FILE*fh;//提醒您有一个文件句柄,而不是文件名
如果(!(fh=fopen(“data.txt”,“r”))
{
printf(“打开%s失败\n”,“data.txt”);退出(1);
}
整数数组[100];
int idx=0;//永远不要使用“i”,因为太难找到
int-num;
虽然(fscanf(fh,“%d”,&num)==1){//我在这里收到了错误
数组[idx]=num;
printf(“%d”,数组[idx]);
idx++;
}
printf(“\n”);
fclose(fh);
//您只有idx号码(0..idx-1)
int-jdx;
对于(jdx=0;jdx您是否创建了文件“data.txt”并将其放在本地
touch data.txt
echo 111 222 333 444 555 > data.txt
检查文件打开是否成功
这是一个工作版本
#include <stdio.h>
#include <stdlib.h> //for exit
int main (int argc, const char * argv[])
{
FILE *fh; //reminder that you have a file handle, not a file name
if( ! (fh= fopen("data.txt", "r") ) )
{
printf("open %s failed\n", "data.txt"); exit(1);
}
int array[100];
int idx = 0; //never use 'i', too hard to find
int num;
while( fscanf(fh, "%d" , &num) == 1) { // I RECEIVE THE ERROR HERE
array[idx] = num;
printf("%d,", array[idx]);
idx++;
}
printf("\n");
fclose(fh);
//you only have idx numbers (0..idx-1)
int jdx;
for(jdx = 0; jdx<idx; jdx++)
{
printf("%d,", array[jdx]);
}
printf("\n");
}
#包括
#包含//以退出
int main(int argc,const char*argv[]
{
FILE*fh;//提醒您有一个文件句柄,而不是文件名
如果(!(fh=fopen(“data.txt”,“r”))
{
printf(“打开%s失败\n”,“data.txt”);退出(1);
}
整数数组[100];
int idx=0;//永远不要使用“i”,因为太难找到
int-num;
虽然(fscanf(fh,“%d”,&num)==1){//我在这里收到了错误
数组[idx]=num;
printf(“%d”,数组[idx]);
idx++;
}
printf(“\n”);
fclose(fh);
//您只有idx号码(0..idx-1)
int-jdx;
对于(jdx=0;jdx最有可能fopen
失败,并且file
为NULL
。代码的另一个问题是sizeof(array)
。它将返回字节大小,您将超出范围。您应该使用sizeof(array)/sizeof(int)
或sizeof(array)/sizeof(array[0])
。此外,在您的情况下,最好将i
重命名为num\u count
,并使用它来代替sizeof
。@Patrick M也给出了一个很好的建议,只是您不应该将i
增加两次。很可能fopen
失败且文件为NULL
。代码的另一个问题是大小of(array)
。它将返回字节大小,您将超出范围。您应该使用sizeof(array)/sizeof(int)
或sizeof(array)/sizeof(array[0])
。另外,在您的情况下,最好将i
重命名为num\u count
,并用它代替sizeof
。@Patrick M也给出了一个很好的建议,除了您不应该将i
增加两次。我会尝试printf(“无法打开%s\n”,“data.txt”)
而不是printf(“无法打开%s\n”,“data.txt”)
@Charlie Burns我想这就是问题所在。我不确定data.txt文件是否在正确的目录中。@cnill很公平。旧习惯很难改掉。我会尝试printf(“无法打开%s\n”,“data.txt”);
而不是perror(“fopen”)
@Charlie Burns我想这就是问题所在。我不确定data.txt文件是否在正确的目录中。@cnill很公平。旧习惯很难改掉。