0

I'm writing a program in C, in which I am reading the data from a .txt file, and my goal is to put each element from the .txt file into an array. When I compile and run the program, the values of 50, 55, and 0 are returned. These are the ASCII values (I'm not sure why the elements are being stored as ASCII codes, but that's okay for now) for 2, 7, and 0 (meaning nothing was initialized since we reached the end of the .txt file. Why is my program not reading the .txt file from the beginning??

...

int main(int argc, char *argv[]){
FILE *inputFile;
char *input = argv[1];
char magicSquareArray[257];

inputFile = fopen(input, "r");

if (inputFile == 0){
    printf("Cannot open file for reading!\n");
    return -1;
}

fscanf(inputFile, "%s", magicSquareArray);
while (!feof(inputFile)){
    fscanf(inputFile, "%s", magicSquareArray);
}

printf("%i\n", magicSquareArray[0]);
int sideSize = magicSquareArray[0];
int squareSize = sideSize * sideSize;
printf("%i\n", squareSize);

fclose(inputFile);

The text file:

3
4,3,8
9,5,1
2,7,6
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
Carter Klein
  • 103
  • 6
  • 3
    `while (!feof(inputFile)){ fscanf(inputFile, "%s", magicSquareArray); }` --> `while (1 == fscanf(inputFile, "%256s", magicSquareArray);` or something like that. do not use `magicSquareArray` if the result of `fscanf(...%s...) != 1` – chux - Reinstate Monica Sep 27 '16 at 03:53
  • 2
    You will want to look at [**Why is while ( !feof (file) ) always wrong?**](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). – David C. Rankin Sep 27 '16 at 03:56
  • @chux thank you, but sadly I am still getting 55 (ASCII code for 2) :( – Carter Klein Sep 27 '16 at 03:58
  • You read characters with `%s`, so if you want a single integer value from a single character use, eg. `printf("%i\n", magicSquareArray[0] - '0');` and use `%d` for integer values to avoid interpretation as hex or octal in case of a leading `0`. – David C. Rankin Sep 27 '16 at 04:00
  • "getting 55 (ASCII code for 2)" --> Certainly 55 is not the ASCII code for 2. 55 is the ASCII code for `'7'`. – chux - Reinstate Monica Sep 27 '16 at 04:04
  • Sorry, I mean 50. Put given @DavidC.Rankin's comment, I fixed that. My main issue still lies in the fact that it's starting at the 12th element and not the first for whatever reason – Carter Klein Sep 27 '16 at 04:05
  • 1
    After each `fscanf()` that returns a value of 1, try `printf("<%s>\n", magicSquareArray);` to see what code is reading. – chux - Reinstate Monica Sep 27 '16 at 04:07
  • Hm... When doing that, I get: <3> <4,3,8> <9,5,1> <2,7,6> So clearly something is going right. I just don't see how I could be printing the file properly, but when I go to access magicSquareArray[0] I still get 2... – Carter Klein Sep 27 '16 at 04:11
  • The last time `fscanf()` succesfuuly read data into `magicSquareArray`, it read `"2,7,6"`, so `magicSquareArray[0]` is `'2'`. No _magic_ involved. – chux - Reinstate Monica Sep 27 '16 at 04:15
  • 1
    Repeated calls to `fscanf(inputFile, "%s", magicSquareArray);` do not append data into `magicSquareArray`. It does read from the file where is left off previously reading and then puts the next file data into `magicSquareArray`. Good luck. – chux - Reinstate Monica Sep 27 '16 at 04:19

1 Answers1

0

Perhaps you want the code such as the following. (However, I think in the following manner. To prepare an array read the first number, To assign a numerical value to read into it.)

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char *argv[]){
    FILE *inputFile;
    char *input = argv[1];
    char magicSquareArray[257];
    int ch, len;
    inputFile = fopen(input, "r");

    if (inputFile == 0){
        printf("Cannot open file for reading!\n");
        return -1;
    }
    len = 0;
    while((ch = fgetc(inputFile)) != EOF && len < sizeof(magicSquareArray)-1){
        magicSquareArray[len++] = ch;
    }
    magicSquareArray[len] = 0;
    fclose(inputFile);

    printf("%c\n", magicSquareArray[0]);
    int sideSize = atoi(magicSquareArray);
    int squareSize = sideSize * sideSize;
    printf("%i\n", squareSize);

    return 0;
}
Community
  • 1
  • 1
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70