This belongs in both C and C++ forums, but it's more of C++ (I think) so I'll keep it in here for now.

I'm working on a C++ program to extract files that I am constantly compressing and testing (releasing software for beta testers to take a look at). I use the 7-Zip program to aid in this process.

Here is my code so far, and I bet you can just look at it and guess what's going wrong.

#include <iostream>
#include <string.h>
using namespace std;

int main()
{
	char file[256];
	char opening[14] = "7z.x64.exe x ";
	char final[256], tar[6] = "*.tar";
	cout<<"Please enter the archive name to extract the files from (please include the extension): ";
	cin.getline(file, 256);
	strcpy(final, opening);
	strcat(final, file);
	cout<<"\n"<<final<<"\n\n";
	system(final);
	cout<<"\n\n";
	system("pause");
	if (file == "*.tar")
	{
		strcpy(final, opening);
		strcat(final, tar);
		system(final);
		cout<<"\n\n";
		system("pause");
	}
}

Well now, I can't get this "if" statement to work, any suggestions? Thanks.

P.S. The parts before the if statement do work.

I assume you are referring to line 18. It should evaluate true if file is "somefile.tar", false if it is "somefile.txt"? Since no one is going to enter "*.tar", '*' is not meant to be a literal character. Perhaps check if ".tar" occurs in file using strstr from the cstring library?

http://www.cplusplus.com/reference/clibrary/cstring/strstr/

line 18: should be this: if ( strcmp(file,"*.tar") == 0) The way you had it was comparing two address, not their contents. But Vernon is right about using strstr() to see if ".tar" was entered in the file name.

Thank you VernonDozier, that strstr works perfectly! Here's my code now:

#include <iostream>
#include <string.h>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <cstring>
#include <ctime>
using namespace std;

int main()
{
	ofstream outputFile;
	string filePrefix = "output";
    string fileSuffix;
    string fileName;
    stringstream convert;
	char dateStr [9];
    char timeStr [9];

	char file[256];
	char opening[14] = "7z.x64.exe x ";
	char final[256], tar[6] = "*.tar";
	cout<<"Please enter the archive name to extract the files from (please include the extension): ";
	cin.getline(file, 256);
	strcpy(final, opening);
	strcat(final, file);
	cout<<"\n"<<final<<"\n\n";
	system(final);
	cout<<"\n\n";
	system("pause");

/*
*	char time[10];
*	_strtime(time);
*	fileSuffix = time;
*	fileName = filePrefix + fileSuffix;
*		//Open file using *.c_str()
*	outputFile.open(fileName.c_str());
*	outputFile << final << "\n\n";
*/

	if (strstr(file, ".tar"))
	{
		strcpy(final, opening);
		strcat(final, tar);
		system(final);
		cout<<"\n\n";
		system("pause");
		//outputFile << final << "\n\n";
		//outputFile.close();
	}
/*
*	else {
*		outputFile.close();
*	}
*/
}

Now just to implement a debug file of what the program did...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.