Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-10-2007, 01:52 PM
randomFIRE randomFIRE is offline
Newbie
 
Join Date: May 2007
Posts: 1
Rep Power: 0
randomFIRE is on a distinguished road
Default 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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 05-10-2007, 02:49 PM
v0id's Avatar   
v0id v0id is offline
Retired
 
Join Date: Apr 2007
Location: Denmark
Posts: 2,654
Last Blog:
CherryPy(thon)
Rep Power: 29
v0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of lightv0id is a glorious beacon of light
Send a message via MSN to v0id
Default

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().
__________________
05-03-2007 - 11-13-2008

Last edited by v0id; 05-10-2007 at 02:53 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I read .mbm (Multi BitMap) files? moondog General Programming 7 08-07-2007 08:08 AM
location of download files and configuration problem meimei3936 ionFiles 4 06-29-2007 07:28 AM
Can you ever delete all old files on your computer? littlefranciscan Computer Software/OS 36 04-02-2007 01:40 PM
Java:Tutorial - Importing Files John Java Tutorials 0 01-11-2007 03:00 PM
Java Help Files xXHalfSliceXx Java Help 3 11-29-2006 12:30 AM


All times are GMT -5. The time now is 02:04 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 100%


Complete - Celebrate!

Ads