Thread: opening a file from hard drive

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    58

    opening a file from hard drive

    let me revise this. i need help closing the file, not opening it. when i try to run this i get an error message saying it expected a constructor or destructor before the "." token on the line that has inputFile.close(). what does that mean? how do i properly close this file?
    Code:
    #include <iomanip>
    #include <iostream>
    #include <fstream>
    #include <ctype.h>
    
    using namespace std;
    bool myIsAlpha( char ch );
    bool myIsDigit( char ch );
    bool myIsUpper( char ch );
    int countPunct = 0,
    countAlpha = 0,
    countUpper = 0,
    countLower = 0,
    countChar = 0,
    countDigit = 0,
    countSpace = 0,
    countWhite = 0;
    char ch; 
    
    int main()
    {
    ifstream inputFile;
    
    //Open the input file. If it fails to open, display an error message and
    //end the program
    
    inputFile.open( "input.txt" );
    if( inputFile.fail() )
    {
    cout << "input.txt failed to open";
    exit( -1 );
    }
    
    if(ispunct(ch))
    {
    countPunct++;
    }
    
    else if(myIsAlpha(ch))
    {
    countAlpha++;
    }
    
    else if (myIsDigit(ch))
    {
    countDigit++;
    }
    else if (myIsUpper(ch))
    {
    countUpper++;
    }
    else if (islower(ch))
    {
    countLower++;
    }
    else if (isspace(ch))
    {
    countWhite++;
    }
    }
    
    //Close the input file
    
    inputFile.close();
    
    return 0;
    }
    Last edited by joeman; 03-04-2010 at 03:01 PM. Reason: confused

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Is the file in a different directory than the EXE? If it's not in whatever is recognized as the current working directory then you'll need to include the path as well.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Which OS and compiler?

    General: If you're using an IDE, make sure you know what the "current directory" is for the executable is. Sometimes it's the project root directory, sometimes it's the executable directory.

    Windows: if you've got explorer "hide known file extensions" turned on, the file may be called input.txt.txt and you won't even realise it.
    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.

  4. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    58
    I'm using windows 7 and using quincy 2005 to compile the program. i'm not sure if you're responding to my original question or the revised question. i got the file to open properly, and now i can't get it to close properly, which is what i changed my question to. thanks for the help so far though.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    58
    The .txt file is in the same folder as the .exe and .cpp files.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    If it was formatted better, you'd see that there is an extra bracket '}' just before the close.

    Code:
    #include <iomanip>
    #include <iostream>
    #include <fstream>
    #include <ctype.h>
    
    using namespace std;
    bool myIsAlpha( char ch );
    bool myIsDigit( char ch );
    bool myIsUpper( char ch );
    int countPunct = 0,
    countAlpha = 0,
    countUpper = 0,
    countLower = 0,
    countChar = 0,
    countDigit = 0,
    countSpace = 0,
    countWhite = 0;
    char ch; 
    
    int main()
    {
        ifstream inputFile;
    
        //Open the input file. If it fails to open, display an error message and
        //end the program
    
        inputFile.open( "input.txt" );
        if( inputFile.fail() )
        {
            cout << "input.txt failed to open";
            exit( -1 );
        }
    
        if(ispunct(ch))
        {
            countPunct++;
        }
    
        else if(myIsAlpha(ch))
        {
            countAlpha++;
        }
    
        else if (myIsDigit(ch))
        {
            countDigit++;
        }
        else if (myIsUpper(ch))
        {
            countUpper++;
        }
        else if (islower(ch))
        {
            countLower++;
        }
        else if (isspace(ch))
        {
            countWhite++;
        }
    }
    
    //Close the input file
    
    inputFile.close();
    
    return 0;
    }
    Also:
    Code:
    #include <ctype.h>
    prefer <cctype> instead of <ctype.h>
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    58
    awesome, thanks a lot for the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  2. Totally confused on assigment using linked lists
    By Uchihanokonoha in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2008, 04:49 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM