I'm a programming noob so please bear with me.
我是编程新手,所以请容忍我。
I'm trying to read numbers from a text file into an array. The text file, "somenumbers.txt" simply holds 16 numbers as so "5623125698541159".
我试着把文本文件中的数字读入数组。somenumbers文本文件。”txt"只包含16个数字,如"5623125698541159"。
#include <stdio.h>
main()
{
FILE *myFile;
myFile = fopen("somenumbers.txt", "r");
//read file into array
int numberArray[16];
int i;
for (i = 0; i < 16; i++)
{
fscanf(myFile, "%d", &numberArray[i]);
}
for (i = 0; i < 16; i++)
{
printf("Number is: %d\n\n", numberArray[i]);
}
}
The program doesn't work. It compiles but outputs:
程序不工作。它编译但输出:
Number is: -104204697
号码是:-104204697
Number is: 0
数:0
Number is: 4200704
号码是:4200704
Number is: 2686672
号码是:2686672
Number is: 2686728
号码是:2686728
Number is: 2686916
号码是:2686916
Number is: 2004716757
号码是:2004716757
Number is: 1321049414
号码是:1321049414
Number is: -2
数量:2
Number is: 2004619618
号码是:2004619618
Number is: 2004966340
号码是:2004966340
Number is: 4200704
号码是:4200704
Number is: 2686868
号码是:2686868
Number is: 4200798
号码是:4200798
Number is: 4200704
号码是:4200704
Number is: 8727656
号码是:8727656
Process returned 20 (0x14) execution time : 0.118 s Press any key to continue.
进程返回20 (0x14)执行时间:0.118秒按任意键继续。
5 个解决方案
#1
21
change to
改变
fscanf(myFile, "%1d", &numberArray[i]);
#2
11
5623125698541159
is treated as a single number (out of range of int
on most architecture). You need to write numbers in your file as
5623125698541159被视为单个数字(在大多数体系结构上超出int范围)。您需要在文件as中写入数字
5 6 2 3 1 2 5 6 9 8 5 4 1 1 5 9
for 16 numbers.
16的数字。
If your file has input
如果你的文件有输入
5,6,2,3,1,2,5,6,9,8,5,4,1,1,5,9
then change %d
specifier in your fscanf
to %d,
.
然后在fscanf中更改%d说明符到%d。
fscanf(myFile, "%d,", &numberArray[i] );
Here is your full code after few modifications:
这是经过一些修改后的完整代码:
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE *myFile;
myFile = fopen("somenumbers.txt", "r");
//read file into array
int numberArray[16];
int i;
if (myFile == NULL){
printf("Error Reading File\n");
exit (0);
}
for (i = 0; i < 16; i++){
fscanf(myFile, "%d,", &numberArray[i] );
}
for (i = 0; i < 16; i++){
printf("Number is: %d\n\n", numberArray[i]);
}
fclose(myFile);
return 0;
}
#3
2
for (i = 0; i < 16; i++)
{
fscanf(myFile, "%d", &numberArray[i]);
}
This is attempting to read the whole string, "5623125698541159"
into &numArray[0]
. You need spaces between the numbers:
这是尝试读取整个字符串,“5623125698541159”到&numArray[0]。你需要数字之间的空格:
5 6 2 3 ...
#4
2
Loop with %c to read the stream character by character instead of %d.
使用%c循环以按字符读取流字符,而不是按%d。
#5
1
There are two problems in your code:
您的代码中有两个问题:
- the return value of
scanf
must be checked - 必须检查scanf的返回值。
- the
%d
conversion does not take overflows into account (blindly applying*10 + newdigit
for each consecutive numeric character) - %d转换不考虑溢出(盲目地为每个连续的数字字符应用*10 +新数字)
The first value you got (-104204697
) is equals to 5623125698541159
modulo 2^32
; it is thus the result of an overflow (if int
where 64 bits wide, no overflow would happen). The next values are uninitialized (garbage from the stack) and thus unpredictable.
你第一个值(-104204697)等于-104204697模2 ^ 32;因此,它是溢出的结果(如果int宽度为64位,则不会发生溢出)。下一个值未初始化(来自堆栈的垃圾),因此不可预测。
The code you need could be (similar to the answer of BLUEPIXY above, with the illustration how to check the return value of scanf
, the number of items successfully matched):
您需要的代码可以是(类似上面BLUEPIXY的答案,说明如何检查scanf的返回值,成功匹配的项目数量):
#include <stdio.h>
int main(int argc, char *argv[]) {
int i, j;
short unsigned digitArray[16];
i = 0;
while (
i != sizeof(digitArray) / sizeof(digitArray[0])
&& 1 == scanf("%1hu", digitArray + i)
) {
i++;
}
for (j = 0; j != i; j++) {
printf("%hu\n", digitArray[j]);
}
return 0;
}
#1
21
change to
改变
fscanf(myFile, "%1d", &numberArray[i]);
#2
11
5623125698541159
is treated as a single number (out of range of int
on most architecture). You need to write numbers in your file as
5623125698541159被视为单个数字(在大多数体系结构上超出int范围)。您需要在文件as中写入数字
5 6 2 3 1 2 5 6 9 8 5 4 1 1 5 9
for 16 numbers.
16的数字。
If your file has input
如果你的文件有输入
5,6,2,3,1,2,5,6,9,8,5,4,1,1,5,9
then change %d
specifier in your fscanf
to %d,
.
然后在fscanf中更改%d说明符到%d。
fscanf(myFile, "%d,", &numberArray[i] );
Here is your full code after few modifications:
这是经过一些修改后的完整代码:
#include <stdio.h>
#include <stdlib.h>
int main(){
FILE *myFile;
myFile = fopen("somenumbers.txt", "r");
//read file into array
int numberArray[16];
int i;
if (myFile == NULL){
printf("Error Reading File\n");
exit (0);
}
for (i = 0; i < 16; i++){
fscanf(myFile, "%d,", &numberArray[i] );
}
for (i = 0; i < 16; i++){
printf("Number is: %d\n\n", numberArray[i]);
}
fclose(myFile);
return 0;
}
#3
2
for (i = 0; i < 16; i++)
{
fscanf(myFile, "%d", &numberArray[i]);
}
This is attempting to read the whole string, "5623125698541159"
into &numArray[0]
. You need spaces between the numbers:
这是尝试读取整个字符串,“5623125698541159”到&numArray[0]。你需要数字之间的空格:
5 6 2 3 ...
#4
2
Loop with %c to read the stream character by character instead of %d.
使用%c循环以按字符读取流字符,而不是按%d。
#5
1
There are two problems in your code:
您的代码中有两个问题:
- the return value of
scanf
must be checked - 必须检查scanf的返回值。
- the
%d
conversion does not take overflows into account (blindly applying*10 + newdigit
for each consecutive numeric character) - %d转换不考虑溢出(盲目地为每个连续的数字字符应用*10 +新数字)
The first value you got (-104204697
) is equals to 5623125698541159
modulo 2^32
; it is thus the result of an overflow (if int
where 64 bits wide, no overflow would happen). The next values are uninitialized (garbage from the stack) and thus unpredictable.
你第一个值(-104204697)等于-104204697模2 ^ 32;因此,它是溢出的结果(如果int宽度为64位,则不会发生溢出)。下一个值未初始化(来自堆栈的垃圾),因此不可预测。
The code you need could be (similar to the answer of BLUEPIXY above, with the illustration how to check the return value of scanf
, the number of items successfully matched):
您需要的代码可以是(类似上面BLUEPIXY的答案,说明如何检查scanf的返回值,成功匹配的项目数量):
#include <stdio.h>
int main(int argc, char *argv[]) {
int i, j;
short unsigned digitArray[16];
i = 0;
while (
i != sizeof(digitArray) / sizeof(digitArray[0])
&& 1 == scanf("%1hu", digitArray + i)
) {
i++;
}
for (j = 0; j != i; j++) {
printf("%hu\n", digitArray[j]);
}
return 0;
}