Streams and Basic File I/O Problems

golf_girl32's Avatar
Go4Expert Member
I'm trying to insert a file and test to see if it opened successfully but it doesn't seem to be working. It is telling me that I didn't declare "cout" which is confusing me. Thanks

Code:
#include <fstream>

int main()
{
    using namespace std;
    ifstream fin;
    ofstream fout;

    fin.open("Numbers.txt");
    fout.open("Numbers_Results.txt");

if(fin.fail())
{
cout<<"Input file open failed!!";
exit(1);
}

if(fout.fail())
{

cout<<"Input file open failed!!";
exit(1);
}
}
0
golf_girl32's Avatar
Go4Expert Member
I fixed the problem that I had been having but now none of the information from the file is coming up on the program. Here is what I have so far. Any suggestions?

Code:
#include <fstream>
#include <iostream>
#include <cstdlib>

int main()
{
    using namespace std;
    ifstream fin;
    ofstream fout;

    int target;
    cout<<"Enter the number to search for: ";
    cin>> target;

    fin.open("Numbers.txt");
    if(fin.fail())
    {
        cout<<"Input file open failed!!";
        exit(1);
    }

    fout.open("Numbers_Results.txt");
if(fout.fail())
{

cout<<"Input file open failed!!";
exit(1);

}

int count_1, count_2;
count_1 = 0;
count_2 = 0;

int next;
while(fin>>next);
count_1++;

fout<<"The number ";

}
0
jimblumberg's Avatar
Ambitious contributor
Code:
int count_1, count_2;
count_1 = 0;
count_2 = 0;

int next;
while(fin>>next);
count_1++;

fout<<"The number ";
So where in this code are you using anything obtained by reading the file. The only thing you are outputting is "The number"

Jim