By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 882 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

writing to a text file in a specific location

P: 1
I am trying to write a c++ program that reads through a text file and when i comes across a specific line it adds to that file. i have this code, but it doesnt write "meow" until the end of the text file.. does anyone know how i can get it to write meow on the line after "putwordhere"????????????

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main(){
string line;
string searchage;
searchage = "putwordhere";
ifstream Myfile ("random.txt");
while(line != searchage){
if (line == searchage);{
ofstream a_file ("random.txt", ios::app);
a_file << "\n";
a_file << "meow";
Mar 13 '07 #1
Share this Question
Share on Google+
1 Reply

Expert 2.5K+
P: 3,652
I don't think it's a good idea to open the same file for both reading and writing - it could severely mess things up.

Why not read the entire file into a string, look for the portion you want to write after, insert the "meow" (or whatever string needed), and then output the new string to the file?
Mar 13 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.