Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,622 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,022 people online right now. Registration is fast and FREE... Join Now!




How to operate the TXT file?

 
Reply to this topicStart new topic

How to operate the TXT file?

crammer008
post 29 Mar, 2006 - 01:26 AM
Post #1


New D.I.C Head

*
Joined: 9 Feb, 2006
Posts: 14


My Contributions


Dear all,

I have the following TXT file:
    aa;
    bb;
    cc;
Now I want to insert the following line between "aa" and "bb", How can I realize it?
    insert this line between aa and bb;
Thanks a lot!

Crammer008

This post has been edited by Dark_Nexus: 29 Mar, 2006 - 09:13 AM
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 29 Mar, 2006 - 03:57 AM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,176



Thanked 33 times

Dream Kudos: 25
My Contributions


Read the file into an array of strings, then rewrite to the file inserting the desired line.

Alternately, you can read the entire file into one string, use the find method to locate the position you want to insert at (the 'aa'), then use the insert() method to put the desired line into the string.

There is a description of the find and insert methods here:
http://www.msoe.edu/eecs/ce/courseinfo/stl/string.htm
User is offlineProfile CardPM

Go to the top of the page

crammer008
post 29 Mar, 2006 - 05:41 AM
Post #3


New D.I.C Head

*
Joined: 9 Feb, 2006
Posts: 14


My Contributions


Dear Amadeus,

It's very kind of you to help me so much:)

Thank you very much!

Crammer008
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 29 Mar, 2006 - 07:29 AM
Post #4


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,176



Thanked 33 times

Dream Kudos: 25
My Contributions


No problem, friend...that's what we are here for. Please let us know if you need anything to help you, such as code samples, explanations, etc...

Have a great day.
User is offlineProfile CardPM

Go to the top of the page

crammer008
post 29 Mar, 2006 - 07:06 PM
Post #5


New D.I.C Head

*
Joined: 9 Feb, 2006
Posts: 14


My Contributions


Dear Amadeus,

Under your help, I have constructed the program successfully. Thanks again for your and other kiind man's great help. The following code has already been tested. It can work normally.
I paste the code here for other guys who will meet the same question.

CODE

// String search and insert
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;


int main(int argc, char *argv[])
{
    string filename=argv[1];
    filename=filename+".cpp";
    //
    
    //Read the file
    ifstream CPPfile (filename.c_str());
    if (CPPfile.is_open())
    {
 int size,begin,end;
 begin = CPPfile.tellg();
 CPPfile.seekg (0, ios::end);
 end = CPPfile.tellg();
 size = end-begin;
 //cout<<"size: "<<size<<endl;

 char *memblock;
 string filecontent;
 memblock=new char[size];
 CPPfile.seekg (0, ios::beg);
 CPPfile.read (memblock, size);
 CPPfile.close();
 filecontent=memblock;
 delete [] memblock;
 //cout<<memblock<<endl;
 
 string::size_type pos = filecontent.find("return 0;",0);
 if (pos == string::npos) cout << "Not found" << endl;
 string insertCode="cout<<endl;\ncout<<\"Clock ticks: \"<<clock()<<\"  Seconds: \"<<clock()/CLOCKS_PER_SEC << endl;\n";
 filecontent.insert (pos,insertCode);
 
 // Create a CPP file
 ofstream myfile;
 myfile.open (filename.c_str());
 myfile <<filecontent;
 myfile.close();
 cout << "Timing code has been inserted successfully!\n\n";

    }
    else cout << "Unable to open CPP file.";
    
    return 0;
}


This post has been edited by Dark_Nexus: 29 Mar, 2006 - 10:47 PM
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 03:21AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month