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

    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.


    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
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
  4. Replies: 3
  5. Please Help Me With File Into Memory
    By (TNT) in forum Windows Programming
    Replies: 1
    Last Post: 06-21-2002, 01:18 PM
