Thread: check if txt file

  1. #1
    Registered User Boomba's Avatar
    Join Date
    Jun 2003
    Posts
    89

    check if txt file

    I think there is a simple answer to this...how would i check if a file is a text file? after reading in my root directory using an if statement I wanna check for the .txt files in it and then load them into a combobox..but I dont know how to check whether it is a .txt file or not.

    thanx in advance,
    Boomba,

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>but I dont know how to check whether it is a .txt file or not.<<
    Are you asking how to tell if the filename ends .txt or the file itself contains only text (printable characters)?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Code:
    #include <iostream>
    #include <cstring>
    
    int main( void ) {
        char path[] = "C:\\test.txt";
        char *p = std::strrchr( path, '.' );
        
        if( p == NULL )
            std::cout<<"No '.' found."<<std::endl;
        else {
            if( std::strcmp( p, ".txt" ) == 0 )
                std::cout<<"Yep"<<std::endl;
            else
                std::cout<<"Nope"<<std::endl;
        }
    }
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Registered User Boomba's Avatar
    Join Date
    Jun 2003
    Posts
    89
    i'm trying to test whether is has .txt as an extension how would I do that in my if statement?..and the name.txt is recieved from a combobox so the "name" can be anything.

    thanx in advance,
    Boomba

  5. #5
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176
    I'm not that good at C++ but if there's a function you can use to find out the length of an array you could convert the filename into an array and check the last four letters (.txt),
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    An example has already been given. There are of course other ways, try and find something that suits you best.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. spell check in C using a dictionary file
    By goron350 in forum C Programming
    Replies: 10
    Last Post: 11-25-2004, 06:44 PM
  5. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM