?
Solved

Writing to a file

Posted on 2005-05-11
1
Medium Priority
?
256 Views
Last Modified: 2010-04-01
Hey everyone, I have a file containing numerical values ie:

12
45
19
65

I'm messing around with input and output files. Basically I have my program reading in the file with the above numbers in, and I wish ouput another file stating by "Y" or "N" If the values are above 50.

Here is my code:

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

int main()
{
 
   int num;
   ofstream outfile;
   ifstream infile;    
   infile.open("numbers.txt");
   outfile.open("outputs.txt");

   if (infile.fail())
    {
      cout <<"Error opening file, program closing "<< endl;
      exit(1);
    }
     

while ( infile >> num )
   {
       cout  <<  num << endl;
   }  

   cout <<"Checking numbers"<< endl;
  while ( infile >> num )
   {
     
        if (num < 50)
      outfile << "N"  << "\t" ;  
        else
        outfile << "Y" << "\t";
   }
   
   infile.close();
   outfile.close();


   return 0;
}

Although the program compiles and runs ok, it doesn't output anything to the output file. Can anyone help?

Helix
0
Comment
Question by:Helix
1 Comment
 
LVL 37

Accepted Solution

by:
Harisha M G earned 240 total points
ID: 13981105
Hi Helix,

Remove this part of the code...

 while ( infile >> num )
    {
        cout  <<  num << endl;
    }  



Bye
---
Harish
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question