Thread: SEEK_CUR issue

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    62

    SEEK_CUR issue

    Hi,

    I know I am not done with this, but I get an error that I don't know how to fix.

    I am trying to lower the inventory count of this data file.

    named productdata.txt
    id, price, and count
    Code:
       0 0.00 99   1 1.00 10   2 2.00 20   3 3.00 30   4 4.00 40   5 5.00 50   6 6.00 60   7 7.00 70   8 8.00 80   9 9.00 90
    I wrote this:
    to get into and do the work:
    Code:
     #include <stdio.h>
     #include <stdlib.h>
     #include "product.h"
     #define IDSize 4
     #define PriceSize 5
     #define CountSize 3
     #define RecordSize (IDSize+PriceSize+CountSize)
    
     int main ( int argc, char** argv )
     {
         int count;
         int currentID;
         int inputID;
         FILE *myfile;
         int set;
         int i;
         int oldCount;
    
         //fgets( FileName, sizeof( FileName ), stdin );
    
         //if( FileName[strlen( FileName ) - 1] == '\n' )
         //{
         //    FileName[strlen( FileName ) - 1] = '\0';
         //}
         myfile = fopen(FileName, "r");
         if (myfile == NULL)
         {
             printf("Cannot open %s\n", FileName);
             return(1);
         }
    
         printf( "Enter the ID of the product you wish to change: ");
         scanf( " %d", &inputID);
    
         for( i = 0; i < NumProducts; i++ )
         {
            if( inputID == Product[i].id )
             {
                break;
             }
         }
    
         while(1)
         {
             set = fscanf( FileName, "%d", currentID);
    
             if (set == 0)
             {
                 break;
             }
             if( currentID == inputID )
             {
                 fseek( FileName, PriceSize, SEEK_CUR );
                 fscanf( FileName, " %d", &count);
                 count--;
                 oldCount = count;
                 fseek( FileName, -CountSize, SEEK_CUR );
                 fprintf( FileName, "%3d", count);
                 break;
             }
             fseek( FileName, PriceSize + CountSize , SEEK_CUR );
         }
         printf( "Count of product ID %d changed from %d to %d\n", inputID, oldCount , count);
         return (0);
    }
    I was given this header to include as is:
    Code:
    #define FileName "productdata.txt"
    #define NumProducts 10
    #define RecordSize (4+5+3)
    
    struct Product
    {
        int        id;        // four-digit product ID code
        float    price;    // xx.xx product price (5 characters)
        int        count;    // three-digit count of units in stock
    };
    typedef  struct Product  Product;
    I am getting an error at:
    Code:
    if( inputID == Product[i].id )
    won't compile and gives error:

    syntax error before "Product"

    I am confused.

    As I said I know the program is not done yet but it will compile if I comment out:
    Code:
    for( i = 0; i < NumProducts; i++ )
         {
            if( inputID == Product[i].id )
             {
                break;
             }
         }
    Which obviously includes the problem code.

    Am I supposed to reference the header file?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need to declare a variable of the type Product [perhaps call it product], and use the variable. Currently, you are trying to use a struct, rather than a variable with the struct as a type.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    62
    ok thanks
    I declared
    Code:
    Product product;
    with the rest of the declarations and now I get:

    the variable 'product' is being used without being initialized.
    here

    Code:
    for( i = 0; i < NumProducts; i++ )
         {
            if( inputID == product.id[i] )
             {
                break;
             }
         }

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That means you haven't actually put any data into your struct variable before using it. It contains junk data which you're comparing against.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Why not just:
    Code:
    typedef struct
    {
        int        id;        // four-digit product ID code
        float    price;    // xx.xx product price (5 characters)
        int        count;    // three-digit count of units in stock
    } PRODUCT;
    PRODUCT product;
    instead of typing these stuff all over when you wanna to create a "PRODUCT-based" variable.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    62
    @ eXeCuTeR - The only answer I have for that is...I have to, it's part of the deal.

    @ Elysia - I know the variable is full of garbage, I am reading and searching and didn't see anything on how to get the data into the struct and then into the FILE* so I can manipulate it.

    If I weren't already voluntarily bald....

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, I'm not sure how your program works, but to write data, you can use fwrite, fprintf and for reading fread. You can read into a string and then convert it to appropriate data type and store in the struct. I don't know if there's an equalient function for reading as opposed to fprintf, though.

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    62
    hmm, I don't think I need this after all:
    Code:
    for( i = 0; i < NumProducts; i++ )
         {
            if( inputID == Product[i].id )
             {
                break;
             }
         }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Some weird issue.
    By dbzx in forum C Programming
    Replies: 7
    Last Post: 04-12-2009, 04:10 PM
  2. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 04:56 AM
  3. type safe issue
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 02-12-2008, 09:32 PM
  4. Replies: 8
    Last Post: 01-17-2006, 05:39 PM
  5. my first issue of GDM
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 09-12-2002, 04:02 PM