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.

1
  • 44
    You have reached… a stackoverflow. True story. Commented Dec 4, 2013 at 14:51

6 Answers 6

42

change to

fscanf(myFile, "%1d", &numberArray[i]);
Sign up to request clarification or add additional context in comments.

4 Comments

@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.
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 "%1d" 1:read max size of fileld. I think that it may be a "%d," if you are separated by a comma.
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.
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;
}

6 Comments

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, I am posting your full code after some modifications. See the edit.
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.
Yes. It should be. On next time the file open again by fopen.
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.
|
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 ...

Comments

2

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

Comments

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;
}

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.