Thread: advice on file i/o

  1. #1
    Unregistered
    Guest

    advice on file i/o

    I've never done this so i'm looking for some advice. I have a two dimensional 6x6 array of structures, the struct has two integers, one float and two strings, these strings are used as pointers for linked lists that each element in the array can have. I have figured out how to print the array to a text file and back in, I can also print the linked lists to a file. My questions start here, firstly whats the advantage of using a binary file over a text file appart from it takes less space and less time. Secondly, I've seen an example of a way to print a whole array to file at once using a binary file, but i wasnt able to implement it myself, could someone give an example of the lines of code that would do this for a two dimensional array. Thirdly, since my linked lists are made by the user while the program is running, how can i print them to file and read them back, never knowing if there is a linked list, or how long one is if it exists, with a text file I would just read until EOF but I believe this would require me to have a seperate file for each list. I'm hopng that using a binary file can get rid of some of my problems, but i'm having trouble getting information on them. Thanks for any replies.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    reply

    Originally posted by Unregistered
    firstly whats the advantage of using a binary file over a text file appart from it takes less space and less time.
    Isn't that enough?


    Writing an array to a file:
    Code:
    
    int Array[20][30];
    
    FILE *File;
    if((File=fopen("MyFile.sav", "wb"))!=NULL)
    {
      fwrite(Array, sizeof(Array), 1, File);
      fclose(File);
    }
    
    One tip to the question about linked lists. Maybe you could have a header on your file where the size of the list is saved.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-30-2004, 11:21 PM