how to read ifstream c++

Hello, how do i correct my code to read from the file that was created  ? ... i want to write a function to do this but i'm not sure how to..

#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;
int WriteInts (ofstream &write, int how_many);

int main(void)
{
ifstream in_stream;

ofstream out_stream;


int numbers;
char user_control;
cout <<"Do you wish to R)ead or W)rite ints?";
cin >>user_control;


if(user_control=='w')
{
	
	out_stream.open("nums.txt");
	int numbers;
	int how_many;
	cout <<"how many numbers would you like to type : ";
	cin >> how_many;
	WriteInts(out_stream,how_many);
	
}

else if(user_control=='r')
{
	in_stream.open("nums.txt");
	if(in_stream.fail())
	{
		cout <<"The file does not exist yet";
	}
	

}
else 
{
cout <<"un-regonized commend";
}

}//end of program

int WriteInts (ofstream &write,int how_many)
{
	int numbers;
	for(int i =0; i<how_many;++i)
	{
		
	cout<<"Please enter an Int: ";
	cin >> numbers;
	write << numbers;
	write << endl;
	

	}
	return numbers;
}

Open in new window

businessesatozAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HooKooDooKuCommented:
Is the problem that you don't know HOW to read the data, or you don't know what to do with it once it's read.

Here's some example code you can plug in for the 'r' case:
else if(user_control=='r')
{
      in_stream.open("nums.txt");
      if(in_stream.fail())
      {
            cout <<"The file does not exist yet";
      }

      int numbers;
      while( true )
      {
            in_stream >> numbers;
            if( in_stream.eof() )
                  break;
            cout << "Number " << numbers << "\n";
      }
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
businessesatozAuthor Commented:
Where does the numbers variable come from I don't see it declared anywhere in this if statement
HooKooDooKuCommented:
Actually, it's declared twice.

It's already declared in the original code on line 15.  Then I unnecessarily defined it again just before the "while( true )" statement.  Since you already had it defined at the higher level, I didn't need to redefine it again inside the "else if" block you already created.

Once you define a variable inside a 'block', the variable is valid for the entire block and any child blocks.
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

Hugh McCurdyCommented:
I suggest you visit this page   http://www.cplusplus.com/doc/tutorial/files/   and read the text files section.

I'll copy some code from that page to here.  Then I'll modify it to open your file.  I suggest you copy and paste this code to your computer.  Compile and run it.  Then figure out what the code does as best you can.  Come back here if you have questions.

Note: the C++ tutorial pages is very good.  Several of my classmates used them when I took C++.  

// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  ifstream myfile ("nums.txt");
  if (myfile.is_open())
  {
    while ( myfile.good() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();
  }

  else cout << "Unable to open file"; 

  return 0;
}

Open in new window


peprCommented:
I suggest to separate better the opening of the file, closing the file and reading the lines.  For reading the lines, probably the most easy way is to use the function getline() defined in the <string>, like this:

    while (getline(myfile, line))
        count << line << '\n';

Open in new window


peprCommented:
Sorry, should be cout instead of count ;)
businessesatozAuthor Commented:
I've requested that this question be closed as follows:

Accepted answer: 250 points for HooKooDooKu's comment http:/Q_27377793.html#36905906
Assisted answer: 0 points for businessesatoz's comment http:/Q_27377793.html#36906037
Assisted answer: 250 points for hmccurdy's comment http:/Q_27377793.html#36906257

for the following reason:

thanks for the help guys.
businessesatozAuthor Commented:
i did not mean to close this..
businessesatozAuthor Commented:
thanks for the help
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.