1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Streams and Basic File I/O Problems

Discussion in 'C++' started by golf_girl32, Sep 30, 2010.

  1. golf_girl32

    golf_girl32 New Member

    Joined:
    Sep 28, 2010
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    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);
    }
    }
    
     
  2. golf_girl32

    golf_girl32 New Member

    Joined:
    Sep 28, 2010
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    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 ";
    
    }
    
     
  3. jimblumberg

    jimblumberg New Member

    Joined:
    May 30, 2010
    Messages:
    120
    Likes Received:
    29
    Trophy Points:
    0
    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
     

Share This Page