Closed Thread
Results 1 to 2 of 2

Thread: deleting stuff from files

  1. #1
    randomFIRE Guest

    deleting stuff from files

    How can I delete part of the text inside a file? I've navigated to the place where I need to delete using seekg, and now I can't think of how to actually get rid of that part of the document.

  2. CODECALL Circuit advertisement

     
  3. #2
    v0id is offline Retired
    Join Date
    Apr 2007
    Posts
    2,937
    Blog Entries
    3
    Rep Power
    42
    Usually when doing this, you don't do it directly to the file.

    First you are going to save the content of the file, using a input stream pointer (std:: ifstream), then manipulate with the content, and then use a output stream pointer (std:: ofstream) to write to the file again (use std::ios::trunc to overwrite file).

    This shows how to open, read, manipulate and then write to the file again:
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    #define FILENAME "test_file.txt"
    
    int main()
    {
    	std::string   fileContent;
    	std::string   fileContentTemporary;
    	std::ofstream outputFile;
    	std::ifstream inputFile;
    	
    	// Open the file, and read the content
    	inputFile.open(FILENAME, std::ios::in);
    	if(inputFile.is_open())
    	{
    		while(getline(inputFile, fileContentTemporary))
    			fileContent += fileContentTemporary;
    		inputFile.close();
    	}
    	
    	// Manipulate with the file content
    	fileContent += " -- this was appended to the file";
    	// ...
    	
    	// Open the file, and write the manipulated content
    	outputFile.open(FILENAME, std::ios::out | std::ios::trunc);
    	if(outputFile.is_open())
    	{
    		outputFile << fileContent;
    		outputFile.close();
    	}
    	
    	return 0;
    }
    A better way would maybe be, instead of having to stream pointers (I/O), then only have one - the multi-streamed one (std::fstream). This was just to show how to use both types of the stream pointers. If you want to use std::fstream, then you have to use it in the same way, just remember to set the right I/O-flags each time. After you've set the flags once, using the constructor, you can always use the member, std::fstream::flags().
    Last edited by v0id; 05-10-2007 at 11:53 AM.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Batch File - Deleting Log Files
    By Swatcat in forum General Programming
    Replies: 4
    Last Post: 03-16-2011, 03:55 PM
  2. moving and deleting thousands of files
    By rmse17 in forum General Programming
    Replies: 1
    Last Post: 10-12-2010, 05:55 AM
  3. Deleting temporary files
    By bleastan in forum PHP Development
    Replies: 7
    Last Post: 08-23-2010, 01:19 AM
  4. Deleting files from a CD - HELP!
    By travy92 in forum Visual Basic Programming
    Replies: 7
    Last Post: 09-01-2008, 09:25 AM
  5. Deleting files from a CD - HELP!
    By travy92 in forum Visual Basic Tutorials
    Replies: 1
    Last Post: 09-09-2007, 01:08 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts