0

I am fairly new to the C language and allocating memory/using pointers in general. Anyway, I was experimenting with reading a file, putting those values in a struct, etc. I know exactly what I want to do and of course the program runs but the output is incorrect and some kind of jumbled numbers and letters.

There is a text file with new information for each line. Each line represents one object.

This is how a line in the file might look:

meat sirloin 6.55 8 8.50 4

Overall I want to be able to store all of my PRODUCT objects in an array (so I have an array of structs). So I attempted to allocate memory with a pointer, use a line count, and then send the pointer to a function called read. In read I add each struct to the array via a pointer. The program doesn't crash, the output is just not correct and I have no clue why not. It's prob something with pointers. If anyone could help me I would really appreciate it. Any help at all would be great.

      //prototype
void read(pointerToArr);


typedef struct
{
    char supType[15];
    char prodName[15];
    double wholePrice;
    int quantWhole;
    double retPrice;
    int retProdQuantity;
}PRODUCT;

FILE *fr;
int lineCount = 0;
int main()
{
    PRODUCT *ptr;

    int i;
    char check[50];


     fr = fopen("ttt.txt", "r");

      while(fgets(check, sizeof(check), fr)!= NULL)
      {
          if(check[0] != '\n')
          {
              lineCount++;
          }
      }
    // allocate memory for array based on line count.
     PRODUCT prodContainter[lineCount];
     ptr = (PRODUCT*)malloc(sizeof(PRODUCT)*lineCount);
     ptr = prodContainter;

     read(ptr);

      //print after adding to array via pointer from other
      //function. this was a test.

      for(i = 0; i < lineCount; i++)
      {
          printf("%s ", prodContainter[i].supType);
          printf("%s ", prodContainter[i].prodName);
          printf("%f ", prodContainter[i].wholePrice);
          printf("%d ", prodContainter[i].quantWhole);
          printf("%f ", prodContainter[i].retPrice);
          printf("%d\n\n", prodContainter[i].retProdQuantity);

      }

    return 0;
}



void read(PRODUCT *pointerToArr)
{

    // objective in this method is to read in data from the file, create an object for every line and
    // then use the pointer array to add those objects to prodConstainer up above.

       char supplyName[15];
       char productName[15];
       double wholeP = 0;
       int  quantityWhole = 0;
       double retailPrice = 0;
       int retailProductQuant = 0;

    while(fscanf(fr, "%s %s %lf %d %lf %d", supplyName, productName, &wholeP, &quantityWhole, &retailPrice, &retailProductQuant) == 6)
    {
        PRODUCT record;
        int i;

        strcpy(record.supType, supplyName);
        strcpy(record.prodName, productName);
        record.wholePrice = wholeP;
        record.quantWhole = quantityWhole;
        record.retPrice = retailPrice;
        record.retProdQuantity = retailProductQuant;

        for(i = 0; i < lineCount; i++)
        {
            pointerToArr[i] = record;
        }
    }

    fclose(fr);
}
unwind
  • 391,730
  • 64
  • 469
  • 606
Tastybrownies
  • 897
  • 3
  • 23
  • 49

1 Answers1

2

You never rewind the file, so all the reading after you count the number of lines fails.

What you're printing is just what happens to be in memory.

There are many ways to fix this, of course.

  1. Rewind the file, using rewind()
  2. Close the file and let your read() function (whose name collides with a POSIX standard function, btw) re-open the file. This would also involve removing the scary global variable fr.
  3. Re-structure so you never count the number of lines, by just reading and letting the ptr array grow as necessary (see realloc()).

Also, you should really avoid casting the return value of malloc() in C. This:

ptr = (PRODUCT*)malloc(sizeof(PRODUCT)*lineCount);

is better written as:

ptr = malloc(lineCount * sizeof *ptr);

This does away with the cast, and also uses sizeof on a value of the type pointed at to automatically compute the proper number of bytes to allocate.

Community
  • 1
  • 1
unwind
  • 391,730
  • 64
  • 469
  • 606
  • I did decide to remove fr from being a global variable and closed the file after reading the number of lines. I renamed my function from read to addToArray, opened the file newly in the other function, and simplified my malloc declaration. Unfortunately I am not on my home computer at the moment so I can't compile everything and give it a go. I am guessing that there is some problem with how I added each new record in the bottom function, no? Thank youvery much for your help. – Tastybrownies Jan 31 '13 at 17:05