40

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".

#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

Number is: 0

Number is: 4200704

Number is: 2686672

Number is: 2686728

Number is: 2686916

Number is: 2004716757

Number is: 1321049414

Number is: -2

Number is: 2004619618

Number is: 2004966340

Number is: 4200704

Number is: 2686868

Number is: 4200798

Number is: 4200704

Number is: 8727656

Process returned 20 (0x14) execution time : 0.118 s Press any key to continue.

haccks
  • 104,019
  • 25
  • 176
  • 264
Vonti
  • 415
  • 1
  • 4
  • 7

6 Answers6

42

change to

fscanf(myFile, "%1d", &numberArray[i]);
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • @chux It is useless if the minus sign is attached because it has been that it is one of the maximum field width to read. – BLUEPIXY Dec 04 '13 at 15:29
  • 3
    Thanks, this works, but could you explain what exatly is happening with the 1 in the `%1d`. For example, `%2d` doesn't work. Also, I have another file that has 16 numbers but each number is separated by a comma. I thought that `fscanf(myFile, "%d,", &numberArray[i]);` would work but it doesn't. Can you help? – Vonti Dec 04 '13 at 15:54
  • 2
    @Vonti `"%1d"` `1`:read max size of fileld. I think that it may be a `"%d,"` if you are separated by a comma. – BLUEPIXY Dec 04 '13 at 16:02
  • This is what I have but it doesn't work. I'll open post another question with my code. There isn't so much room here. – Vonti Dec 04 '13 at 16:13
25

5623125698541159 is treated as a single number (out of range of int on most architecture). You need to write numbers in your file as

5 6 2 3 1 2 5  6 9 8 5 4 1 1 5 9  

for 16 numbers.

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(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;
}
haccks
  • 104,019
  • 25
  • 176
  • 264
  • My other file is in the format 1,2,3,4 etc but with no spaces. However adding the comma to the %d still doesn't work.`fscanf(myFile, "%d,", &numberArray[i]);` – Vonti Dec 04 '13 at 16:31
  • @Vonti, I am posting your full code after some modifications. See the edit. – haccks Dec 04 '13 at 16:37
  • Thanks this works great. I'm having some problem closing the file with fclose after the first part of my program. I run `fclose(myFile);` then on the next line `if (myFile == NULL) { printf("Error Reading File\n\n"); exit (0); } else { printf("file still ok\n\n"); }` but it says the else or "file still ok" which is really strange but I managed to close off (hide from each other)the 2 halves of the program with { } to get it to work. – Vonti Dec 04 '13 at 20:01
  • Yes. It should be. On next time the file open again by `fopen`. – haccks Dec 04 '13 at 23:05
  • 1
    This question was unrelated to what my actual problem is, but your answer directly answered what I was having a problem with... not to mention that you actually explain **why** the error is happening. Thank you. +1. – rayryeng Sep 26 '17 at 14:39
  • @rayryeng; I am glad that it helped :) – haccks Sep 26 '17 at 15:21
4
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:

5 6 2 3 ...
Fiddling Bits
  • 8,712
  • 3
  • 28
  • 46
2

Loop with %c to read the stream character by character instead of %d.

Pierre Arlaud
  • 4,040
  • 3
  • 28
  • 42
2

There are two problems in your code:

  • the return value of scanf must be checked
  • the %d conversion does not take overflows into account (blindly applying *10 + newdigit for each consecutive numeric character)

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.

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):

#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;
}
Loic
  • 640
  • 4
  • 13
0

enter your file input like this ex: 12 13 22 45 (after every number hit enter) then run your programm it will run properly

ROCKY 007
  • 71
  • 7