Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++
I want to update a .txt file w/ an insetion of text in the 1st line.
 
My method is
1. ask the user to cin "a" if s/he wants to insert things
2. ask the user to input the text to be inserted as "line_insert"
3. fout the text in new_text.txt
4. copy the data in source.txt to new_text.txt
5. copy the data in new_text.txt to source.txt so that upon next running of programme, the old data won't be overwrite
 
My problem is that
1. I can only insert text for once then the programme closes automatically despite that I typed system("pause")
2. The data are not copying at all...
 
attached is the code.
Thanks lots.
#include <time.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
using namespace std;
 
int main()
{  char key;
int size;
char* buffer;
char line_insert;
 
    ofstream fout;
    ifstream fin;
 
fin.open ("source.txt");
fout.open ("new_text.txt");
    while(1){
 
           
             cin>>key;
             if (key=='a')
             {cin>>line_insert;
  
               fout << line_insert;
               
                 
                 key=='0';
                 
                 
                             fin.seekg(0,ifstream::end); // get size of file

                             size=fin.tellg();
                             fin.seekg(0);
 
                             buffer = new char [size];// allocate memory for file content

 
                             fin.read (buffer,size);// read content of infile

 
                             fout.write (buffer,size);// write to outfile

                            delete[] buffer;// release dynamically-allocated memory

                       
                             fout.close();
                             fin.close();
                             
                             
                             fin.open ("new_text.txt");
                             fout.open ("source.txt");
                 fin.seekg(0,ifstream::end); // get size of file

                             size=fin.tellg();
                             fin.seekg(0);
 
                             buffer = new char [size];// allocate memory for file content

 
                             fin.read (buffer,size);// read content of infile

 
                             fout.write (buffer,size);// write to outfile

                            delete[] buffer;// release dynamically-allocated memory
                       if (key =='b')
                      { break;}
                       }
                       fout.close();
system("PAUSE");
  return 0;
 

}
[Edit]Code block added[/Edit]
Posted 17-Dec-12 7:02am
Edited 17-Dec-12 7:04am
ProgramFOX143.4K
v2
Comments
Richard MacCutchan at 17-Dec-12 12:57pm
   
You have some braces missing, please format your code properly and check where the end of your while block actually occurs.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Assuming you must use 'streaming', I would have done this a bit differently.
Read the new first line and write to a file.
Append the old file to this new first file, not necessarily trying to read entire file into memory, either.
Delete the old source file.
Rename the new file as the old file.
 

Or, if you wanted to get really fancy, you could keep the source file but 'extend' its length the size of the new line of text. Now copy bytes from existing file FORWARD into existing file, leaving room at front, where you now write the new line. But maybe that goes against this whole 'streaming' concept. Like that might not work through a pipe or TCP connection or something else. But seriosuly, if I were just working with a file always going to be on a hard drive, I would blow off the 'streaming' and deal with the file as a binary and do what I mentioned here.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

First, for reading one character at a time, streaming from cin isn't useful since streaming requires a press of return every time to complete the input. It would be much easier to use istream::getline()!
 
Second, if you wish to append text to an existing file, you have to open it for appending; see fstream::open(). You omitted the mode, so you're replacing (overwriting) the file every time.
 
Third, IO is slow: instead of writing the stuff you wish to insert to file after every bit of text, it would be better to assemble the entire text in memory before even opening the output file. What you're doing is like an editor that saves the entire file after every single word you type. That is not very sensible for multiple reasons, most importantly you needlessly copy the text that's already in there over and over again.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web04 | 2.8.150305.1 | Last Updated 21 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100