for loop?

L

LT

Good day!
I'm trying to read in values from input file (in_file)
and add them to another file (out_file)
Repeat the same process but opening a different file...
I'm trying to use a combination of for loop (2 run, does not work) and while
loops.

Any help on this will be great...
This is probably a very, very simple question and I apologize for that.

I declared the files as follow

ifstream in_file;
ofstream out_file;
out_file.open("outputNumbers.txt", ios::eek:ut);

//I'll like to use a loop for
this...

for (int i=1; i<5; i++)
{
out_file <<"\n **running run number "<<i;
out_file <<"\n ----------------------";
if (i==1)
{
in_file.open("in_data", ios::in); //Opening my first input
file

while (!in_file.eof()) // while the file is not empty
{
in_file >> number;
out_file<<"\nNumber from the in_data file is "<<number;
}
in_file.close(); // closing the file, so that I can open a
different file
}

if (i==2) // on my second run, this should happen, BUT it isn't
{
in_file.open("in_bonus", ios::in);

while (!in_file.eof())
{
out_file<<"\nOpening in_Bonus file";
in_file >>number;
out_file <<"\nNumber from the in_bonus file is "<<number;
}
in_file.close();
}
}
}


-LT
 
R

Rolf Magnus

LT said:
Good day!
I'm trying to read in values from input file (in_file)
and add them to another file (out_file)
Repeat the same process but opening a different file...
I'm trying to use a combination of for loop (2 run, does not work) and
while loops.

Define "does not work".
Any help on this will be great...
This is probably a very, very simple question and I apologize for
that.

I declared the files as follow

Please always post _full_ compilable code, i.e. a minimal, but complete
program (i.e. including a main function and all header includes). There
may be things wrong in parts you snipped out, or others may want to try
your code to find out what's wrong with it.
ifstream in_file;
ofstream out_file;
out_file.open("outputNumbers.txt", ios::eek:ut);

ofstreams are by default opended as ios::eek:ut. That's why they are
ofstreams ;-)
//I'll like to use a loop
for
this...

for (int i=1; i<5; i++)
{
out_file <<"\n **running run number "<<i;
out_file <<"\n ----------------------";
if (i==1)
{
in_file.open("in_data", ios::in); //Opening my first
input
file

while (!in_file.eof()) // while the file is not
empty

eof() doesn't mean empty. It means the end of the file was reached. And
you're not using it correctly, since eof() will only return true
_after_ you tried to read beyond the end of the file. So your loop will
do one iteration too much. Also, you don't catch any real errors. Your
program will go to an endless loop if there is any error reading the
file. Replace your loop with:

while (in_file >> number)
out_file<<"\nNumber from the in_data file is"<<number;

if (!in_file.eof())
sd::cerr << "Error reading file\n";
{
in_file >> number;
out_file<<"\nNumber from the in_data file is
"<<number;
}
in_file.close(); // closing the file, so that I can open
a
different file

Closing a file will _not_ reset its status bits. I.e. if you want to
reuse the stream for another file, your stream will still be in fail
state. Try adding an:

in_file.clear();
}

if (i==2) // on my second run, this should happen, BUT it
isn't
{
in_file.open("in_bonus", ios::in);

while (!in_file.eof())
{
out_file<<"\nOpening in_Bonus file";
in_file >>number;
out_file <<"\nNumber from the in_bonus file is
"<<number;
}
in_file.close();
}
}
}

Btw, your outer loop isn't very useful and makes the code harder to
understand.
 
C

Charles LaCour

LT said:
Good day!
I'm trying to read in values from input file (in_file)
and add them to another file (out_file)
Repeat the same process but opening a different file...
I'm trying to use a combination of for loop (2 run, does not work) and while
loops.

Any help on this will be great...
This is probably a very, very simple question and I apologize for that.

I declared the files as follow

ifstream in_file;
ofstream out_file;
out_file.open("outputNumbers.txt", ios::eek:ut);

//I'll like to use a loop for
this...

for (int i=1; i<5; i++)
{
out_file <<"\n **running run number "<<i;
out_file <<"\n ----------------------";
if (i==1)
{
in_file.open("in_data", ios::in); //Opening my first input
file

while (!in_file.eof()) // while the file is not empty
{
in_file >> number;
out_file<<"\nNumber from the in_data file is "<<number;
}
in_file.close(); // closing the file, so that I can open a
different file
}

if (i==2) // on my second run, this should happen, BUT it isn't
{
in_file.open("in_bonus", ios::in);

while (!in_file.eof())
{
out_file<<"\nOpening in_Bonus file";
in_file >>number;
out_file <<"\nNumber from the in_bonus file is "<<number;
}
in_file.close();
}
}
}


-LT

#include <iostream.h>
#include <fstream.h>

int main()
{
char *inDataFile[] = { "inData01.txt", "inData02.txt", "inData03.txt",
"inData04.txt", "inData05.txt", "inData06.txt" };
ifstream src; // Source file object
ofstream dst; // Destination file object
int i, number;
int fileCount = sizeof (inDataFile) / sizeof (inDataFile[ 0 ]);

// Open the output file. If it won't open, return with failure.
dst.open( "outData.txt", ios::eek:ut );
if ( !dst )
return -1;

for ( i = 0; i < fileCount; ++i )
{
src.open( inDataFile[ i ], ios::in | ios::nocreate );
if ( src.fail() )
{
continue;
}

// Read each number from the source file
// and send it to the destination file.
src >> number;
while ( !src.fail() )
{
dst << number << endl;
src >> number;
}

src.close();
}

dst.close();

return 0;
}
 
L

LT

Thank you all for all the Great info,
next time I'll post the entire code, sorry about that, I figured less is
better...

Also, I now know what I was doing wrong, a combination of things including
not knowing the " in_file.clear(); "
to clear the stream...

Thank you all,
LT
Charles LaCour said:
LT said:
Good day!
I'm trying to read in values from input file (in_file)
and add them to another file (out_file)
Repeat the same process but opening a different file...
I'm trying to use a combination of for loop (2 run, does not work) and while
loops.

Any help on this will be great...
This is probably a very, very simple question and I apologize for that.

I declared the files as follow

ifstream in_file;
ofstream out_file;
out_file.open("outputNumbers.txt", ios::eek:ut);

//I'll like to use a loop for
this...

for (int i=1; i<5; i++)
{
out_file <<"\n **running run number "<<i;
out_file <<"\n ----------------------";
if (i==1)
{
in_file.open("in_data", ios::in); //Opening my first input
file

while (!in_file.eof()) // while the file is not empty
{
in_file >> number;
out_file<<"\nNumber from the in_data file is "<<number;
}
in_file.close(); // closing the file, so that I can open a
different file
}

if (i==2) // on my second run, this should happen, BUT it isn't
{
in_file.open("in_bonus", ios::in);

while (!in_file.eof())
{
out_file<<"\nOpening in_Bonus file";
in_file >>number;
out_file <<"\nNumber from the in_bonus file is "<<number;
}
in_file.close();
}
}
}


-LT

#include <iostream.h>
#include <fstream.h>

int main()
{
char *inDataFile[] = { "inData01.txt", "inData02.txt", "inData03.txt",
"inData04.txt", "inData05.txt", "inData06.txt" };
ifstream src; // Source file object
ofstream dst; // Destination file object
int i, number;
int fileCount = sizeof (inDataFile) / sizeof (inDataFile[ 0 ]);

// Open the output file. If it won't open, return with failure.
dst.open( "outData.txt", ios::eek:ut );
if ( !dst )
return -1;

for ( i = 0; i < fileCount; ++i )
{
src.open( inDataFile[ i ], ios::in | ios::nocreate );
if ( src.fail() )
{
continue;
}

// Read each number from the source file
// and send it to the destination file.
src >> number;
while ( !src.fail() )
{
dst << number << endl;
src >> number;
}

src.close();
}

dst.close();

return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,155
Messages
2,570,871
Members
47,401
Latest member
CliffGrime

Latest Threads

Top