trouble with file i/o program.

This is a discussion on trouble with file i/o program. within the C++ Programming forums, part of the General Programming Boards category; I'm having trouble getting the file I/O working in this little program. can anyone help? thanks for anything in advance! ...

  1. #1
    Novice Programmer Pyroteh's Avatar
    Join Date
    Jan 2005
    Location
    Alaska
    Posts
    82

    trouble with file i/o program.

    I'm having trouble getting the file I/O working in this little program. can anyone help?
    thanks for anything in advance!

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
            string line;
            int counter = 0;
            ifstream myFile(argv[1]);
            if(myFile.is_open())
            {
                    while(! myFile.eof())
                    {
                            getline(cin, line, '\n');
                            cout << line << endl;
                            counter++;
                    }
                    myFile.close();
                    cout << "there were " << counter << " lines in the file \n";
            }
            else cout << "unable to open file \n";
            return 0;
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    getline(cin, line, '\n'); do you want to read from the file or from the cin?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    'cin' is input from the keyboard.

    Also, the basic rule for reading from a file in a loop is that the read statement itself should be the while loop conditional, e.g.
    Code:
    while( inputFile>>input)
    {
    	...
    	...
    }
    inputFile>>input calls a function that reads data into the variable on the right, in this case 'input', and then the function returns what's on the left side, which in this case is inputFile, and that converts the while conditional to:

    while(inputFile)

    If any errors occur that will prevent you from reading data from the file, the inputFile object will evaluate to false in the while conditional. End of file(eof) is considered an error, so the while loop will terminate correctly when you reach the end of the data in a file. However, there are also other errors that can occur while reading from a file. If one of those errors occurs, then a loop such as:
    Code:
    while(!inputFile.eof())
    {
    	inputFile>>input
    }
    will try to keep reading in data because it hasn't encountered eof yet--but the error will prevent the read statement inside the loop from reading any data. As a result, the loop will loop indefinitely: it will not be able to read in any data because of the error and therefore it will never reach eof.
    Last edited by 7stud; 01-09-2007 at 12:36 AM.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > while(! myFile.eof())
    Also, you should not do this anyway - see the FAQ
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Novice Programmer Pyroteh's Avatar
    Join Date
    Jan 2005
    Location
    Alaska
    Posts
    82
    So instead of:
    Code:
                    while(! myFile.eof())
                    {
                            getline(cin, line, '\n');
                            cout << line << endl;
                            counter++;
                    }
    I should use something like:
    Code:
                    while(myFile >> line) // while(! myFile.eof())
                    {
                            getline(myFile, line, '\n');
                            cout << line << endl;
                            counter++;
                    }
    ?
    Or because of the while loop do I need to use getline?

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    You use getline or operator>>, but not both (unless you wanted to read two times). Since you are using getline, you would place getline into the while control like this (the '\n' is optional so I removed it):
    Code:
    while (getline(myFile, line))
    with only the output and counter increment inside the loop.

  7. #7
    Novice Programmer Pyroteh's Avatar
    Join Date
    Jan 2005
    Location
    Alaska
    Posts
    82
    Ok! sweet thanks everything works perfectly now

    thanks for the help!

    Completed program:
    Code:
    /*
    * Alvin David Morris
    * 1/09/07
    * count.cpp
    */
    
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
            string line;
            int counter = 0;
            ifstream myFile(argv[1]);
            if(myFile.is_open())
            {
                    while(getline(myFile, line))
                    {
                            cout << line << endl;
                            counter++;
                    }
                    myFile.close();
                    cout << "there were " << counter << " lines in the file \n";
            }
            else cout << "unable to open file \n";
            return 0;
    }
    usage:
    Code:
    $g++ -o count count.cpp
    $count <filename>
    Last edited by Pyroteh; 01-09-2007 at 06:18 PM.

  8. #8
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    905
    Keep in mind why it works, though -- the reading in getline() is done before (or rather while) the conditional is being evaluated; which is why, in the event of EOF, the code within the loop is not involved, even though the data is still read.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 03:49 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-30-2004, 11:21 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21