Results 1 to 5 of 5

Thread: Need to put the text to the end of my text file in c++

  1. #1
    Join Date
    Nov 2009
    Posts
    50

    Need to put the text to the end of my text file in c++

    Hello friends,
    I am new to this forum. I am getting problem in following code. I want to put the text to the end of my text file. But whenever I run this code It overwrites all my text. I don't know what is the problem in my code. Please help me.

    Code:
    void adds1()
    {
        string text;
        cout << "the story is completed " << endl;
        cin >> texts;
    
        ofstream file ("story.txt", ios::ate);
        file << "testing ";
    }
    Last edited by Luz; 08-02-2010 at 08:19 PM.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Need to put the text to the end of my text file in c++

    Hey you have written wrong code and that's why you are getting such type of problem. In this case you have to write following program to do this. just try to understand it. using following program you are able to take input from the user and you can easily insert this input at the end of text file.



    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main () {
      string lines;
      ifstream myfile ("story.txt");
      if (myfiles.is_open())
      {
        while (! myfiles.eof() )
        {
          getline(myfiles,lines);
          cout << line << endl;
        }
        myfiles.close();
      }
    
      else cout << "Unable to open file"; 
    
      return 0;
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: Need to put the text to the end of my text file in c++

    You have to use ios::in in your program to perform this operation. You also have to use eof function to add text to end of file. I have written small code for you just try to understand it.

    Code:
    void add1()
    {
        string text;
        cout << " The story is completed " << endl;
        cin >> text;
    
        ofstream file ("story.txt", ios::in |  ios::ate);
    
        file << text << endl;

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Need to put the text to the end of my text file in c++

    It is very simple to add text to the end of my text file in c++. You have to use iostream , fstream and strstream class to do this. I have written follwing program for you which are able to write text at the end of text file. It is very simple program. You have to just put text into buffer and then add this text at the end of file using eof function.



    Code:
    #include <iostream>
    #include <fstream>
    #include <strstream>
    
    using namespace std;
    
    int main()
    {
    	char buffer1[2356];
    	char buffer2[2356];
    	istrstream ostr1(buffer1, 2356);
    	istrstream ostr2(buffer2, 2356);
    	int values1[200];
    	int values2[200];
    	int x=0;
    
    	ifstream fin("story.txt");
    
    	fin.getline(buffer1,2356);
    	fin.getline(buffer2,2356);
    
    	while (ostra1 >> values1[x])
    	{
    		ostra2 >> values2[x++];
    	}
    
    	for (int k=0;k<c;x++)
    	{
    		cout << values1[k] << ":" << values2[k] << endl;
    	}
    
    	return 0;
    }

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: Need to put the text to the end of my text file in c++

    You have to use string , iostream , fstream class to put the text to the end of my text file in c++. In the following program I have use ios::in function to take input from the user. After this I have use test_files.eof() to add text to end of the file.



    Code:
    #include "stdafx.h"
    #include<string>
    #include<iostream>
    #include<fstream>
    using namespace std;
    
    void main()
    {
      string line;
      ifstream test_files;
      test_file.open("story.txt", ios::in);
      if(test_files.is_open())
      {
         while(!test_files.eof())
         {
            getline(test_files, line);
            cout<<line<<endl;
         }
      }
    }

Similar Threads

  1. Replies: 5
    Last Post: 28-02-2011, 08:23 PM
  2. How to convert image text into a text file
    By Wadee in forum Windows Software
    Replies: 5
    Last Post: 02-12-2010, 06:33 PM
  3. BlackBerry Torch TEXT message icon not showing text messages
    By rOSAMOND in forum Portable Devices
    Replies: 8
    Last Post: 17-10-2010, 01:04 AM
  4. How to recover lost text from text area in Google Chrome?
    By Corwinn in forum Technology & Internet
    Replies: 5
    Last Post: 10-03-2010, 12:56 PM
  5. Reading text string from text file (PHP)
    By Moderate in forum Software Development
    Replies: 3
    Last Post: 16-01-2009, 03:27 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,279,223.55609 seconds with 16 queries