Check if a file extension exists in a directory

I am working on some code to talk with a printer. I am communicating with the printer through a tcp socket and am sending and receiving commands. The part I am stuck on is when the printer does a print job, it will create a '.prn' file in a specified directory of which I am to always acknowledge when a file appears. My question is, how can I check to see if that specific file extension exists? I have tried using the access() function to check if there is an accessible file:

1
2
3
4
5
6
if ((::access("/home/Desktop/printFiles/*.prn", F_OK)) != -1) {
     std::cout << "File Exists!" << std::endl;
}
else {
     std::cout << "File Does Not Exist!" << std::endl;
}


But the ::access() function checks for the file name and not the extension. I used the '*' to try to represent any possible file, but that seems to not be working as well. I know there is a way to get around all this by writing new code, but it is not desirable to write new code instead of using predefined functions if at all possible...trying not to re-invent the wheel. I have done research on this, and from what I have seen, people keep getting off topic and start suggesting string::find and other string functions. I feel like there is a function similar to ::access() that should do the trick.
Also, I understand a lot of people like to go directly to using boost, but I am staying away from that for now.
Sorry, but there is no simple, single function solution.

If you don't want to use boost, then could have a look at opendir(), readdir(), ...

opendir()
http://pubs.opengroup.org/onlinepubs/7908799/xsh/opendir.html
(follow links for other, related calls)

It will give you all the files in a folder. You have to check the file names to see if they match you pattern. In the simple case, you can just look to see if the path ends with ".prn". This will require something like string::find() (or C library strstr() call), if you want to search for the substring, or string::length() plus string::substr() (or C library strlen() and strcmp() calls). For a more general solution, people often use regex.

Andy
Last edited on
Ok thank you. I have looked into the string::find functionality and strstr(). The problem with those now are I do not know how to communicate with the directory. How can I check if the path ends with '.prn'? As in, how can I set the string value to actually look in the directory and not just take the string literal of "/home/Desktop..."?
Have you checked the example yet?

readdir()
http://pubs.opengroup.org/onlinepubs/7908799/xsh/readdir.html

Tt is looking for a specific file (using strcmp). You just need to modify the condition so it looks for an extension of ".prn".

Andy
Yes, this is what I have:

1
2
3
4
5
6
7
8
9
10
11
	    if ((dirp = opendir("/home/byu/Documents/PrinterFiles/")) == NULL) {
	    	cout << "Could not open directory!!";
	    	return;
	    }
		if ((dp = readdir(dirp)) != NULL && (strcmp(dp->d_name, ".prn") == 0)) {

			cout << "Found .prn File!!!!!";
		}
		else {
			cout << "No .prn files detected at this time...";
		}


I still get no kind of indication that it is looking for the .prn file extension. I get back "No .prn files detected at this time..."
Last edited on
:-\

I assumed you knew that strcmp() tests whether two strings are equal.

You want to test if dp->d_name ends with ".prn", not if it's equal. So you need a helper function which tests a string to see it it has a suffix.

Andy
Last edited on
Yea...that's my bad. It's been a long day haha. I will look at this tomorrow. I need to get away from it for a bit. Thank you for your help!
Ok, I have looked back at this over the weekend and it seems that I have it working, but only to an extent. The program is able to now detect when a .prn file is created in the directory. The problem is that when the file is deleted from the directory and there are no more .prn files there, I still get a result saying there is a .prn file there. Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	    if ((dirp = opendir("/home/byu/Documents/PrinterFiles/")) == NULL) {
	    	LOG(INFO) << "Could not open directory!!";
	    	return;
	    }
		if ((dp = readdir(dirp)) != NULL) {
			prnStr << dp->d_name;
			size_t findprn = prnStr.str().find(".prn");

			if ((int)findprn != -1) {
				LOG(INFO) << "Found .prn File!!!!!";
			}
			else {
				LOG(INFO) << "No .prn files detected at this time...";
			}
		}


The LOG(INFO) is like cout.

The program is a multithreaded application, so this is simply a function that gets called over and over on a separate thread, constantly checking to see whether a .prn file has been found or dropped. It is the latter that I am now having the issue with. Any thoughts?
Nevermind, I got it. I need to re-initialize my stringstream variable every time. I didnt think that was necessary, I figured the function would take care of that for me.
Last edited on
Topic archived. No new replies allowed.