Hello, and thank you for taking the time to read this post. Here is my problem: every time i try to open up a text file, in a program, and try to add something to the end of it on a new line, then it erases the entire file and only puts in the single line. Thank you very much in advance for your help and dedication to these forums. --Parker Woods --MonsterHackz DEV Team 6 4070
Hi,
Then it means u are opening the file in write mode.
Open the file in append mode if u want add any info o open in read mode if u only want to read info.
Thanks
Raghuram
Hi,
Then it means u are opening the file in write mode.
Open the file in append mode if u want add any info o open in read mode if u only want to read info.
Thanks
Raghuram
That was very......helpful
could I please have some code with that?
just, use this example: -
ofstream filesave ("C:\\my text file.txt");
-
if (filesave.is_open())
-
{
-
filesave << testToBeAdded;
-
filesave.close();
-
}
-
else
-
{
-
cout << "The File Was Unable To Open For An Unknown Reason." << endl;
-
cout << " Program Terminated" << endl;
-
system("PAUSE");
-
return 0;
-
}
-
thanks
That was very......helpful
could I please have some code with that?
just, use this example: -
ofstream filesave ("C:\\my text file.txt");
-
if (filesave.is_open())
-
{
-
filesave << testToBeAdded;
-
filesave.close();
-
}
-
else
-
{
-
cout << "The File Was Unable To Open For An Unknown Reason." << endl;
-
cout << " Program Terminated" << endl;
-
system("PAUSE");
-
return 0;
-
}
-
thanks
Well, I just figureed out how to append text files. But, now the question is a little bit harder: How do I add the line of text that I want to the end of the file? Every time I run the program, it just rewirtes the first line. How do I fix this? --Parker Woods --MonsterHackz DEV Team
Well, I just figureed out how to append text files. But, now the question is a little bit harder: How do I add the line of text that I want to the end of the file? Every time I run the program, it just rewirtes the first line. How do I fix this? --Parker Woods --MonsterHackz DEV Team
You could use the fseek() function. Look at this: http://www.cplusplus.com/reference/c...dio/fseek.html
Well, I just figureed out how to append text files. But, now the question is a little bit harder: How do I add the line of text that I want to the end of the file? Every time I run the program, it just rewirtes the first line. How do I fix this? --Parker Woods --MonsterHackz DEV Team
Well, i have done it, not in a very practical way but anyway.
I used fopen("Filename","mode"); where mode was the mode it would be opened, i used "r+" because it let you open without erasing and you could also edit.
To get to the end, i used some functions, although i know it is quite impractical but at least it does work. - FILE *myfile;
-
if((myfile=fopen("File.txt","r+"))==NULL)printf("File could not be opened");
-
else {
-
while(!feof(myfile)){//while it is not the end
-
fgetc(myfile);
-
}
-
}
Then you will get to the end.
You can also write '\n' so that you write in the following line.
For that i use fwrite().
The syntax is fwrite("Text",int length,int times,file);
you would use it in this way:
fwrite("Hello World",11,1,myfile);
The second parameter is not exactly the size, it's the length in bytes, but since a character is a byte, then you can say it's the size in number of words.
If you want to write a value stored in an array write & before the name of the string
Well, i have done it, not in a very practical way but anyway.
I used fopen("Filename","mode"); where mode was the mode it would be opened, i used "r+" because it let you open without erasing and you could also edit.
To get to the end, i used some functions, although i know it is quite impractical but at least it does work. - FILE *myfile;
-
if((myfile=fopen("File.txt","r+"))==NULL)printf("File could not be opened");
-
else {
-
while(!feof(myfile)){//while it is not the end
-
fgetc(myfile);
-
}
-
}
Then you will get to the end.
You can also write '\n' so that you write in the following line.
For that i use fwrite().
The syntax is fwrite("Text",int length,int times,file);
you would use it in this way:
fwrite("Hello World",11,1,myfile);
The second parameter is not exactly the size, it's the length in bytes, but since a character is a byte, then you can say it's the size in number of words.
If you want to write a value stored in an array write & before the name of the string
These are all great solutions, but I do not want to use fopen() or fseek(). I only can use fstream. Sorry, and thank you for you help.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Anonymous |
last post by:
I need to create an application that will do fairly simple text manipulation
on 20,000 files in text format (html files). The files exist both on my
Windows machine and on a FreeBSD server. I...
|
by: ross |
last post by:
I want to do some tricky text file manipulation on many files, but have
only a little programming knowledge.
What are the ideal languages for the following examples?
1. Starting from a certain...
|
by: Michael Thomas |
last post by:
Hi everyone
Do any of you know if this is possible in Access either by using the import
tool or by writing a module in VB:
I have a database containing information for a retail chain which,...
|
by: UJ |
last post by:
I've got a text box that I'm going to be doing some manipulation on
(selecting text, moving stuff around, that kind of thing) but I don't
want/need the user to see it while it's going on. I'd like...
|
by: Marc Miller |
last post by:
Hello everyone,
First I'll address this as a textfile manipulation question and if there is
no answer
there, then I'll need to ask it as a vb ADO question.
I have a text file that I'm reading...
|
by: Joe1986 |
last post by:
Hi there, im trying to create a python program that can read a text file line by line and search for specified words/text/strings and remove them from the text file. Then finally save the modified...
|
by: vitaminz |
last post by:
Hi it's me again, I've pretty much finished a program I'm making, which does the following:
The program is for a garage, and it does the following functions:
Adds a new car
Display cars
Amend...
|
by: blangela |
last post by:
Bjarne Stroustrup has a new text coming out called "Programming:
Principles and Practice Using C++" (ISBN: 0321543726), due to be
published in December of this year.
Some of the features of this...
|
by: pakerly |
last post by:
How would i do this, convert a test file to excel?
Lets say my text file has fields like this:
NUMBER NAME ADDRESS PHONE
11002 Test1 ...
|
by: nick777 |
last post by:
Hope the Community can bear with me as I muddle with the vocabulary since I am not really sure if I am going about this the correct way.
My question is as follows:
If I had some sample data in...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
| |