Any C programmers who are using Visual Studio?


Status
Not open for further replies.

mcgeezer

Winger
I've downloaded Visual Studio 2010 Express edition.

The following C code which is a simple "copy file1 to file2" program compliles fine, but when I run the program it's responding back with Error 2 which is file not found?

If I go into a DOS box and tun vcvars32.bat and then use cl.exe to build the same source code the program works fine, I'm not that good with the VS IDE so can anyone who use it give me any help as to where I'm going wrong? It's a default install of Visual Studio Express 2010 I'm using.

Cheers,
McGeezer


Code:
#include <windows.h>
#include <stdio.h>
#define BUF_SIZE 16384  /* Optimal in several experiments. Small values such as 256 give very bad performance */

int main (int argc, LPTSTR argv [])
{
	HANDLE hIn, hOut;
	DWORD nIn, nOut;
	CHAR buffer [BUF_SIZE];
	if (argc != 3) {
		fprintf (stderr, "Usage: cp file1 file2\n");
		return 1;
	}
	hIn = CreateFile (argv[1], GENERIC_READ, FILE_SHARE_READ, NULL,
			OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if (hIn == INVALID_HANDLE_VALUE) {
		fprintf (stderr, "Cannot open input file. Error: %x\n", GetLastError ());
		return 2;
	}

	hOut = CreateFile (argv[2], GENERIC_WRITE, 0, NULL,
			CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
	if (hOut == INVALID_HANDLE_VALUE) {
		fprintf (stderr, "Cannot open output file. Error: %x\n", GetLastError ());
		CloseHandle(hIn);
		return 3;
	}
	while (ReadFile (hIn, buffer, BUF_SIZE, &nIn, NULL) && nIn > 0) {
		WriteFile (hOut, buffer, nIn, &nOut, NULL);
		if (nIn != nOut) {
			fprintf (stderr, "Fatal write error: %x\n", GetLastError ());
			CloseHandle(hIn); CloseHandle(hOut);
			return 4;
		}
	}
	CloseHandle (hIn);
	CloseHandle (hOut);
	return 0;
}
 
It's 6+ years since I did any C or C++ so very rusty.

You mention that error 2 is "file not found" but that might not necessarily the case. To ascertain the true failure you need to get extended error information via a call to GetLastError.

http://msdn.microsoft.com/en-us/library/aa363858(v=vs.85).aspx

Anyway, you should go on codeguru where you'll likely get a better answer.

Parsnip this shite (always wanted to say that).

Thanks for the info mate - will give code guru a shot.
 
Hoy this in to see what file you're trying to copy. Rough guess is you're not using a fully specced pathname, or are in the wrong initial directory, and it isn't finding the file.

printf ("file1 = %s\n", (LPCSTR) argv[1])

When you say "run it" - how exactly are you running it (in the case where it doesn't work)?
 
Hoy this in to see what file you're trying to copy. Rough guess is you're not using a fully specced pathname, or are in the wrong initial directory, and it isn't finding the file.

printf ("file1 = %s\n", (LPCSTR) argv[1])

When you say "run it" - how exactly are you running it (in the case where it doesn't work)?

I'm running it from inside a dos box - or cmd.exe

The output I get is as follows after putting a printf directly before the CreateFile call and which has been compiled using the IDE.

Code:
D:\dev\vc\cpW\Debug>dir
 Volume in drive D is Data
 Volume Serial Number is 86D5-9AD1

 Directory of D:\dev\vc\cpW\Debug

22/04/2011  20:50    <DIR>          .
22/04/2011  20:50    <DIR>          ..
23/04/2011  12:06            29,184 cpW.exe
23/04/2011  12:06           329,208 cpW.ilk
23/04/2011  12:06           461,824 cpW.pdb
               3 File(s)        820,216 bytes
               2 Dir(s)  392,801,148,928 bytes free

D:\dev\vc\cpW\Debug>cpw cpw.pdb test2
file1 = cpw.pdb
Cannot open input file. Error: 2

D:\dev\vc\cpW\Debug>

The same file compiled using the cl.exe.

Code:
C:\Program Files\Microsoft Visual Studio 10.0\VC\bin>[b]vcvars32.bat[/b]
Setting environment for using Microsoft Visual Studio 2010 x86 tools.

C:\Program Files\Microsoft Visual Studio 10.0\VC\bin>d:

D:\dev\WSP4_Examples\CHAPTR01>[b]dir[/b]
 Volume in drive D is Data
 Volume Serial Number is 86D5-9AD1

 Directory of D:\dev\WSP4_Examples\CHAPTR01

23/04/2011  09:50    <DIR>          .
23/04/2011  09:50    <DIR>          ..
13/06/2010  08:59             1,187 cpC.c
13/06/2010  08:59               503 cpCF.c
13/06/2010  08:59               982 cpU.c
13/06/2010  08:59             1,264 cpUC.c
23/04/2011  12:06             1,343 cpW.c
23/04/2011  09:38             1,830 cpw.obj
13/06/2010  08:59             1,377 cpwFA.c
23/04/2011  09:39                 8 test1.txt
23/04/2011  10:52                 8 test2.txt
              10 File(s)         54,070 bytes
               2 Dir(s)  392,801,148,928 bytes free

D:\dev\WSP4_Examples\CHAPTR01>[b]cl cpw.c[/b]
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 16.00.30319.01 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

cpw.c
Microsoft (R) Incremental Linker Version 10.00.30319.01
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:cpw.exe
cpw.obj

D:\dev\WSP4_Examples\CHAPTR01>[b]dir[/b]
 Volume in drive D is Data
 Volume Serial Number is 86D5-9AD1

 Directory of D:\dev\WSP4_Examples\CHAPTR01

23/04/2011  12:12    <DIR>          .
23/04/2011  12:12    <DIR>          ..
13/06/2010  08:59             1,187 cpC.c
13/06/2010  08:59               503 cpCF.c
13/06/2010  08:59               982 cpU.c
13/06/2010  08:59             1,264 cpUC.c
23/04/2011  12:06             1,343 cpW.c
23/04/2011  12:12            46,080 cpw.exe
23/04/2011  12:12             1,918 cpw.obj
13/06/2010  08:59             1,377 cpwFA.c
23/04/2011  09:39                 8 test1.txt
23/04/2011  10:52                 8 test2.txt
              10 File(s)         54,670 bytes
               2 Dir(s)  392,801,148,928 bytes free

D:\dev\WSP4_Examples\CHAPTR01>[b]cpw test1.txt abc123
file1 = test1.txt
[/b]

D:\dev\WSP4_Examples\CHAPTR01>[b]dir[/b]
 Volume in drive D is Data
 Volume Serial Number is 86D5-9AD1

 Directory of D:\dev\WSP4_Examples\CHAPTR01

23/04/2011  12:12    <DIR>          .
23/04/2011  12:12    <DIR>          ..
23/04/2011  12:12                 8 abc123
13/06/2010  08:59             1,187 cpC.c
13/06/2010  08:59               503 cpCF.c
13/06/2010  08:59               982 cpU.c
13/06/2010  08:59             1,264 cpUC.c
23/04/2011  12:06             1,343 cpW.c
23/04/2011  12:12            46,080 cpw.exe
23/04/2011  12:12             1,918 cpw.obj
13/06/2010  08:59             1,377 cpwFA.c
23/04/2011  09:39                 8 test1.txt
23/04/2011  10:52                 8 test2.txt
              11 File(s)         54,678 bytes
               2 Dir(s)  392,801,148,928 bytes free

D:\dev\WSP4_Examples\CHAPTR01>
 
The answer is that I was forcing unicode yet the main function was taking in argv[] as ANSI. The Windows API generally has single functions but will call either an ANSI equivilent function or a Unicode equivilent function.

For example... CreateFile will either call CreateFileA (for ANSI) or CreateFileW (for Unicode) depending on your project setup.

The answer was to either switch off forced unicode in Visual C++ by goiung to Project Properties->Configuration Properties->General and set Character Set to Not Set

or

by fixing the source to tell main to cast argv as Unicode (rather than ansi).

This was done by adding...

Code:
#include <tchar.h>
.
.
.

and changing...
Code:
int main (int argc, LPTSTR argv [])
to
Code:
int _tmain (int argc, LPTSTR argv [])


This Windows shite - what a fuckin pallava.
 
It's not Windows shite, it's general character set shite. Had Unicode (and cheap storage) been around in the 60s, the IBM PC would have adopted it from the start, and Windows would probably never have supported anything else. Unfortunately that wasn't the case and it has to support multiple standards in different encoding widths, hence the dual-personality APIs.

The macros/headers do a fairly decent job of hiding the shite from you, in fact.
 
Status
Not open for further replies.

Back
Top