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?
 
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";
      }
}
0
 
businessesatozAuthor Commented:
Where does the numbers variable come from I don't see it declared anywhere in this if statement
0
 
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.
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
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


0
 
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


0
 
peprCommented:
Sorry, should be cout instead of count ;)
0
 
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.
0
 
businessesatozAuthor Commented:
i did not mean to close this..
0
 
businessesatozAuthor Commented:
thanks for the help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.