Thread: database....

  1. #1
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129

    database....

    I dont know much about structures so i need help.

    I am making a database.....for say WAL-MART.

    WHat I am trying to do is to read from a file (WMinventory.dat) and send data from the file to a structure, but I get a bunch of error which i still havent been able to fix.

    To make it easier for me i took the programme down to its simplest form.....i cant even read one line from the file.

    Heres my STructure:
    Code:
    typedef struct{
          char name [50];
          float price;
          int noofItems;
          char expiry;
          char type;
          char detail;
    }structure;
    Heres the first 6 lines from my data file.
    Code:
    Sony 29" TV
    699.99
    5
    N/A
    Electronic
    Flat Screen WEGA
    Heres my attempt....i am trying to read one line (Sony 29" TV) and put it in (char name [50].
    Code:
    #define dataFileName "WMinventory.dat"
    #define MAXchar (100)
    
    void loadData(structure ,FILE *);
    FILE *open_file(char [], char []);
    
    int main(void)
    {
        structure inventory;
        FILE *dataFile;
        
        dataFile = open_file(dataFileName ,"r");
        
        loadData(inventory,dataFile);
        
        puts(inventory.name);
        
        fclose(dataFile);
        getchar();
        return 0;
    }
    
    void loadData(structure inventory,FILE *dataFile)
    {
         fgets(inventory.name, MAXchar,dataFile);
    }
    
    FILE *open_file(char fileName[], char accessType[])
    {
        FILE *fptr;
        if ( NULL == (fptr = fopen(fileName, accessType))) {
            puts("FILE OPEN ERROR:");
            puts(fileName);
            puts("\n");
            system ("pause");
            abort();
            }
        return fptr;
    }
    When i try to compile i get this error:
    " undefined reference to `loadData(structure*, _iobuf*)' "

    If i figure this out the rest of the program will become easier....any help would be appreciated.

    [EDIT:] Changed a few things around and now I get this error:
    "cannot convert `structure' to `structure*' for argument `1' to `void loadData(structure*, FILE*)"

    [EDIT:] Changed some more things and now i'm stuck....the program runs but prints out some garbage...._"3½┬wHô├8="
    Last edited by nomi; 01-11-2004 at 11:09 AM.

  2. #2
    Climber spoon_'s Avatar
    Join Date
    Jun 2002
    Location
    ATL
    Posts
    182
    you want to write the structure to the file as binary. use the "wb" arguments when you open a file with fopen.

    check out:

    http://cboard.cprogramming.com/showt...ctures+to+file
    {RTFM, KISS}

  3. #3
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    Originally posted by spoon_
    you want to write the structure to the file as binary. use the "wb" arguments when you open a file with fopen.
    I rather want to write the file to the structureright now....but i will use that^^^ later....thx

  4. #4
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    I have searched through the forums already....

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Some questions: Do you have to have the data in the file be in a readable format? If not look into fread() and fwrite().
    Second look at the following closely and tell me if you can't see a problem:
    Code:
          char expiry;
          char type;
          char detail;
    Code:
    N/A
    Electronic
    Flat Screen WEGA
    If you still can't find the problem let me tell you . You are allocating storage for one character but are trying to get 3, 10, and 16 characters.

  6. #6
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    Originally posted by Thantos

    If you still can't find the problem let me tell you . You are allocating storage for one character but are trying to get 3, 10, and 16 characters.
    Well that shouldnt make difference right now coz i am not using them.....i am using name[50].....which i okay....

    I am opening the file as a read only....it is a dat file i created....since i am not writing to the file it shouldnt be a big problem...

    thx for pointing that out...but my problem is still left unsolved

  7. #7
    Climber spoon_'s Avatar
    Join Date
    Jun 2002
    Location
    ATL
    Posts
    182
    this seems to work, the simple way:

    Code:
    #include<stdio.h>
    
    typedef struct
    {
    	char name[50];
    }mystruct;
    
    int main(void)
    {
    	FILE * fin = NULL;
    	mystruct stuff;
    
    	fin = fopen("WMinventory.dat", "r");
    
    	if(fin == NULL)
    	{
    		printf("error: file open failed.\n");
    		return 0;
    	}
    
    	fgets(stuff.name, 100, fin);
    
    	printf("%s\n", stuff.name);
    
    	fclose(fin);
    
        return 0;
    }
    {RTFM, KISS}

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
    void loadData(structure *,FILE *);
    FILE *open_file(char [], char []);
    
    int main(void)
    {
        structure inventory;
        FILE *dataFile;
        
        dataFile = open_file(dataFileName ,"r");
        
        loadData(&inventory,dataFile);
        
        puts(inventory.name);
        
        fclose(dataFile);
        getchar();
        return 0;
    }
    
    void loadData(structure *inventory,FILE *dataFile)
    {
         fgets(inventory->name, MAXchar,dataFile);
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Climber spoon_'s Avatar
    Join Date
    Jun 2002
    Location
    ATL
    Posts
    182
    change:

    Code:
    void loadData(structure inventory,FILE *dataFile)
    {
         fgets(inventory.name, MAXchar,dataFile);
    }
    to:

    Code:
    void loadData(structure * inventory,FILE *dataFile)
    {
         fgets(inventory->name, MAXchar,dataFile);
    }
    and change:

    Code:
    loadData(inventory,dataFile);
    to:

    Code:
    loadData(&inventory,dataFile);
    damn, BEATEN! =)

    sorry for the confusion earlier. correct the stuff that thantos pointed out and you should be on your way.

    you were not modifying the structure in the 'main' function when you called 'fgets.' you were passing a copy of the structure, not a reference.

    *waits for better explantion*
    Last edited by spoon_; 01-11-2004 at 11:57 AM.
    {RTFM, KISS}

  10. #10
    Senor Member nomi's Avatar
    Join Date
    Jan 2004
    Posts
    129
    ....Works....

    thx for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. literature database: help with planning
    By officedog in forum C++ Programming
    Replies: 1
    Last Post: 01-23-2009, 12:34 PM
  2. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. Developing database management software
    By jdm in forum C++ Programming
    Replies: 4
    Last Post: 06-15-2004, 04:06 PM
  5. Making a Simple Database System
    By Speedy5 in forum C++ Programming
    Replies: 1
    Last Post: 03-14-2003, 10:17 PM