473,386 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Regarding Text files

Hi,

I have a question. I know how to read text files using ifstream. Now I would like to append some data at the end of the file.

I read a text file as follows:

#include "stdafx.h"
#include<string>
#include<iostream>
#include<fstream>
using namespace std;

void main()
{
string line;
ifstream test_file;
test_file.open("test.txt", ios::in);
if(test_file.is_open())
{
while(!test_file.eof())
{
getline(test_file, line);
cout<<line<<endl;
}
}
}

Now I would like to append some more data to the end of the text file. The data are numbers(int).

How can I do that.

Please Help.

Thanks!!
Jul 8 '07 #1
3 1399
niskin
109 100+
You will need to include stdio.h to do this:

Expand|Select|Wrap|Line Numbers
  1. FILE *fp;
  2. fp=fopen("example.txt", "a");
  3. fprintf(fp, "This is text will be added to the end of the file");
  4. fclose(fp);
Jul 8 '07 #2
weaknessforcats
9,208 Expert Mod 8TB
FILE *fp;
fp=fopen("example.txt", "a");
fprintf(fp, "This is text will be added to the end of the file");
fclose(fp);
This is C.

In C++, just open your file to append:
Expand|Select|Wrap|Line Numbers
  1. ofstream test_file;
  2. test_file.open("test.txt", ios::app);
  3.  
You can also use the same stream for input and output:

Expand|Select|Wrap|Line Numbers
  1. fstream test_file;
  2. test_file.open("test.txt", ios_base::out |ios_base::app);  //output
  3. test_file.open("test.txt", ios_base::in |ios_base::beg);  //input
  4.  
Jul 8 '07 #3
Thnx for the help!!

It solved my problem.
Jul 9 '07 #4

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

Similar topics

8
by: LG | last post by:
Just have a question with regards to the clipboard, and how to read what other applications (Adobe InDesignCS) place in the clipboard. I am currently in the process of creating a booklet from a...
4
by: Azhar Bilgrami | last post by:
Dear Hi: Hope to see u in good health. " I want to migrate a database which is currently running Unix as Operating System and Oracle ver 6 as Database, it is also using oracle froms version 3...
14
by: MLH | last post by:
GHudson has working procedures posted at http://www.access-programmers.co.uk/forums/showthread.php?t=66320 They work. Relationships, however, and some minor default settings are not preserved....
0
by: Paul Hsieh | last post by:
"Paul D. Boyle" <boyle@laue.chem.ncsu.edu> wrote: > There was a recent thread in this group which talked about the > shortcomings of fgets(). I decided to try my hand at writing a > replacement...
1
by: Andrew Robert | last post by:
Hi everyone, Could someone help explain what I am doing wrong in this code block? This code block is an excerpt from a larger file that receives transmitted files via IBM WebSphere MQSeries...
42
by: mellyshum123 | last post by:
I need to read in a comma separated file, and for this I was going to use fgets. I was reading about it at http://www.cplusplus.com/ref/ and I noticed that the document said: "Reads characters...
4
by: noone | last post by:
Hi. I've got a fella working for me who is trying to convince me to change our coding standards to using separate .h and .cc files for definitions and implementations. I know this is a...
1
by: akalmand | last post by:
Hi there, I am writing a code to read some data from the text files. The number of text files is not fixed and could be more that 15. the length of each file is large... close to 100,000 on an...
9
by: ramsatishv | last post by:
Hi, If I include a ".h" file for multiple times, will it increase my program size?? Regards Ram.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.