Loading a file into memory ???

This is a discussion on Loading a file into memory ??? within the C++ Programming forums, part of the General Programming Boards category; I want to write a program to load a file into memory and then modify it, and then save the ...

  1. #1
    Unregistered
    Guest

    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
    Unregistered
    Guest
    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:
    Code:
    struct Record
    {
        //first line
        int item1,
            item2;
            item3;
        char item4;
        //extra lines
        char lineTwo[50],
             lineThree[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
    Location
    Hannover, Germany
    Posts
    3,139
    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.
    hth
    -nv

    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, 11:39 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 09: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, 01:18 PM

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