How to check for file extension?

closed account (SwAfjE8b)
Hello!

I am trying to have a user input a file name but I am trying to figure out some way for it to check for the extension and that it has at least one letter before the period. Below is my code so far and further on down is my goal. Any help is greatly appreciated, Thank you!

#include <iostream>
#include <fstream>
#include <string>

void fileData()
{
bool ok = true;
std::ofstream file;
std::string fileName;
std::string response;

std::cout << "Enter the filename for the data:" << std::endl;
getline(std::cin, fileName);

file.open(fileName.c_str());

do
{
std::cout << "Enter an integer, or DONE to stop:" << std::endl;
std::cin >> response;
if (response == "DONE" || response == "done")
ok = false;
}
while(ok);
file << response << std::endl;

file.close();
}

int main()
{
std::cout << "Welcome to the Number Conversion Answer Generator" << std::endl;
fileData();

system ("PAUSE");
return 0;
}


what I'd like it to do:

Enter the filename for the data: a
WARNING: a does not end in .xml or is not long enough
Enter the filename for the data: .xml
WARNING: .xml does not end in .xml or is not long enough
Enter the filename for the data: sample.xml
Enter an integer, or DONE to stop: 0
Enter an integer, or DONE to stop: -10
WARNING: -10 is less than 0. Will not be accepted.
Enter an integer, or DONE to stop: cat
WARNING: cat cannot be converted to an integer.

P.S - if anyone could also help with checking for a int and making sure it's not negative that would be incredible :)
P.S - if anyone could also help with checking for a int and making sure it's not negative that would be incredible :)


This is very simple. You just check if its less than 0 or not.

1
2
3
4
5
6
int x;
cin >> x;
if(x < 0) // if x is less than 0, it would be negative
{
    cout >> "x is a negative number!" << endl;
}
Last edited on
closed account (SwAfjE8b)
Thank you TarikNeaj, but I have to use a string because the user has to have to option to enter done to quit the program. Do you know how you could do it with a string?
Once you know it is a number, you can simply convert it to an integer using std::stoi.

1
2
3
4
5
6
7
string number = "5";
int y = std::stoi(number);
cout << y << endl;
if (y < 0) // if you did number < 0 it wouldnt work since number is string. Conver it to int.
{
	// http://www.cplusplus.com/reference/string/stoi/
}
As for your actual problem, you can simply use the function find().

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string word = "word.xml";
	int x = word.find(".");
	cout << x << endl;
	
	system("pause");
}


The function find() returns the position of what you are trying to find. x will equal 4. Becuase word[4] is where the dot is. So if you want to check that there are characters before that, you can simply make sure that x's value is 1 or higher, meaning that something is taking up a character before it.

Update: If you want to make sure it ends in .xml, just check the last 4 characters in the string, and see if it is ".xml".
Last edited on
I am trying to have a user input a file name but I am trying to figure out some way for it to check for the extension and that it has at least one letter before the period.


This takes a file name and breaks it apart. You'll just have to figure out which parts you want to keep and remove all the other couts. Remember a period can be part of the file name such as x1.y2.z3.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Verify a file name is valid

#include <iostream>
using namespace std;

int main()
{
string fileName;
int fileNameLength=0;
int exensionLength=0;
int Namelength=0;
int whitepace=0;
int periods=0;

std::cout << "Enter the filename : " << std::endl;
getline(std::cin, fileName);
fileNameLength=fileName.size();

std::cout << "\tChar" << "\tCount" << "\tWS" << "\tPeriod" << endl;
	for (int i=0; i < fileNameLength; i++)
		{
		exensionLength++;
		if (isspace(fileName[i])){whitepace++;}
		if (fileName[i] == '.'){periods++;exensionLength=0;}
		
		std::cout << "\t" << i+1 << "\t- "<< fileName[i] << "\t" ;
		std::cout << whitepace << "\t" << periods << endl;
		}
std::cout << "\tThe filename is " << fileNameLength-1-exensionLength << " charcters " << endl;
std::cout << "\tThe exetention is " << exensionLength << " charcters " << endl;
std::cout << "\tThe full name contains " << periods << " period(s). " << endl;
return 0;
}
Last edited on
Topic archived. No new replies allowed.