1

I want to process data given in a txt file. It has several rows and 2 columns. For my algorithm, I have to use a structure array so that each array item corresponds to one line in the txt file. Within one line, there is a string and a number. Therefore, I created two members of the structure. I go on row-by-row and fill in the structure. However, somehow, I have to reset the structure array pointer to the first element, and this is what I can't achieve.

#include <stdio.h>

void loadData(struct item *inputItems, FILE *inputStream);
void printItemStructure(struct item *itemArray, int nItems);

struct item{
    char *tag;
    double itemSize;
};


int main()
{
    /* Handle the input file */
    char *inputFileName = "d:\\Users\\User\\Downloads\\input.txt";
    FILE *input;
    input = fopen(inputFileName, "r");
    if (input == NULL){
        printf("Could not open the file for reading.");
        return -1;
    }
/* ========== Read the file content to the input structure ========== */

    /*Determine the number of items by counting the rows of the input file */
    int N = 0; /* number of items */
    int nChars = -1; /* number of characters (exclude EOF character) */
    char penChar; /* penultimate character */
    char currentChar = ' '; /* last character read from the file stream */
    char lastChar; /* number of characters */
    while (!feof(input)){
        lastChar = currentChar;
        currentChar = fgetc(input);
        nChars++;
        if (currentChar == '\n')
            N++;
    }
    if (lastChar != '\n' /* the file does not end with '\n' ... */
        && nChars != 0)  /* ... and is not empty */
        N++;

    /* Process data row-by-row */
    struct item *inputItems = calloc(N, sizeof(struct item));
    struct item *origItems = inputItems; /* this will be reseted */
    loadData(inputItems, input);

    inputItems = origItems;
    printItemStructure(inputItems, N);

    /* Close the file */
    fclose(input);

    return 0;
}

void loadData(struct item *inputItems, FILE *inputStream){
    rewind(inputStream);
    char *start = inputItems[0].tag;
    char row[200];
    int rowSize = 200;
    int i = 0;
    char title[200];
    double size;
    while (fgets(row, rowSize, inputStream)){
        sscanf(row, "%s %lf", title, &size); // why doesn't it work directly?
        inputItems[i].tag = title;
        inputItems[i].itemSize = size;
        printf("%s\n", row);
        i++;
    }
}

void printItemStructure(struct item *itemArray, int nItems){
    /* Print the members of the item structure array for debugging purposes */
    for (int j = 0; j<nItems; j++)
    {
        printf("\nitemArray[%d]\n   Tag: %s,   Size: %lf\n",
            j, itemArray[j].tag, itemArray[j].itemSize);
    }
}

Why can't I reset the *tag field of the inputItems structure array? The input.txt file contains:

Film1 1.8
Film2   4.25
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Zoltán Csáti
  • 679
  • 5
  • 17
  • possible duplicate of [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Natasha Dutta May 20 '15 at 09:47
  • You are reserving Memory with calloc. Where is it deallocated/? MAy this be the Problem?! Don`t understand your question completly. Which pointer and which Array element? –  May 20 '15 at 10:00
  • I think my problem is not as of "Why is “while ( !feof (file) )” always wrong?", because the strings are read properly but I just cannot reset the pointer `inputItems` to point to the first structure of a structure array. Miguel13366, you will see my problem if you run the code. Specifically, when printing the structure array using the function `printItemStructure`, `itputItems[0].tag` does not point to the first character of "Film1". It is because the pointer is not reset. – Zoltán Csáti May 20 '15 at 10:08
  • I forgot about `free` to deallocate memory, but it is not the case here, since `free` would follow the `printItemStructure` function where the problem occurs. – Zoltán Csáti May 20 '15 at 10:16
  • During, I saw that every element of the structure array `inputItems` contains the `inputItems[N-1].tag`. I just cannot figure out, why. Please help me. – Zoltán Csáti May 20 '15 at 10:39
  • user3121023, I used `calloc` and then `strcpy` as you advised. It works! Thank you very much, it was all that was missing from my larger code. How can I accept your answer, since you sent it as a comment? Perhaps you could copy your answer to the answer section so that I can accept it. – Zoltán Csáti May 20 '15 at 10:56

0 Answers0