-1

So I am a beginner to C, and I am trying to open and read a file, storing each element of the file in an array. The following code seems like it should work in practice, but when the output gives me

50 2500

for whatever reason. If anybody could offer any help on what I need to fix, it would be greatly appreciated

...

Carter Klein
  • 103
  • 6
  • Possible duplicate of [Read int values from a text file in C](http://stackoverflow.com/questions/4600797/read-int-values-from-a-text-file-in-c) – Ken Y-N Sep 26 '16 at 03:32
  • I just edited my post to include the input file people – Carter Klein Sep 26 '16 at 03:34
  • Also, attempting to use the solution posed in "Read int values from a text file in C" does not work - potentially because my .txt file has commas whereas the other posts is all ints. – Carter Klein Sep 26 '16 at 03:40
  • magicSquareArray[0] is the first character in the file - what were you expecting it to contain? – user253751 Sep 26 '16 at 03:49
  • @immibis I was expecting it to contain 3, but when I printf the first element it prints 50 – Carter Klein Sep 26 '16 at 03:50

3 Answers3

1

When you use fgets(), you're pulling strings(character arrays) from the file with the newline char as the delimiter. fscanf() would be more appropriate for pulling ints from files.

The reason your program prints 50 is because the value of magicSquareArray[0] after the while loop is '2', not 2, and the ascii value of '2' is 50.

C. Rudnick
  • 11
  • 3
0

An example something like this

#include <stdio.h>

int main(int argc, char *argv[]){
    char *input = argv[1];//if(argc > 1)
    FILE *inputFile = fopen(input, "r");

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

    int n;
    fscanf(inputFile, "%d", &n);//if(1==
    int squareArray[n][n];
    int r, c, v;
    char comma;
    r = c = 0;
    while(2==fscanf(inputFile, "%d%c", &v, &comma)){
        if(comma == ',' || comma == '\n' && c == n-1){//comma == ',' && c < n-1
            squareArray[r][c] = v;
            if(++c == n){
                c = 0;
                ++r;
            }
        } else {
            fprintf(stderr, "input file invalid format\n");
            return -2;
        }
    }
    fclose(inputFile);

    if(r != n || c != 0){
        fprintf(stderr, "It could not be read correctly.\n");
        return -3;
    }

    for(r = 0; r < n; ++r){
        for(c = 0; c < n; ++c){
            printf("%d ", squareArray[r][c]);
        }
        puts("");
    }
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
0

You can use this function:

strucT *ReadFromFile (const char *fileName)
{
    printf ("\n***Start function ReadFromFile for file name: %s *** \n" , fileName );   
    strucT *myfilePtr = (strucT*) malloc ( sizeof (strucT) );

    strcpy( myfilePtr->fileName, fileName );    
    myfilePtr->file = fopen ( myfilePtr->fileName , "r");
    int i = 0;

    fscanf( myfilePtr->file , "%d", &myfilePtr->rows);
    printf("rowSize is: %d\n" , myfilePtr->rows);

    fscanf( myfilePtr->file , "%d", &myfilePtr->colus);
    printf("culSize is: %d\n" , myfilePtr->colus);

    myfilePtr->elementNumber = (myfilePtr->rows) * (myfilePtr->colus);
    printf("arraySize is %ld\n", myfilePtr-> elementNumber);

//  myfilePtr->array = (unsigned char*)malloc(sizeof(unsigned char) * (myfilePtr->elementNumber));
    for (i = 0 ; i < myfilePtr->elementNumber; i++)
             {
                fscanf(myfilePtr->file, "%1d", (int*) &myfilePtr->array[i]);    
                //printf("%d" , myfilePtr.array[0]);
            }
// FOR DEBUG    
for (i = 0 ; i < 12 ; i++)
        printf ("%d" , myfilePtr->array[i]);
    printf ("\n");

    fclose(myfilePtr->file);
    printf ("\n***Finish function ReadFromFile for file name: %s *** \n" , fileName );  
    return strucT;
}

this function reads the first and second int and save them. after this she reads the array valus (each value is 1 character)

Yosef Alon
  • 78
  • 12