473,471 Members | 1,953 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to write to a text file without deleting what's already in there

3 New Member
Hello all,

I'm quite new to c++. I made a small program that will help me analyse wind tunnel data. But now I want this program to write to a text file. This works just fine, except it discards whatever is in the textfile at that time.

I open the file, and then I go to the end, and write the stuff. I came up with the following code:

ofstream logfile;
logfile.open ("HWlog.txt");
logfile.seekp (0, ios::end);
logfile << " " << endl;
logfile << " Average speed: \t \t \t" << average_speed << "\t [m/s ]" << endl;
logfile << " Turbulence level: \t \t \t" << turb_level << " %" << endl;
logfile << " ----------------------------------------------------------- " << endl;
logfile.close();

I would also like to add time and date to these lines. I searched but I couldn't find a way of doing this. I sure there is some convenient predefined function to generate a string that contains time and date, but what is it?

Thanks in advance for your help!

Mac
Jan 28 '08 #1
4 3306
ragumaniraj
2 New Member
Hello all,

I'm quite new to c++. I made a small program that will help me analyse wind tunnel data. But now I want this program to write to a text file. This works just fine, except it discards whatever is in the textfile at that time.

I open the file, and then I go to the end, and write the stuff. I came up with the following code:

ofstream logfile;
logfile.open ("HWlog.txt");
logfile.seekp (0, ios::end);
logfile << " " << endl;
logfile << " Average speed: \t \t \t" << average_speed << "\t [m/s ]" << endl;
logfile << " Turbulence level: \t \t \t" << turb_level << " %" << endl;
logfile << " ----------------------------------------------------------- " << endl;
logfile.close();

I would also like to add time and date to these lines. I searched but I couldn't find a way of doing this. I sure there is some convenient predefined function to generate a string that contains time and date, but what is it?

Thanks in advance for your help!

Mac

hi!
you must open the file in edit mode.
and then move the filepointer to the end of the file.
and then write the new contents whatever you want.
Jan 28 '08 #2
mvvdsteen
3 New Member
Thanks! It works magnificently.

Meanwhile, I solved the time and date issue (came across it sort of by accident). I'm affraid, however, that one more issue just popped up.

I'm using the code below to read in the data entries. However, for some dark reason the line in the data file is read twice. In this case it isn't a big deal because I have > 6000 points, so having the last point twice doesn't really change the outcome of the statistical analysis, but I'd still like to get it right. Should I first go over the file so see how many lines it has, and then make it a for loop (from 0 to amount of lines -1?) This will probably add unnecessary operations to my file, or not?

while(!file_to_read.eof()) {
float time,volt,speed;
file_to_read >> time >> volt >> speed;
// Store each value into the appropriate array
time_array.push_back(time);
volt_array.push_back(volt);
speed_array.push_back(speed);
sum_speed=sum_speed+speed;
}
Jan 28 '08 #3
mvvdsteen
3 New Member
By now I also solved the problem with the double last entry. I did this by implementing a while loop which only saves the values if the time entry is greater than the previous one. It's probably not the most efficient way but it works.

This leaves me with just one problem, which I really can't solve. That's the problem of the user defined file name. I.e., I'd like to be able to provide a name for the data file which must be read. The program accepts the string without complaining (and a char too), but it won't accept is as a file name.

So what I have now is:
ifstream file_to_read("HW.txt");

And what I'd like is something like
ifstream file_to_read(user_defined);
And be able to input the user_defined via the prompt.

Anybody any idea?

All help is greatly appreciated!

Mac
Jan 28 '08 #4
toefraz
19 New Member
Just split the the declaration in two:

Expand|Select|Wrap|Line Numbers
  1. ifstream file_to_open;
  2. file_to_open.open(c_str);
Jan 29 '08 #5

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

Similar topics

33
by: Nick Evans | last post by:
Hello there, I have been on and off learning to code (with python being the second language I have worked on after a bit of BASIC). What I really want to know is, if you are going to actually...
4
by: Asipu | last post by:
The problem is quite difficult and i'm starting to think that it's impossible to accomplish.. Suppose you have C.exe. When you run C.exe, it must change his 0x4e's byte into 0xff.. Of course,...
1
by: javajavalink | last post by:
I am relatively new to java and have already tried many ways to write to text files with Java. Is there any way to write to a text file without overwriting what is already there??
16
by: nephish | last post by:
Hey there, kinda newbie question here. i know how to read the lines of a txt file. i know how to write a txt file. but how do i overwrite a line value with another value ? i mean, how do go...
13
by: Bob Darlington | last post by:
I have a repair and backup database routine which runs when a user closes down my application. It works fine in my development machine, but breaks on a client's at the following line: If...
5
by: Sumana | last post by:
Hi All, We developed our project on VC++.Net console application to create image of disk and to write the image We are having problem with reading and writing the sector beyond 6GB Disk or...
3
by: sonu | last post by:
I am getting some data 'strData' from database through reader object. The data 'strData' is sent to a doc using FilesystemObject line by line in doc file located on a specific location. 1.Code...
63
by: Bill Cunningham | last post by:
I don't think I can do this without some help or hints. Here is the code I have. #include <stdio.h> #include <stdlib.h> double input(double input) { int count=0,div=0; double...
12
by: feel | last post by:
Hi,All I am sure it's an old question. But I just find a interesting design about this: Polymorphism without virtual function in a C++ class. My solution is for some special case, trust me, very...
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
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,...
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,...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.