Thread: Problem with files in C programm

  1. #1
    Registered User
    Join Date
    Jun 2014
    Posts
    3

    Problem with files in C programm

    Greetings! Im kinda new around here, aand c programm altogether. I have reached a standstill in a programm im currently working at and i was hoping for a bit of insight/help. Ill try to explain things thouroughly and simply the best i can, as said im new here and im not sure how things are done per say :P

    Anyhow the formentioned program is as follows:

    Code:
    #include <stdio.h>
    
    
    
    
    typedef struct{
     int code;
     char description[100];
     int volume;
    }product;
    
    
    int main() {
    
    
    void add(FILE *filePtr);
    void removal(FILE *filePtr);
    void show(FILE *filePtr);
    void edit(FILE *filePtr);
    void description(FILE *filePtr);
    
    
    int pick =0, i;
    product p;
    FILE *filePtr;
    
    
    
    
    if ( ( filePtr = fopen( "stock.dat", "a+")) == NULL ) {
        printf("The file could not be read");
    }
    else {
    
    
    
    
    do{
       printf("    ************ MAIN MENU ************\n");
       printf("  ** -------------welcome------------- **\n");
       printf(" **                                      ** \n");
       printf("*  Please select one of the commands below *\n");
       printf("********************************************\n");
       printf("* Press 1 to remove products from the list *\n");
       printf("* Press 2 to change a product's quantity   *\n");
       printf("* Press 3 to add a new product to the list *\n");
       printf("* Press 4 to view product list             *\n");
       printf("* Press 5 to exit the programm             *\n");
       printf("********************************************\n");
       scanf("%d", &pick);
    
    
       switch ( pick ) {
    
    
       case 1:
          removal(filePtr);
        break;
    
    
       case 2:
          edit(filePtr);
        break;
    
    
       case 3:
         add(filePtr);
       break;
    
    
       case 4:
         show(filePtr);
       break;
    
    
    
    
    
    
    
    
    
    
       default:
          printf("Error! Please enter a correct number");
       break;
     }
     }while (pick != 5);
    
    
      }
    
    
    
    
    
    
    
    
    fclose(filePtr);
    
    
    
    
    return 0;
    }
    
    
    
    
    void removal(FILE *fileptr) {
        int code;
    
    
        typedef struct {
            int code;
            char description[100];
            int volume;
        } product;
    
    
        product p;
        product empty = { 0, " ", 0 };
    
    
        printf("\n\nYou are about to delete a record, please enter the product's code");
        scanf("%d", &code);
    
    
        fseek( fileptr, (code - 1) * sizeof(p), SEEK_SET);
        fread( &p.code, sizeof(p), 1, fileptr);
    
    
        if (p.code == 0){
            printf("Product code not found, please retry");
        } else {
            fseek( fileptr, (code -1) * sizeof(p), SEEK_SET);
    
    
            fwrite( &empty, sizeof(p), 1, fileptr);
    
    
            printf("Code was found, product removed successfully\n\n");
        }
    }
    
    
     void add(FILE *fileptr){
    
    
    
    
      typedef struct{
      int code;
      char description[100];
      int volume;
     }product;
    
    
      product p;
    
    
      int code, quantity;
    
    
      printf("You are about to add a new product in the list\n Please input the product's code\n");
    
    
      scanf("%d", &code);
    
    
    
    
      fseek( fileptr, (code-1) * sizeof(p), SEEK_SET);
      fread(&code, sizeof(p), 1, fileptr);
    
    
    
    
        fseek(fileptr, (code-1) * sizeof(p), SEEK_SET);
        fwrite( &code, sizeof(p), 1, fileptr);
    
    
        printf("Now enter the product's remaining quantity");
        scanf("%d", &quantity);
    
    
        fseek(fileptr, (quantity-1) * sizeof(p), SEEK_SET);
        fwrite( &quantity, sizeof(p), 1, fileptr);
    
    
    
    
    
    
    
    
    
    
     }
    
    
    
    
     void edit(FILE *fileptr){
    
    
      typedef struct{
      int code;
      char description[100];
      int volume;
     }product;
    
    
      product p;
    
    
      int code, volume;
    
    
      printf("\nYou are about to edit a existing product's quantity\n Please enter the product's code\n");
      scanf("%d", &code);
    
    
      fseek(fileptr, (code -1) * sizeof(p), SEEK_SET);
      fread(&code, sizeof(p), 1, fileptr);
    
    
      if (p.code == 0){
        printf("\nThe code you entered is not in the list, please retry\n");
      }else{
        printf("\nCode found!\n Product's  current quantity is %d", &p.volume);
        printf("\n Please enter the new quantity");
        scanf("%d", &volume);
    
    
        p.volume = volume;
    
    
    
    
        fseek(fileptr, (volume -1) * sizeof(p), SEEK_SET);
        fwrite(&volume, sizeof(p), 1, fileptr);
    
    
        }
    
    
    
    
     }
    
    
    
    
    void show(FILE *fileptr){
    
    
     typedef struct{
      int code;
      char description[100];
      int volume;
     }product;
    
    
     product p;
    
    
     int i=1;
    
    
    
    
     printf("\n\n\n               ****SHOWING ALL PRODUCTS****\n\n\n");
     printf("PRODUCT CODE      QUANTITY IN STOCK\n");
    
    
    
    
    
    
     while (i<100 && !feof(fileptr)){
        fscanf( fileptr, "%d%d", &p.code, &p.volume);
        printf("%d            %d\n", &p.code, &p.volume);
        fseek(fileptr, (p.code + 1) * sizeof(p), SEEK_SET);
        fscanf( fileptr, "%d%d", &p.code, &p.volume);
        i++;
       }
     printf("You may scroll back up to view the list\n\n");
    
    
     }
    My main problem lies ( i think so at least) within the functions. In short, the program must simulate a menu, with which the user can either view, edit,add,delete records in a file named stock.dat . So far so good, the program does run, however it seems it fails saving the newly added records properly, and to it's extend to show them via case 4 (that open the show function). I hope i was descriptive enough, again its my first post and one of my first programs sooo thank you in advance for any suggestions!!

    PS dont hate me too much for any silly mistakes :/

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,452
    First, you've defined your structure in the global space so there is no need to keep defining the structure in all the functions.

    Next you should be passing the instance of this structure that you created in main() to and from your functions, not declaring new instances in every function.

    Last, for now anyway, you need to decide if you're going to use unformatted input and output (read()/write()) or formatted input and output (fscanf()/fprintf()) and stick to one or the other. If you're going to use the unformatted method then you really should open the file in binary mode as well. Mixing formatted and unformatted input and output will usually end in problems.

    Jim

  3. #3
    Registered User
    Join Date
    Jun 2014
    Posts
    3
    Alriht as Jim stated i did change some parts of this code, switching the file to binary, passing the struct to the functions. I did get it to work ONCE, i have no idea why, but ever since my first run it crashes almost immidiately. the code is as follows

    Code:
    #include <stdio.h>
    
    
    
    
    typedef struct{
     int code;
     char description[100];
     int volume;
    }product;
    
    
    
    
    
    
    int main() {
    
    
    
    
    
    
    int pick =0, i;
    product *p;
    FILE *filePtr;
    
    
    void add(FILE *filePtr, product* p);
    void removal(FILE *filePtr, product* p);
    void show(FILE *filePtr, product* p);
    void edit(FILE *filePtr, product* p);
    void description(FILE *filePtr, product* p);
    
    
    
    
    if ( ( filePtr = fopen( "stock.dat", "ab+")) == NULL ) {
        printf("The file could not be read");
    }
    else {
    
    
    
    
    do{
       printf("    ************ MAIN MENU ************\n");
       printf("  ** -------------welcome------------- **\n");
       printf(" **                                      ** \n");
       printf("*  Please select one of the commands below *\n");
       printf("********************************************\n");
       printf("* Press 1 to remove products from the list *\n");
       printf("* Press 2 to change a product's quantity   *\n");
       printf("* Press 3 to add a new product to the list *\n");
       printf("* Press 4 to view product list             *\n");
       printf("* Press 5 to exit the programm             *\n");
       printf("********************************************\n");
       scanf("%d", &pick);
    
    
       switch ( pick ) {
    
    
       case 1:
          removal(filePtr, p);
        break;
    
    
       case 2:
          edit(filePtr, p);
        break;
    
    
       case 3:
         add(filePtr, p);
       break;
    
    
       case 4:
         show(filePtr, p);
       break;
    
    
    
    
    
    
    
    
    
    
       default:
          printf("Error! Please enter a correct number");
       break;
     }
     }while (pick != 5);
    
    
      }
    
    
    
    
    
    
    
    
    fclose(filePtr);
    
    
    
    
    return 0;
    }
    
    
    
    
    void removal(FILE *fileptr, product * p) {
        int code;
    
    
    
    
        product empty = { 0, " ", 0 };
    
    
        printf("\n\nYou are about to delete a record, please enter the product's code");
        scanf("%d", &code);
    
    
        fseek( fileptr, (code - 1) * sizeof(p), SEEK_SET);
        fread( &p->code, sizeof(p), 1, fileptr);
    
    
        if (p->code == 0){
            printf("Product code not found, please retry");
        } else {
            fseek( fileptr, (code -1) * sizeof(p), SEEK_SET);
    
    
            fwrite( &empty, sizeof(p), 1, fileptr);
    
    
            printf("Code was found, product removed successfully\n\n");
        }
    }
    
    
     void add(FILE *fileptr, product * p){
    
    
      int code, quantity;
    
    
      printf("You are about to add a new product in the list\n Please input the product's code\n");
    
    
      scanf("%d", &code);
    
    
    
    
      fseek( fileptr, (code-1) * sizeof(p), SEEK_SET);
      fread(&code, sizeof(p), 1, fileptr);
    
    
    
    
        fseek(fileptr, (code-1) * sizeof(p), SEEK_SET);
        fwrite( &code, sizeof(p), 1, fileptr);
    
    
        printf("Now enter the product's remaining quantity");
        scanf("%d", &quantity);
    
    
        fseek(fileptr, (quantity-1) * sizeof(p), SEEK_SET);
        fwrite( &quantity, sizeof(p), 1, fileptr);
    
    
    
    
    
    
    
    
    
    
     }
    
    
    
    
     void edit(FILE *fileptr, product * p){
    
    
      int code, volume;
    
    
      printf("\nYou are about to edit a existing product's quantity\n Please enter the product's code\n");
      scanf("%d", &code);
    
    
      fseek(fileptr, (code -1) * sizeof(p), SEEK_SET);
      fread(&code, sizeof(p), 1, fileptr);
    
    
      if (p->code == 0){
        printf("\nThe code you entered is not in the list, please retry\n");
      }else{
        printf("\nCode found!\n Product's  current quantity is %d", &p->volume);
        printf("\n Please enter the new quantity");
        scanf("%d", &volume);
    
    
        p->volume = volume;
    
    
    
    
        fseek(fileptr, (volume -1) * sizeof(p), SEEK_SET);
        fwrite(&volume, sizeof(p), 1, fileptr);
    
    
        }
    
    
    
    
     }
    
    
    
    
    void show(FILE *fileptr, product * p){
    
    
     int i=1;
    
    
    
    
     printf("\n\n\n               ****SHOWING ALL PRODUCTS****\n\n\n");
     printf("PRODUCT CODE      QUANTITY IN STOCK\n");
    
    
    
    
    
    
     while (i<100 && !feof(fileptr)){
        fscanf( fileptr, "%d%d", &p->code, &p->volume);
        printf("%d            %d\n", &p->code, &p->volume);
        fseek(fileptr, (p->code + 1) * sizeof(p), SEEK_SET);
        fscanf( fileptr, "%d%d", &p->code, &p->volume);
        i++;
       }
     printf("You may scroll back up to view the list\n\n");
    
    
     }
    Any ideas why :/ ?

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    3,676
    Point to the line of code where you set this line to a valid value?
    Code:
    product *p;
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Jun 2014
    Posts
    3
    shortly after i changed it to
    Code:
    product *p = { 0, "", 0};
    Problem persists

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    3,676
    Learn how to use pointers or do NOT use them!!!
    Pointers in C - Tutorial - Cprogramming.com

    I suggest NOT using them in this problem!
    Edit: As in declare the variable as NOT being a pointer; then pass the variable by address.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,452
    There are many problems with your program. First as already noted above you're not allocating any memory for that pointer. It appears that you aren't trying to use an array so just use a single "normal" non-pointer instance.

    Next look at your fopen() call. You're using the "ab+" open mode specifier which means that all output is occurring at the end of the file. So editing the file is out of the question.

    Is it an assignment requirement that you use binary files with read()/write()?

    If so I really suggest you start over, first write the add function, then the display function. Once these two functions are operating properly, (you can write then read back the information), continue with the rest of the functions. As written your display function will probably not read what you wrote in your add function because you are mixing unformatted output and formatted input. If you use write() you need to use read() to read the data. Also since you're using a structure it might be easier to write() and read() the entire structure at once, instead of the individual member variables one at a time.

    Your seek operations also seem incorrect. I suggest you re-read the documentaion for this standard function. And remember if you use the "ab+" specifier seeking before a write operation is useless since writes always happen at the end of the file. You'll probably want to change your open mode if you want to edit the actual records. Untill you actually get the add function to work I suggest the "wb+" so you always start with an empty file. Once you get the add and display functions to work you can change the the "rb+" mode.

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 02-04-2013, 11:33 AM
  2. TCP/IP server/client programm +files
    By rounsmith in forum C Programming
    Replies: 2
    Last Post: 04-03-2011, 01:45 PM
  3. a programm plz help me!!!!
    By chaudhryali55 in forum C++ Programming
    Replies: 1
    Last Post: 11-21-2009, 07:29 AM
  4. Which programm should I use to ...
    By samus535 in forum Game Programming
    Replies: 3
    Last Post: 10-04-2005, 11:43 AM
  5. problem with my srand programm
    By datainjector in forum C Programming
    Replies: 10
    Last Post: 07-15-2002, 07:15 AM

Tags for this Thread