Working With Files

This is a discussion on Working With Files within the C Programming forums, part of the General Programming Boards category; The compiler gives me an error: Code: void openInv(struct inventory invDB) { disk_file = fopen("a:\may.inv", "rb+"); if (!disk_file) { printf("Error: ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    19

    Working With Files

    The compiler gives me an error:

    Code:
    void openInv(struct inventory invDB) 
    {
        disk_file = fopen("a:\\may.inv", "rb+");
        if (!disk_file) {
            printf("Error: Disk\n");
            exit(1);
            }
        fread(invDB, sizeof(invDB), 1, disk_file);
        fclose(disk_file);
    }
    Error:
    incompatible type for argument 1 of `fread'

  2. #2
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790
    Code:
      // Should be : void openInv(struct inventory *invDB), because you want it to be stored in a variable outside of the function scope
    void openInv(struct inventory invDB) 
    {
    // what is the type of disk_file? should be : FILE* disk_file = ...
        disk_file = fopen("a:\\may.inv", "rb+");
    // mainly a personal preference, but it should be : if(disk_file != NULL), 
      //as NULL is what is returned on error by fopen and you can't depend on NULL being
      // defined as 0 on all computers
        if (!disk_file) {
            printf("Error: Disk\n");
            exit(1);
            }
    
       // should be : fread((void*)invDB,sizeof(invDB), . . .); because 
      //fread needs a void * for the first argument. NOTE: for this part 
      //to work, you need to correct the mistakes I labeled above,
      // because currently, even if you obeyed the first correction you 
      // would be getting alot of other syntax errors
         fread(invDB, sizeof(invDB), 1, disk_file);
        fclose(disk_file);
    }
    EDIT: I presume you know about pointers... if not, I suggest reading the tutorials on this site.
    Last edited by EvBladeRunnervE; 06-03-2004 at 01:06 PM.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    19
    Oh, this is up at the top aswell:

    Code:
    FILE * disk_file;
    Also, invDB is an array of a struct.
    Last edited by Explicit; 06-03-2004 at 01:17 PM.

  4. #4
    'AlHamdulillah
    Join Date
    Feb 2003
    Posts
    790
    whoah, post your whole code, cause you are using global variables... a big no-no.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Deployment and DLL/OCX Files?
    By dfghjk in forum C++ Programming
    Replies: 5
    Last Post: 06-16-2008, 02:47 AM
  2. Ressources files
    By mikahell in forum Windows Programming
    Replies: 4
    Last Post: 06-19-2006, 06:50 AM
  3. Working with DLL files...
    By Devil Panther in forum Windows Programming
    Replies: 8
    Last Post: 11-14-2004, 11:42 PM
  4. working with resource files
    By the Wookie in forum Windows Programming
    Replies: 4
    Last Post: 02-01-2003, 09:26 AM
  5. Dos commands hehe
    By Carp in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 01-17-2003, 01:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21