473,385 Members | 2,014 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,385 software developers and data experts.

How to create a txt file, read, write in c++ open for more information

Hello briefly explaning my question how do i create a text file read and write in it but every time i reopen the program i must not overrie the data in the text file please help me i a new to c++ and computer programming.

Thanks in advance
Jan 25 '15 #1

✓ answered by weaknessforcats

To append to the existing file:

Expand|Select|Wrap|Line Numbers
  1. myfile.open ("example.txt", std::ofstream::app);
  2.  
All this does is position the file to the end instead of the beginning (which is the default).

The are several open mode flags. I would look them up and get familiar with what is possible.

If you run the program several times, example.txt should contain all the data all the runs of the program.

6 1482
weaknessforcats
9,208 Expert Mod 8TB
When you open the file, specify that you want to append. This will stop the overwrite of the current file contents.
Jan 25 '15 #2
How do i do that exactly
Jan 25 '15 #3
weaknessforcats
9,208 Expert Mod 8TB
Maybe you could post how you are opening the file.
Jan 25 '15 #4
something like this

and so every time i run this it should and i open the text file i should see this sentence repeatedly "Writing this to a file."

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. int main () {
  6.   ofstream myfile;
  7.   myfile.open ("example.txt");
  8.   myfile << "Writing this to a file.\n";
  9.   myfile.close();
  10.   return 0;
  11. }
Jan 25 '15 #5
weaknessforcats
9,208 Expert Mod 8TB
To append to the existing file:

Expand|Select|Wrap|Line Numbers
  1. myfile.open ("example.txt", std::ofstream::app);
  2.  
All this does is position the file to the end instead of the beginning (which is the default).

The are several open mode flags. I would look them up and get familiar with what is possible.

If you run the program several times, example.txt should contain all the data all the runs of the program.
Jan 25 '15 #6
Thanks you have solved my problem THANKS !!!
Jan 25 '15 #7

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

Similar topics

3
by: Simon Wigzell | last post by:
I recently wrote a program with MS Visual Studio C++, sent it off to the client where it didn't run, after some probing I discover they are on a Mac! My program is a MSF interface that is really...
1
by: David Arden Stevensonn | last post by:
Say I have an XML file on my website that gets read alot (by a c# aspx page) but written to occasionally (also by the same c# aspx page) . Its a simple caching situation based on time. Example: If...
2
by: Smoothy | last post by:
Hi all, Can someone help me with the following issue? I want to read/write from/to a particular position (line/character) of a text file. How can I do this? In other words, how can I tell to...
11
by: Marcia Hon | last post by:
Hi, I am trying to create a file for writing: FILE * List_of_Files; char * List = "List_of_Files; if((List_of_Files = fopen(List, "w")) == NULL) { fprintf(stderr, "Cannot write file");
1
by: Magix | last post by:
Hi, I have these string data: str_data1, str_data2, str_data3, which capture some value after a routine process A. Then I would like to write (append) these 3 string values into a text file each...
1
by: Nadja Schmitt | last post by:
Hi! Is it possible to read/write a *.resx-file? The problem: I'd like to add some more columns to my resource-file, than read the data out of the file and write the data in this file. I...
1
by: gce | last post by:
Hi, As a newbie (old vb6 programmer) I like to know how to read/write/lock a file using asp.net ? Best regards, Gert
10
by: lorenzogordon | last post by:
Hi there, I'd greatly appreciate any insights into the following problem: I've got PHP running fine on IIS (OS: Server 2003, SP1; IIS: 6.0; PHP: 4.3.11). In PHP, the user uploads a file,...
3
by: Podi | last post by:
As far as I know, there is pyXLWriter for writing and xlrd for reading. Is there such thing so that one can open an Excel file into memory and read/update any sheet/cell on the fly. One analogy to...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.