Thread: Loading a file into memory ???

  1. #1

    Question Loading a file into memory ???

    I want to write a program to load a file into memory and then modify it, and then save the changes. I have tried many methods to do this, like storing each line in a structure and and then modifying, etc, but I have failed. Please tell me a method to achieve this, I don't want any code, just a method.

  2. #2
    If your file consists of simple lines where one line is all of the information you need to read one record then all you have to do is open the file for reading, open a second file for writing and read one line at a time. Do your processing on that line, save it to the out file and read another from the in file.

    If you need to read several lines to get one record then it would be best to wrap your data in a struct. Say you have one line with several items that need to be extracted and two more lines that are taken as a whole but must be kept with the data on the first line, you'd use a struct something like this:
    struct Record
        //first line
        int item1,
        char item4;
        //extra lines
        char lineTwo[50],
    The you read from the file with cin.getline() and extract the items of the first line with sscanf. When you're done processing, print out the data in the struct to your out file.

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Hannover, Germany
    What structure to use depends on your manipulation needs. The easiest way would be calculating filesize, allocate a block that large in memory and read the file into that block as a whole. Now you can manipulate it as you like. However, other structures might make manipulation easier.

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. Please Help Me With File Into Memory
    By (TNT) in forum Windows Programming
    Replies: 1
    Last Post: 06-21-2002, 12:18 PM