|
-
April 30th, 2010, 02:34 PM
#1
Little help with fstream outFile
Hey all,
Have a slight problem I can't seem to figure out. First, I am a beginner in C++ and this is an assignment due 4/2/2010. Forgive me if the solution is simple...it's just evading me at the moment.
OK, my problem is in the FSTREAM portion of my code (near the bottom). I am trying to read 5 numbers from an inFile. I got this working, however, the 2nd part is writing the stored numbers from the inFile into a SECOND file....which I named "outFile_Reverse.open" is my dilemma.
It does not read the correct numbers. I suspect it's reading numbers from the portion of the code titled "ARRAY OF INT" because I pasted the "for loop" into the FSTREAM logic. It displays "5 4 3 2 1" So, how can I make inFile_1 read the stored numbers_1 text file and display them in reversed order?
Here is my code:
Code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
int number[5];
int input;
int counter;
string words[6];
const string end = "end of array";
cout <<"\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-ARRAY OF INT-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" <<endl;
cout <<"\n\nThis portion of the program will ask you to type in 5 numbers." <<endl;
cout <<"It will then display them back to you in reverse order." <<endl;
cout <<"\nPlease enter 5 positive numbers with a space between each: ";
for (counter = 0; counter < 5; counter++)
{
cin >> number[counter];
}
cout <<endl;
cout <<"\nThe numbers in reverse order are ===> ";
for (counter = 4; counter >= 0; counter--)
{
cout <<number[counter] << " ";
}
cout <<"\n\n\n\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-ARRAY OF STRING=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" <<endl;
cout <<"\n\nThis portion of the program will ask you to type in 5 words." <<endl;
cout <<"It will then use a do while loop and display the 1st and 3rd letter of each" <<endl;
cout <<"word using the substring function." <<endl;
cout <<"\nPlease enter 5 words:\n"<<endl;
for (counter = 0; counter < 5; counter++)
{
cin >> words[counter];
}
counter = 0;
cout <<"\n\nThe 1st and 3rd letter of each word you entered are:\n" <<endl;
do
{
if(words[counter].size() >= 3)
{
cout << words[counter][0] << words[counter][2] << "\n\n";
}
else
{
cout <<"\n\n\tUH-OH ===> One or more words you entered have less than 3 characters." <<endl;
cout <<"\tOnly words containing 3 OR MORE characters will display a result.)" <<endl;
}
counter++;
}
while (counter < 5);
cout <<"\n\n\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-FSTREAM=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" <<endl;
cout <<"\nThis portion of the program will:\n\n" <<endl;
cout <<"1. Read 5 numbers FROM a text file" <<endl;
cout <<"2. Write the numbers in reverse order to a SECOND file" <<endl;
cout <<"3. Read 5 words in from a THIRD file" <<endl;
cout <<"4. Display results of words to the CONSOLE\n\n" <<endl;
ifstream inFile_1; // File we are reading the numbers from
ifstream inFile_2; // File we are reading the words from
ofstream outFile_Reverse; // File to use for output
string numbers_1 ; // Name of number txt file
string words_1; // Name of words txt fileLi
int num1, num2, num3, num4, num5; // Declaring variables to read numbers from stored file
string w1, w2, w3, w4 ,w5; // Declaring variables to read words from stored file
inFile_1.open("numbers_1.txt"); // File stream variable to open numbers_1.txt file to read
inFile_2.open("words_1.txt"); // File stream varibale to open words_1.txt file to read
outFile_Reverse.open("outFile_Reverse.txt"); // File stream variable to display reversed numbers into this outFile.
/***********************************************************************************************
outData file to display words_1 is not needed; displaying directly to console from inFile_2.
***********************************************************************************************/
inFile_1 >> numbers_1;
outFile_Reverse << numbers_1;
for (counter = 4; counter >= 0; counter--)
{
cout <<number[counter] << " ";
}
// inFile_2 >> words;
// cout <<"The words stored in the words.txt file are: "<<words<<;
inFile_1.close(); // Closes numbers_1.txt file
inFile_2.close(); // Closes words_1.txt file
outFile_Reverse.close(); // Closes outfile
cin.get();
cin.get();
return 0;
}
Last edited by SoloXX; April 30th, 2010 at 02:36 PM.
Reason: code format
-
April 30th, 2010, 02:46 PM
#2
Re: Little help with fstream outFile
The only time I see you reading from any file is putting a single string into numbers_1, and then writing the same string back to outfile_Reverse. Actually, that doesn't entirely make sense, since in your comments it says that numbers_1 should store the *name* of the number input file, but you aren't using it that way.
-
April 30th, 2010, 03:13 PM
#3
Re: Little help with fstream outFile
Not sure I follow you Lindley. I thought I had to declare the name of the number_1 as a variable in order for it to recognize it as an identifier? Am I misinterpreting your meaning?
Also, as far as putting a single string of numbers into number_1, that is correct. I manually entered 5 random numbers into the numbers_1 file and expected the "for loop" to reverse these numbers and output them into the outFile_Reversed text file. That's not happening with the logic I have so far.
Any further clarification or ideas would be greatly appreciated on how to achieve this. I know it has to be simple because there's not much logic here but my mind is mush right now trying to figure this out.
Thanks Lindley for your quick response and help.
 Originally Posted by Lindley
The only time I see you reading from any file is putting a single string into numbers_1, and then writing the same string back to outfile_Reverse. Actually, that doesn't entirely make sense, since in your comments it says that numbers_1 should store the *name* of the number input file, but you aren't using it that way.
-
April 30th, 2010, 03:39 PM
#4
Re: Little help with fstream outFile
 Originally Posted by SoloXX
Not sure I follow you Lindley. I thought I had to declare the name of the number_1 as a variable in order for it to recognize it as an identifier? Am I misinterpreting your meaning?
Code:
string numbers_1 ; // Name of number txt file
inFile_1.open("numbers_1.txt");
Here you are declaring a variable intended to hold the name of the file, but assigning nothing to it. Then you assume that the name *is* "numbers_1.txt", a statement completely unrelated to the variable.
There's nothing actually wrong with that; it's just really strange. If you're going to hard-code the filename, then don't have a variable which you claim will hold the name. And if you're going to use the variable to hold the name, then use the variable.
Also, as far as putting a single string of numbers into number_1, that is correct. I manually entered 5 random numbers into the numbers_1 file and expected the "for loop" to reverse these numbers and output them into the outFile_Reversed text file. That's not happening with the logic I have so far.
You have for loops which are capable of reading to and writing from the numbers array.....from cin and to cout. You are not doing that in relation to ifstreams or ofstreams anywhere that I can see.
The only statements relating to file IO I see in that program are:
Code:
inFile_1 >> numbers_1;
outFile_Reverse << numbers_1;
which is simply going to copy the first few bytes from the input file (up until the first whitespace) over to the output.
-
April 30th, 2010, 03:44 PM
#5
Re: Little help with fstream outFile
Ok, somehow I've gotten it to only "see" the first number in the numbers_1 file but it repeats 5 times. It won't read anything past that. What am I doing wrong?
Code:
cout <<"\n\n\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-FSTREAM=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" <<endl;
cout <<"\nThis portion of the program will:\n\n" <<endl;
cout <<"1. Read 5 numbers FROM a text file" <<endl;
cout <<"2. Write the numbers in reverse order to a SECOND file" <<endl;
cout <<"3. Read 5 words in from a THIRD file" <<endl;
cout <<"4. Display results of words to the CONSOLE\n\n" <<endl;
ifstream inFile_1; // File we are reading the numbers from
ifstream inFile_2; // File we are reading the words from
ofstream outFile_Reverse; // File to use for output
string numbers_1 ; // Name of number txt file
string words_1; // Name of words txt fileLi
inFile_1.open("numbers_1.txt"); // File stream variable to open numbers_1.txt file to read
inFile_2.open("words_1.txt"); // File stream varibale to open words_1.txt file to read
outFile_Reverse.open("outFile_Reverse.txt"); // File stream variable to display reversed numbers into this outFile.
/***********************************************************************************************
outData file to display words_1 is not needed; displaying directly to console from inFile_2.
***********************************************************************************************/
inFile_1 >> numbers_1;
cout <<"The numbers stored in numbers_1 file are: "<<numbers_1 "\n";
cout <<"Please check the outFile Reverse.txt to verify the numbers have reversed.";
for (counter = 4; counter >= 0; counter--)
{
outFile_Reverse << numbers_1 << " ";
}
// inFile_2 >> words_1;
// cout <<"The words stored in the words.txt file are: "<<words_1<<;
inFile_1.close(); // Closes numbers_1.txt file
inFile_2.close(); // Closes words_1.txt file
outFile_Reverse.close(); // Closes outfile
cin.get();
cin.get();
return 0;
}
-
April 30th, 2010, 03:46 PM
#6
Re: Little help with fstream outFile
You already have code to do the correct logic further up the program. The only difference from what you've already written is that you're using inFile_1 as a source rather than cin, and outFile_Reverse as a destination rather than cout.
In fact, this duplication of functionality with slight variations makes this logic an excellent candidate for a function, taking the appropriate istream and ostream as parameters.
-
April 30th, 2010, 04:27 PM
#7
Re: Little help with fstream outFile
I don't mean to flame, but as lindley already said, it kind of boggles the mind that you got the entire program correct with cin and cout, and when you have to do EXACTLY THE SAME THING with file_in and file_out, you choke.
I'd help, but the solution is already in front of your eyes.
-
April 30th, 2010, 07:24 PM
#8
Re: Little help with fstream outFile
Code:
cout <<"\nThis portion of the program will:\n\n" <<endl;
cout <<"1. Read 5 numbers FROM a text file" <<endl;
cout <<"2. Write the numbers in reverse order to a SECOND file" <<endl;
cout <<"3. Read 5 words in from a THIRD file" <<endl;
cout <<"4. Display results of words to the CONSOLE\n\n" <<endl;
Am I able to use cin and cout to write TO files? It's just step 2 above that is evading me. Previously, we had a FSTREAM assignment and I use "inFile" and outFile" to serve my purpose without ever using cin or cout. That's why I'm a bit confused.
-
April 30th, 2010, 07:33 PM
#9
Re: Little help with fstream outFile
 Originally Posted by SoloXX
Code:
cout <<"\nThis portion of the program will:\n\n" <<endl;
cout <<"1. Read 5 numbers FROM a text file" <<endl;
cout <<"2. Write the numbers in reverse order to a SECOND file" <<endl;
cout <<"3. Read 5 words in from a THIRD file" <<endl;
cout <<"4. Display results of words to the CONSOLE\n\n" <<endl;
Am I able to use cin and cout to write TO files? It's just step 2 above that is evading me. Previously, we had a FSTREAM assignment and I use "inFile" and outFile" to serve my purpose without ever using cin or cout. That's why I'm a bit confused.
cin and cout are streams. inFile and outFile are filestreams, which means they can also be used as normal streams.
Once you have created inFile and outFile (which so do successfully), you can use inFile and outFile anywhere you used cin and cout, instead of cin and cout. For all intents and purposes, you will get EXACTLY the same behavior, except that rather than interfacing with your console, you will interface with your files.
Is that clearer? You don't actually use cin and cout to read/write to files, you just use streams that happen to have a different name. Remember, cin and cout are not special, they are just streams like any other. They just happen to be wired to your console.
-
April 30th, 2010, 07:33 PM
#10
Re: Little help with fstream outFile
 Originally Posted by SoloXX
Am I able to use cin and cout to write TO files?
No. However, cout is an ostream, and outFile_Reverse is an ostream. They have the same base type, so insofar as output syntax goes, they're interchangable.
Similarly, cin is an istream, and inFile_1 is an istream. Again, they have different concrete types---one takes input from the console, another from a file---but the same base class, so all input operations are identical between them on the syntax level.
What this means is that if you know how to do something using cin and cout, then you know how to do it with files. It's the exact same operation.
Last edited by Lindley; April 30th, 2010 at 07:41 PM.
-
April 30th, 2010, 10:51 PM
#11
Re: Little help with fstream outFile
I figured out why it was only placing the last number in the outFile_Reverse. After thinking about all of the advice I've gotten here, it appeared I was reading the input values into a single variable
(numbers_1) (as Lindley observed), so after the first for() loop completes, numbers_1 contains
only the last variable read in from the input file.
Then I was writing that variable to the output file 5 times (by counter = 5). To reverse the numbers, I needed to read them into separate variables in memory...something I did in the program already but as an array of int.
Something finally "clicked" in me and I made the numbers_1 named variable into an array to house the 5 numbers in memory.
So, the completed code (for step #2) is below: (don't know why the code tags won't maintain the comments as shown in my program. They look in disarray here)
Thanks all for your help and direction!
Code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
cout <<"\n\n\n=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-FSTREAM=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" <<endl;
cout <<"\nThis portion of the program will:\n\n" <<endl;
cout <<"1. Read 5 numbers FROM a text file" <<endl;
cout <<"2. Write the numbers in reverse order to a SECOND file" <<endl;
cout <<"3. Read 5 words in from a THIRD file" <<endl;
cout <<"4. Display results of words to the CONSOLE\n\n" <<endl;
ifstream inFile_1; // File we are reading the numbers from
ifstream inFile_2; // File we are reading the words from
ofstream outFile_Reverse; // File to use for reverse number output
string numbers_1[5]; // Numbers_1 array to house the 5 numbers in memory
string words_1; // Name of words txt file
int counter = 1;
inFile_1.open("numbers_1.txt"); // File stream variable to open numbers_1.txt file to read
inFile_2.open("words_1.txt"); // File stream varibale to open words_1.txt file to read
outFile_Reverse.open("outFile_Reverse.txt"); // File stream variable to display reversed numbers into this outFile.
/***********************************************************************************************
outData file to display words_1 is not needed; displaying directly to console from inFile_2.
***********************************************************************************************/
for (counter = 0; counter < 5; counter++)
{
inFile_1 >> numbers_1[counter];
}
outFile_Reverse <<"The numbers reversed are: ";
for (counter = 4 ; counter >= 0; counter--)
{
outFile_Reverse << numbers_1[counter] <<" ";
}
cout <<"\nPlease check the outFile_Reverse file to verify the numbers have been reversed.";
inFile_1.close(); // Closes numbers_1.txt file
inFile_2.close(); // Closes words_1.txt file
outFile_Reverse.close(); // Closes outfile
cin.get();
cin.get();
return 0;
}
Last edited by SoloXX; April 30th, 2010 at 10:53 PM.
Reason: Give thanks
-
April 30th, 2010, 11:20 PM
#12
Re: Little help with fstream outFile
That will work. Of course, you're treating them as strings rather than integers here. That isn't actually wrong, just unnecessarily different.
-
May 2nd, 2010, 11:02 AM
#13
Re: Little help with fstream outFile
Lindley and monarch,
Thanks all for your help. I got it working correctly by using a for() loop I used further up my program but just tweaked it a bit different. Having slept on it and thinking about what you all have said in your advice is just what I needed. Thanks so much for all your help and direction.
Here is how I solved it:
Code:
cout <<"\n\n=-=-=-=-=-=-=-=-=-=-=-=-=-FSTREAM (Words to Console)=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n" <<endl;
cout <<"This final portion will:\n\n" <<endl;
cout <<"3. Read 5 words in from a THIRD file" <<endl;
cout <<"4. Display results of words to the CONSOLE\n\n" <<endl;
cout << "The stored words in the THIRD file are:\n\n" <<endl;
for(int i = 0; inFile_2 >> words_1[i]; i++) //Loop with declared index for preparation to output to console
{
cout <<words_1[i] <<endl; //Output to console
}
inFile_1.close(); // Closes numbers_1.txt file
inFile_2.close(); // Closes words_1.txt file
outFile_Reverse.close(); // Closes outfile
cout <<"\n\n\nPress ENTER to exit the program....";
cin.get();
cin.get();
return EXIT_SUCCESS;
}
Last edited by SoloXX; May 2nd, 2010 at 11:03 AM.
Reason: code tags
-
May 2nd, 2010, 11:21 AM
#14
Re: Little help with fstream outFile
 Originally Posted by SoloXX
Lindley and monarch,
Thanks all for your help. I got it working correctly by using a for() loop I used further up my program but just tweaked it a bit different. Having slept on it and thinking about what you all have said in your advice is just what I needed. Thanks so much for all your help and direction.
Here is how I solved it:
Code:
cout <<"\n\n=-=-=-=-=-=-=-=-=-=-=-=-=-FSTREAM (Words to Console)=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n" <<endl;
cout <<"This final portion will:\n\n" <<endl;
cout <<"3. Read 5 words in from a THIRD file" <<endl;
cout <<"4. Display results of words to the CONSOLE\n\n" <<endl;
cout << "The stored words in the THIRD file are:\n\n" <<endl;
for(int i = 0; inFile_2 >> words_1[i]; i++) //Loop with declared index for preparation to output to console
{
cout <<words_1[i] <<endl; //Output to console
}
inFile_1.close(); // Closes numbers_1.txt file
inFile_2.close(); // Closes words_1.txt file
outFile_Reverse.close(); // Closes outfile
cout <<"\n\n\nPress ENTER to exit the program....";
cin.get();
cin.get();
return EXIT_SUCCESS;
}
That is actually pretty good and pretty clever. Well done.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|