-
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,
-
>>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)?
-
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;
}
}
-
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
-
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),
-
An example has already been given. There are of course other ways, try and find something that suits you best.