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

Text File Manipulation

compman9902
105 100+
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
May 9 '07 #1
6 4070
gpraghuram
1,275 Expert 1GB
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
May 9 '07 #2
compman9902
105 100+
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:
Expand|Select|Wrap|Line Numbers
  1. ofstream filesave ("C:\\my text file.txt");
  2. if (filesave.is_open())
  3. {
  4. filesave << testToBeAdded;
  5. filesave.close();
  6. }
  7. else
  8. {
  9. cout << "The File Was Unable To Open For An Unknown Reason." << endl;
  10. cout << "                  Program Terminated" << endl;
  11. system("PAUSE");
  12. return 0;
  13. }
  14.  
thanks
May 9 '07 #3
compman9902
105 100+
That was very......helpful
could I please have some code with that?
just, use this example:
Expand|Select|Wrap|Line Numbers
  1. ofstream filesave ("C:\\my text file.txt");
  2. if (filesave.is_open())
  3. {
  4. filesave << testToBeAdded;
  5. filesave.close();
  6. }
  7. else
  8. {
  9. cout << "The File Was Unable To Open For An Unknown Reason." << endl;
  10. cout << "                  Program Terminated" << endl;
  11. system("PAUSE");
  12. return 0;
  13. }
  14.  
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
May 9 '07 #4
ilikepython
844 Expert 512MB
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
May 9 '07 #5
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.

Expand|Select|Wrap|Line Numbers
  1. FILE *myfile;
  2. if((myfile=fopen("File.txt","r+"))==NULL)printf("File could not be opened");
  3. else {
  4. while(!feof(myfile)){//while it is not the end
  5. fgetc(myfile);
  6. }
  7. }
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
May 9 '07 #6
compman9902
105 100+
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.

Expand|Select|Wrap|Line Numbers
  1. FILE *myfile;
  2. if((myfile=fopen("File.txt","r+"))==NULL)printf("File could not be opened");
  3. else {
  4. while(!feof(myfile)){//while it is not the end
  5. fgetc(myfile);
  6. }
  7. }
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.
May 10 '07 #7

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

Similar topics

3
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...
10
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...
2
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,...
2
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...
1
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...
5
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...
4
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...
1
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...
15
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 ...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
tracyyun
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...
0
agi2029
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,...
0
isladogs
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...

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.