Thread: Trying out working with Files. Small problem.

  1. #1
    Registered User
    Join Date
    Dec 2014
    Posts
    6

    Trying out working with Files. Small problem.

    I'm teaching myself how to work with Files with C++ and I ran into a small problem. I coded everything right, at least I think. When I run the program, it's not reading and outputting all of the contents in the file.

    I tried other methods(getline, etc) but it's still not working right.

    Code:
    #include <iostream> //Library included for input and output functions
    #include <fstream> //Library included for file stream functions
    #include <string> //Library included for strings
    
    using namespace std;
    
    int main()
    {
    
    
        /*Variables. 'int counter'= the incrementor for the for loop. 
          'const int' = the constant variable used to declare the amount of contents in file
          and tells the for loop what value to count up to and keep track of current position in file.
        */
        const int MAX_SWORDS= 5;
        int counter;
    
        string Swords;
    
        /*Declare ifstream function and inputFile object from function. 
          use '.open' to make retrieve the file from the HDD and open the file.
        */
        ifstream inputFile;
        inputFile.open("Swords.txt");
    
        /*For loop that counts through the file called and until it reaches the value declared in 'MAX_SWORDS' 
          the counter variable will increment 
        */
        for(counter= 1; counter <= MAX_SWORDS; counter++)
        {
            /* For loop places all the contents of the file one by one and deplays them*/
            inputFile >> Swords;
    
            getline(inputFile, Swords);
            cout << Swords<< endl;
        }
    
         
    
        //Close the file
        inputFile.close();
    
        //End program and wait for user input to close window
        cin.ignore().get();
        exit(0);
    }
    This the content of the file
    Code:
    Goddess Sword
    Master Sword
    Picori Blade
    Elemental White Sword
    Four Sword

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You have a couple of potential issues:

    1) always check the file handle to see if your open() operation worked. Sometimes it will fail because the file doesn't exist or something, and it will (rightfully) cause most loops to fail.

    2) your loop is actually reading more than it should. Notice in this pseudo code that we only read in one place.
    Code:
    // while getline can read inputFile AND counter < max swords:
    //    display sword
    //    bump counter up 1
    // end while
    ifstream inputFile("Swords.txt");
    if (!inputFile)
    {
        cerr << "Swords.txt couldn't be opened!\n";
        return 0;
    }
    int counter = 0;
    string sword;
    while (getline(inputFile, sword) && counter < MAX_SWORDS) 
    {
       cout << sword << endl;
       ++counter;
    }
    There are different ways to write the loop as long as it is equivalent to the pseudo code.

  3. #3
    Registered User
    Join Date
    Dec 2014
    Posts
    6
    Quote Originally Posted by whiteflags View Post
    You have a couple of potential issues:

    1) always check the file handle to see if your open() operation worked. Sometimes it will fail because the file doesn't exist or something, and it will (rightfully) cause most loops to fail.

    2) your loop is actually reading more than it should. Notice in this pseudo code that we only read in one place.
    Code:
    // while getline can read inputFile AND counter < max swords:
    //    display sword
    //    bump counter up 1
    // end while
    ifstream inputFile("Swords.txt");
    if (!inputFile)
    {
        cerr << "Swords.txt couldn't be opened!\n";
        return 0;
    }
    int counter = 0;
    string sword;
    while (getline(inputFile, sword) && counter < MAX_SWORDS) 
    {
       cout << sword << endl;
       ++counter;
    }
    There are different ways to write the loop as long as it is equivalent to the pseudo code.

    Bingo! It worked.

    But now I have another question. 2 actually.

    Why wouldn't the for loop work? I took from a reliable textbook that I use to teach myself almost exactly save for the different name of the file and some other keywords and variables.

    Also, is there a way to include a function to require the loop to require user input in order to continue incrementing? In the past I'd use 'system("pause")' but I learned that that function wasn't the most efficient. Would cin.ignore().get() still work?

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    1) It would work; just take the while loop I gave you and add in all the for parts.

    2) Yes, cin.get(); should work fine here. Something like this may work as well:
    Code:
    #include <iostream>
    void mypause()
    {
       std::streamsize offset = cin.rdbuf()->in_avail();
       if (offset > 0) {
           std::cin.ignore(offset);
       }
       std::cout << "Press the enter key to continue . . . " << std::endl;
       std::cin.get();
    }
    Reason I'm unsure is that I heard Standard compliant implementations of in_avail() can return 0 always, and I don't really know how common that is, but try.
    Last edited by whiteflags; 07-04-2016 at 07:38 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to split long programe in small files
    By umeshjaviya in forum C Programming
    Replies: 11
    Last Post: 04-15-2008, 02:45 AM
  2. Small exercise not working: trees
    By Wezel in forum C Programming
    Replies: 1
    Last Post: 03-17-2006, 11:20 AM
  3. working with files
    By kristy in forum C Programming
    Replies: 2
    Last Post: 11-23-2003, 04:49 AM
  4. Large exe files for small amounts of code problem
    By rainmanddw in forum C++ Programming
    Replies: 4
    Last Post: 11-03-2003, 05:52 PM
  5. A small problem with a small program
    By Wetling in forum C Programming
    Replies: 7
    Last Post: 03-25-2002, 09:45 PM

Tags for this Thread