By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,657 Members | 994 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,657 IT Pros & Developers. It's quick & easy.

for loop?

P: n/a
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::out);

//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
Jul 22 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
LT wrote:
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::out);
ofstreams are by default opended as ios::out. 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.

Jul 22 '05 #2

P: n/a

"LT" <Tr************@mYpRiVaCy.com> wrote in message
news:Nr********************@comcast.com...
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::out);

//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::out );
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;
}
Jul 22 '05 #3

P: n/a
Charles LaCour wrote:
#include <iostream.h>
#include <fstream.h>


#include <iostream>
#include <fstream>
using namespace std;

--
Unforgiven

"Most people make generalisations"
Freek de Jonge
Jul 22 '05 #4

P: n/a
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" <Yo***********@phantom.net> wrote in message
news:toBCb.1997$i55.1317@fed1read06...

"LT" <Tr************@mYpRiVaCy.com> wrote in message
news:Nr********************@comcast.com...
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::out);

//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::out );
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;
}

Jul 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.