Using fstream with .txt files

Jul 30, 2014 at 3:33pm
Hi all,

I'm new to C++, and have been working on the following problem for 2 straight days, but can't seem to get anywhere.

I'd really appreciate any help or suggestions.

I have a .txt file that contains 3 columns of data, and looks a bit like this:

1.250E-4 0.9999719 2.39E-006
1.350E-4 0.9999665 3.13E-006
1.450E-4 0.9999608 4.02E-006
1.500E-4 0.9999578 4.52E-006
1.750E-4 0.9999415 7.78E-006
: : :
: : :

(Each entry is separated by a tab). I need to write a program that writes the following data to a new .txt file, for each row:

Column1 -> Same as Column 1 in the original file
Column2 -> Column2^2 - Column3^2
Column3 -> 2*Column2*Column3

So, the first line would be:

1.250E-4 0.9999438007839 0.00000477986568


I will then be using a separate program to plot the results. FYI, I've been using the fstream header.

Any help would be greatly appreciated.

Thanks,

Matt
Last edited on Jul 30, 2014 at 3:34pm
Jul 30, 2014 at 3:58pm
So what have you tried?

Jul 30, 2014 at 5:59pm
Basic use of txt files:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
ifstream name1;
ofstream name2;
name1.open("textfilename.txt");
name2.open("textfilename.txt",ofstream::app);

// read file
while(!name1.eof()){
    char read=name1.get();
    cout<<read;
}

// add something to the txt file
name2<<"hi"<<endl;

// exit
name1.close();
name2.close();

It will automatically save.
Last edited on Jul 30, 2014 at 6:00pm
Jul 31, 2014 at 9:31am
Thank you very much. That'll be enough to be getting on with.

Cheers,

Matt
Jul 31, 2014 at 5:02pm
Your welcome. You can close the topic now by going to the top and click
mark as solved
Topic archived. No new replies allowed.