Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Streams and Basic File I/O Problems (http://www.go4expert.com/forums/streams-basic-file-i-o-t23452/)

golf_girl32 1Oct2010 03:46

Streams and Basic File I/O Problems
 
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);
}
}


golf_girl32 1Oct2010 07:03

Re: Streams and Basic File I/O Problems
 
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 ";

}


jimblumberg 1Oct2010 09:12

Re: Streams and Basic File I/O Problems
 
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


All times are GMT +5.5. The time now is 07:22.