writing \b to a file

This is a discussion on writing \b to a file within the C++ Programming forums, part of the General Programming Boards category; Hi all, Im trying to delete the last character from a file called myfile.txt The contents of myfile.txt is just ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    100

    writing \b to a file

    Hi all,

    Im trying to delete the last character from a file called myfile.txt The contents of myfile.txt is just text saying "cloudy". I want the programme to remove the last charachter from that text so it says just "cloud". This is my code:

    Code:
    ofstream file_op("myfile.txt", ios::app);
    file_op << '\b';
    file_op.close();
    The program compliles and runs fine but when i check the contents of myfile.txt it still says "cloudy" but there is a funny box at the end of it. I know that the box at the end represnts the backspace charachter but i want it to actually remove the last charchter and not just show the backspace charachter. Any idea how to get around this? Sorry if me explanation isnt too good. Oh by the way, this bit of code below is not the solution i'm looking for:

    Code:
    ofstream file_op("myfile.txt", ios::out);
    file_op<<"cloud";
    file_op.close();
    Thanks for any help

  2. #2
    Registered /usr
    Join Date
    Aug 2001
    Location
    Newport, South Wales, UK
    Posts
    1,263
    The backspace control code \b can only be interpreted by the standard output stream. Using it anywhere else results in that funny square you saw.

    I'm not sure how best this could be accomplished, but have you tried:-
    Code:
    file_op.seekp(-1, ios::end);
    file_op << EOF;
    file_op.close();

  3. #3
    Registered User
    Join Date
    Oct 2004
    Posts
    100
    thanks for the reply mate :-)

    Your code prints the following in the text file "cloudy-1". So it isnt what I want. Do you have any other ideas?

    Thanks mate

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    if you don't mind having an extra space at the end of your file:
    Code:
    #include <fstream>
    #include <cstdlib>	//testing purposes
    
    int main()
    {
    	//the next two lines are for testing
    	system("cp test.out test.in");
    	system("cat test.in");
    
    	//the space to be written to the file
    	char ch=' ';
    	
    	//open the file for binary reading/writing
    	std::fstream file("test.in",std::ios::out|std::ios::in|std::ios::binary);
    	//seek to the last char in the file
    	file.seekp(-2,std::ios::end);
    	//overwrite it with a space
    	file.write(reinterpret_cast<char*>(&ch),sizeof(ch));
    	//close the file
    	file.close();
    
    	//for testing purposes only
    	system("cat test.in");
    	
    	return 0;
    }
    I can't remember exactly how to mark it as the end of the file ATM though...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    125
    The way I'd do it:
    use an ifstream to load myfile.txt
    Measure the length of the file.
    Read the contents of the file to a character array.
    Close the ifstream.
    Use an ofstream on myfile.txt with ios::trunc
    Write the first (filesize-1) bytes to myfile.txt

    I'm not sure if this'd actually work because I haven't really try ios::trunc myself, but I think I'll give it a go right now. I'll post the source file if I get it to work.
    Typing stuff in Code::Blocks 8.02, compiling stuff with MinGW 3.4.5.

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    100
    Thanks mate, id appreciate it if you could do that :-)

    Major_Small, thanks for your help too

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    here ya go:
    Code:
    //fstream for file I/O
    #include <fstream>
    
    int main()
    {
    	//to hold the file
    	char*buf;
    	//to hold the size of the file
    	int size;
    	
    	//open the file for binary reading
    	std::fstream file("test.in",std::ios::in|std::ios::binary);
    	//seek to the end of the file
    	file.seekg(0,std::ios::end);
    	//get the size of the file
    	size=file.tellg();
    	//allocate enough memory to hold the entire file
    	buf=new char[size];
    	//seek to the beginning of the file
    	file.seekg(0,std::ios::beg);
    	//read the file
    	file.read(buf,sizeof(char)*size);
    	//close the file
    	file.close();
    	//clear any flags
    	file.clear();
    	
    	/* 
    	 * TRUNCATING THE BUFFER BEFORE OUTPUT
    	 * 
    	 * we're taking two off here because my file ends with a newlin, which
    	 * counts as a char.  your file may not end with a newline, depending
    	 * on how it was generated.  if you come out with 'clou' if your file,
    	 * you want to change the 2 below to 1.  If your file does end with a
    	 * newline, you may want to consider replacing the '\0' with '\n' and 
    	 * adding a new line: buf[size-1]='\0';
    	 */
    	buf[size-2]='\0';
    	
    	//open the file for text output (truncate)
    	file.open("test.in",std::ios::out|std::ios::trunc);
    	//write the buffer
    	file<<buf;
    	//close the file
    	file.close();
    
    	//free up the memory again
    	delete[]buf;
    
    	return 0;
    }
    Last edited by major_small; 08-16-2005 at 06:21 AM. Reason: fixed code and comments, fixed memory leak in code.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    125
    Here's what I came up with:

    Code:
    #include <iostream>
    #include <fstream>
    #include <stdlib.h>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
    	ifstream read_file("myfile.txt", ios::in | ios::binary);
    	if (read_file.fail())
    	{
    		cout << "Couldn't find the file!" << endl;
    		system("PAUSE");	
    		return 0;
    	}
    	read_file.seekg( 0, ios::end );
    	int size = read_file.tellg();
    	read_file.seekg( 0, ios::beg );
    
    	if (size==0)
    	{
    		cout << "File is already 0 bytes!" << endl;
    		system("PAUSE");	
    		return 0;
    	}
    
    	char *contents=new char[size+1];
    	read_file.read(contents, size); // this'll get the whole file, endlines or not
    	contents[size]=0;
    	read_file.close(); // might be a good idea since we're reopening it
    	
    	ofstream write_file("myfile.txt", ios::trunc | ios::binary);
    	write_file.write(contents, size - 1);
    
    	delete [] contents;
    	
    	cout << "File cut!" << endl;
      
    	system("PAUSE");	
    	return 0;
    }
    I used binary file access because that makes it easier to leave the rest of the file untouched and it works with all files. (not just text files) Note that it's probably possible to get a garbled textfile with this program if you run it on a text file with enters in it, although this should fix itself if you run it a few more times.
    I got the seekg thing to get the filesize from NeHe. seekg just sets or changes the position in the file, and tellg returns the current position in the file.

    edit: by the way, heh I always forget leaving out the "using namespace" line and replacing ios:: with std::ios. My IDE puts that line in there for all console applications and I can never remember what is part of the std namespace.
    Last edited by Boksha; 08-16-2005 at 06:29 AM.
    Typing stuff in Code::Blocks 8.02, compiling stuff with MinGW 3.4.5.

  9. #9
    Registered User
    Join Date
    Oct 2004
    Posts
    100
    Thanks both, i've got it working :-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 09:47 AM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

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