-1

I have tried for about a day and a half. Tried several methods to put my file's integers into array. Can you help please? The output I get now is the same number repeated over and over.

if ((nfPtr=fopen("c:\\Users\\raphaeljones\\Desktop\\newfile.dat","r"))==NULL)
{
    printf("File could not be opened\n");
}
else {
    printf("The integers you have entered are: \n");
    fscanf(nfPtr,"%d\n",&i);
    while(!feof(nfPtr)){
        for (count=0;count<=SIZE;count++){  
            fscanf(nfPtr,"%d",&array[i]);
            i++;
            printf("%d\n",i);
        }
    }
}//end else
fclose(nfPtr);
getch();    
return 0;
Brodie
  • 429
  • 1
  • 5
  • 16
Raphael Jones
  • 115
  • 2
  • 12

2 Answers2

1

In this code:

i++;
printf("%d\n",i);

You're incrementing the index (i) then printing it? Did you intend to print the numbers read from the file instead? Perhaps you want:

printf("%d\n",array[i]);
i++;
Jay
  • 13,803
  • 4
  • 42
  • 69
  • doesn't work. but thanks. i want to read how many ever integers are in the file. so i increment i. but it's wrong logic. just not sure what to tell the computer. – Raphael Jones Jun 09 '15 at 16:39
1

You can try something like:

for (i = 0; fscanf(nfPtr, "%d", &array[i]) == 1; i++) {
   printf("%d\n", array[i]);
}

Provided your array is always big enough (which you usually don't know), otherwise you have to dynamically allocate space for each new element.

Tlacenka
  • 520
  • 9
  • 15