Problem loading file...URGENT

This is a discussion on Problem loading file...URGENT within the C Programming forums, part of the General Programming Boards category; Hi, I've found quite a number of posts on this topic but all of those were how to read only ...

  1. #1
    Registered User khpuce's Avatar
    Join Date
    May 2003
    Posts
    165

    Problem loading file...URGENT

    Hi,

    I've found quite a number of posts on this topic but all of those were how to read only streams from a data file. I know fgets() can do the job but what if I have BOTH streams and floats/integers in my data file? I used structure to create the records and I am trying to load the whole file into the memory.

    After saving to the data file it looks like this :

    Name = aaa
    Address = bbb
    Total Hours Worked = 45
    Pay Rate = 12
    Wages = 540

    Name = ccc
    Address = ddd
    Total Hours Worked = 20
    Pay Rate = 12
    Wages = 240

    ...
    ...
    Note: Pay Rate & Wages are not saved as strings but as floats/integers.

    1. How can I load these records into memory?
    2. Will it help me if I save the records in my datafile like these :

    aaa
    bbb
    45
    12
    540

    ccc
    ddd
    20
    12
    240

    Any help from anyone...PLEASE...

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    1
    I think you can put your records in a binary file.
    You can use fwrite to put your entire record in the file and you can use fread to retrieve your records.
    So you can access to the values of your records easily.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    38
    Other than that you could probably do it the complicated way and have a fgets() for the different types you are using.
    Cheers,
    Johannes

  4. #4
    Registered User khpuce's Avatar
    Join Date
    May 2003
    Posts
    165
    Thanks to EVERYONE for your reply. I've solved the problem. My datafile now looks like this:

    name1
    address1
    45
    12
    540
    name2
    address2
    ...
    ...

    And I am using the following codes:

    Code:
    while(!feof(datafile))
       {
         
         fscanf(datafile,"%s",&List[index].name);
         fscanf(datafile,"%s",&List[index].address);
         fscanf(datafile,"%f",&List[index].Total_hrs);
         fscanf(datafile,"%f",&List[index].Pay_rate);
         fscanf(datafile,"%f",&List[index].wages);
         index++;
         
       }

  5. #5
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    >while(!feof(datafile))
    Bzzzt! This is wrong, look here to learn why.
    p.s. What the alphabet would look like without q and r.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 09:47 AM
  2. Error when loading 3ds file
    By The Wazaa in forum C++ Programming
    Replies: 6
    Last Post: 01-24-2006, 07:27 AM
  3. Replies: 6
    Last Post: 03-06-2005, 01:43 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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