C++ prob...

MrTux

Senior member
Nov 6, 2001
717
0
0
The file named my_reals.dat contains an unknown number of real numbers separated with whitespace. Writthe complete program to copy the positive reals to a file named my_pos.dat, and copy the negative reals to a file called my_neg.dat. Occurrences of zero should not be written to either file, but should be counted and the total number of zeros should be printed out at the end of the program.



I'd be very thankful if you could help me out by giving me a working example of how to do this one...thanks. I only really need the logic that takes the numbers, seperates them, and puts them into seperate files.
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
Psuedo Code:

Open input file
Open both output files

Loop until input file is EOF
Get line from input file
Loop while parsing the file using tokens with a white space seperator.
Convert each token to a number.
if number is > 0.0 Output it to Positive file
if number is < 0.0 Output it to Negative file
Increment count based on number type
End Token parsing loop for line read in
End Read loop (EOF)

Output statics to screen
Close input and output files.
 

MrTux

Senior member
Nov 6, 2001
717
0
0


<< Psuedo Code:

Open input file
Open both output files

Loop until input file is EOF
Get line from input file
Loop while parsing the file using tokens with a white space seperator.
Convert each token to a number.

if number is > 0.0 Output it to Positive file
if number is < 0.0 Output it to Negative file
Increment count based on number type
End Token parsing loop for line read in
End Read loop (EOF)

Output statics to screen
Close input and output files.
>>




Thanks...the two parts in bold I do not understand...I haven't worked with psuedo code hardly at all. Could you convert those two to code if possible?
 

CyrusTCH

Member
Feb 4, 2001
40
0
0
the while loop looks like this

while(!infile.eof())
{
infile >> value;
if (value > 0)
{
outfilePos << value << " ";
}
else if (value == 0)
{
counter++;
}
else
{
outfileNeg << value << " ";
}
}
 

ProviaFan

Lifer
Mar 17, 2001
14,993
1
0
I also have a question relating to this... how does one open and close a file in C++? Yes, I have been studying a tutorial or two, but they cover mostly theory of the language, not how to actually acomplish something. So if you could recommend a tutorial that is more practical I would appreciate it.
 

Armitage

Banned
Feb 23, 2001
8,086
0
0


<< The file named my_reals.dat contains an unknown number of real numbers separated with whitespace. Writthe complete program to copy the positive reals to a file named my_pos.dat, and copy the negative reals to a file called my_neg.dat. Occurrences of zero should not be written to either file, but should be counted and the total number of zeros should be printed out at the end of the program.



I'd be very thankful if you could help me out by giving me a working example of how to do this one...thanks. I only really need the logic that takes the numbers, seperates them, and puts them into seperate files.
>>



Jeez, what a loser... don't you have any pride? Try doing your own homework. I mean seriously ... "giving me a working example of how to do this one."

At least give it a try and ask specific questions if you get stuck. But asking for the whole solution is really lame. And this is a simple assignment.

 

Armitage

Banned
Feb 23, 2001
8,086
0
0


<< I also have a question relating to this... how does one open and close a file in C++? Yes, I have been studying a tutorial or two, but they cover mostly theory of the language, not how to actually acomplish something. So if you could recommend a tutorial that is more practical I would appreciate it. >>



To open a file in C++:

you need to include the fstream header file. Then you declare a file object. You can either pass the parameters to the constructor, or use the open method. Here is an example:

#include <fstream>

int main(void)
{
fstream infile, outfile;

infile.open("my_infile.dat", ios::in);
outfile.open("my_outfile.dat", ios::eek:ut);

if(!in_file || !outfile)
{
cout << "ERROR: Failed opening files.\n";
return -1;
}

//do file io stuff

infile.close();
outfile.close();

return 0;
}


(edit: Stupid Emoticons :()
 

CyrusTCH

Member
Feb 4, 2001
40
0
0
to do some file I/O you first have to :
#include <fstream.h>

then you can make two objects which are suppose to work like cin and cout:
ofstream outfile; // works like cout
ifstream infile; // works like cin

then you can use them to open files for input and output:
outfile.open("path/nameoffile.txt"); // open a file for output
infile.open("path/nameoffile.txt"); // open a file for input