Hi,

Could anyone please check my sample code? I am a beginner in C++. This code is for file comparison. Whenever I run it, the compiling result is always "Usage: compiles <file1> <file2>".
I think something is wrong. Please give me some suggestions how to go through the code.

Best Regards,
zawpai

Recommended Answers

All 13 Replies

Please post your code here instead of giving it as an attachment. I'm not going to open a zip file of 1.04 MB, that should contain code. 1.04 MB of text is rather much.

So just post your cpp file(s) here and be sure to use [code=cpp] //code here [/code] tags

Try printing the arguments the program receives, I think you are simply not calling it correctly (maybe e.g. due to spaces in file paths)

if (argc != 3)
{
    cout << "Usage: compfiles <file1> <file2>\n";
    for(int ii = 0; ii < argc; ++ ii)
    {
        cout << "Arg# " << ii << " = " << argv[ii] << "\n";
    }
return 1;
}

Hi niek_e,

The following is my sample code. Please check for me. Actually, I have already saved the two files in the same folder before doing the compiling my codes.

/*
	Create a file comparison utility.
*/

#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char *argv[])
{
	register int i;
	//int numread;

	unsigned char buf1[1024], buf2[1024];

	if (argc != 3)
	{
		cout << "Usage: compfiles <file1> <file2>\n";
		return 1;
	}

	ifstream f1(argv[1], ios::in | ios::binary);
	
	if(!f1)
	{
		cout << "Can't open first file.\n";
		return 1;
	}

	ifstream f2(argv[2], ios::in | ios::binary);

	if (!f2)
	{
		cout << "Can't open second file.\n";
		return 1;
	}

	cout << "Comparing files.....\n";

	do
	{
		f1.read((char *) buf1, sizeof buf1);
		f2.read((char *) buf2, sizeof buf2);

		if (f1.gcount() != f2.gcount())
		{
			cout << "Files are of differing sizes.\n";
			f1.close();
			f2.close();
			return 0;
		}

		// compare contents of buffers
		for (i=0; i< f1.gcount(); i++)
		{
			if(buf1[i] != buf2[i])
			{
				cout << "Files differ.\n";
				f1.close();
				f2.close();
				return 0;
			}
		}
	}while(!f1.eof() && ! f2.eof());

	cout << "Files are the same.\n";

	f1.close();
	f2.close();
	
	
	return 0;
}

mitrmkar:
I have already test ur codes. I saw the value of 'argc' is zero. Do u have another good ideals?

Best Regards,
zawpai

I saw the value of 'argc' is zero. Do u have another good ideals?

That's not possible, It should always be > 0. The first 'argv'(argv[0]) is always the program-name. (in your case 'compiles'?) and if you run the program with the two parameters as you programmed, argc should be 3 (programname, param1, param2).

When I run your program argc == 1; as it should be:

if (argc != 3)
{
	cout << "Usage: compfiles <file1> <file2>\n";
	cout << "argc="<< argc;
	return 1;
}

output: argc=1

p.s.: Next time you post code use [code=cpp] //code here [/code] tags as I mentioned before

Hi niek_e,

I also don't know where the problem is exactly. Thank you for ur replying.

Best Regards,
zawpai

You might want to try running your program from the command line as opposed to your IDE's built in console (I assume that's what you're using right now).

Hi John A,

Your way will be corrected, but I can't still do well. My friend told me not to run the code and compile the exe file. He also told me after building my code, I will see the exe file. But, I can't see that file and where it is located.
How should I do command line compling?

Best Regards,
zawpai

What compiler and OS are you using?

Hi niek_e,

I run my codes in 'Microsoft Visual C++ 6.0'. I am using window XP.

Thanks,
zawpai

I haven't used vc6 for a while (because the newer and free version 2008 is available)
But my guess is that you didn't release the build. Somewhere there should be a configurationmanager. In this you've probably selected 'debugbuild'. Change this to 'release' and the .exe should be made in your project-directory

I run my codes in 'Microsoft Visual C++ 6.0'. I am using window XP.

Then, from the menu, open Project/Settings/Debug -tab. Supply the paths of the two files you want to cmpare in the 'Program arguments' field.

Hi,

Now, I can compare the two files by using "mitrmkar's way". But, I need to find out how to create the exe file as "niek_e" told me.
Thanks all for giving the good suggestions.

Best Regards,
zawpai

But, I need to find out how to create the exe file as "niek_e" told me.
Thanks all for giving the good suggestions.

From the menu, open Project/Settings/Linker -tab. You should see a field titled 'Output file name', there you can specify the file path/name for your program. The paths are relative to your project's path.
By default, there are two project build configurations available Release and Debug, The output file is specified on a per build configuration basis. You can switch the build configuration by selecting from the menu; Build/Set Active Configuration ...

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.