writing to a file with C++ (solved, I am helpless without programmers!!)

Keego

Diamond Member
Aug 15, 2000
6,223
2
81
Say I have an input file xab.txt

I have 3 variables from that file, var1, var2, var3

I need to rename xab.txt to "var1,var2 - var3.txt"

What code would I need to rename the existing code to the format I want?

Thanks!
 
Jun 18, 2000
11,215
781
126
You can do this a couple ways:

// you'll need to include the file stream header file.
#include <fstream.h>

// main
int main {

// you'll need to declare your input stream.
ifstream lf_FromFile;

// declare 3 separate output streams (one for each file)
ofstream lf_ToFile1, lf_ToFile2, lf_ToFile3;

/* THE REST IS JUST PSEUDOCODE, I'M NOT DOING ALL YOUR WORK FOR YOU */

<open your first file>

<depending on how the data is stored in the file, start a loop that will break at the end of the file "while lf_FromFile.eof() {}"
<using the stream extraction operator (">>") read in from the file>
<store the data from the previous step into your local variables>
<close your input file>

<open your 3 new file>
<using the stream insertion operator ("<<") copy any text into the 3 files>
<close your 3 new files>

/* END PSEUDOCODE */

} // end main
 
Jun 18, 2000
11,215
781
126
Or if you want to be cute about it, you can send MS-DOS commands directly to the system shell.

system("copy c:\xab.txt c:\var1.txt");
system("copy c:\xab.txt c:\var2.txt");
system("copy c:\xab.txt c:\var3.txt");

But I'm guessing this is an exercize in using file stream I/O, so I would recommend going with my first suggestion.

edit: If you need more help, feel free to ask. You won't learn anything that way. I'll be happy to point you in the right direction, but don't expect me/us to write out all the code for you.
 

Keego

Diamond Member
Aug 15, 2000
6,223
2
81
I just need one file with the filename called

"var1, var2 - var3"
 

Keego

Diamond Member
Aug 15, 2000
6,223
2
81
This is for work, not school, and my job does not entail programming, I'm trying to make my job easier.

Here's the code so far, a programmer has been helping me but he's busy.

ifstream bumps;



bumps.open(ReadFile);
double ZMean=0;
double ZSum = 0;



CQLBump bumpdata;
static char arr[10];


//for(int i = 0;i<720;i++)
//{
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumpdata.Row = atoi(arr);
bumps.getline(arr,10,'\t');
bumpdata.Col = atoi(arr);
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumpdata.ZHeight = atoi(arr);
ZSum += bumpdata.ZHeight;


for(int i=0;i<719; i++)
{
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumpdata.ZHeight = atoi(arr);
ZSum += bumpdata.ZHeight;
bumps.getline(arr,100,'\n');

}

ZMean = ZSum/720;


so the variables are ZMean, Row, Col
So I want the output to look like:

Row,Col - ZMean.txt
 
Jun 18, 2000
11,215
781
126
Originally posted by: Kyguy
This is for work, not school, and my job does not entail programming, I'm trying to make my job easier.
LMAO! My apologies. :D

Do you need any actual data in this file? Or is just a blank file with the specified name?
 

Keego

Diamond Member
Aug 15, 2000
6,223
2
81
the file looks like this:
txt.jpg

Can I just use this? :

rename('bumps',"'Row','Col' - 'ZMean'")
 
Jun 18, 2000
11,215
781
126
Try it? Is there actually a rename() function, or did you just make that up? :)

You can try this, but I don't know how well it works in every MS OS.

system("copy " + ReadFile + " " + FilePath + "Row,Col - ZMean.txt");

The program will pass the argument to the system shell, and then execute it like a DOS command.

Edit: Forgot the "FilePath" variable. FilePath should be the entire path up to, but not including, the original file name. You will need to extract it out of the ReadFile variable.
 

Keego

Diamond Member
Aug 15, 2000
6,223
2
81
ah HA I got it! I'm so happy! I was like crying to my boss I get so emotional when programming :D

here's what I ended up with:

ifstream bumps;
ofstream results;



bumps.open(ReadFile);

results.open(SaveResults);
int ZHeight=0;
double ZSum = 0;
int Row;
int Col;


CQLBump bumpdata[772];
static char arr[10];

bumps.getline(arr,230,'\n');

for(int i = 0;i<772;i++)
{
ZSum = 0;

bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
Row = atoi(arr);
bumps.getline(arr,10,'\t');
Col = atoi(arr);
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
ZHeight = atoi(arr);
ZSum += ZHeight;
bumps.getline(arr,230,'\n');

for(int i=0;i<719; i++)
{
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
bumps.getline(arr,10,'\t');
ZHeight = atoi(arr);
ZSum += ZHeight;
bumps.getline(arr,230,'\n');
}

bumpdata[I ].Row = Row;
bumpdata[I ].Col = Col;
bumpdata[I ].ZMean = ZSum/720;


//write out results
results << bumpdata[I ].Row << ", " << bumpdata[I ].Col << ", " << bumpdata[I ].ZMean <<"\n";


}

results.close();
bumps.close();