With respect for my friend Alex... GetCurrentDirectory() and SetCurrentDirectory() is one way to do this... but they change more than just the name of the directory you're viewing... SetCurrentDirectory() also changes where files are stored if you don't specify a path so it should be used with deliberacy and you should store the original directory name to put it back when you're done. More than one programmer has gotten a big surprise when writing a file they later cannot find.
If you are looking for a list of all files in a folder... The better plan is to use
FindFirstFile() and
FindNextFile() from the windows api. These will allow you to enumerate the files in any folder, find folders etc. without disturbing system settings. And as a bonus, they're going to be 10X faster than system() calls.
If you already know the path and name of the file and just need to check it's size (etc.) you should use
GetFileAttributesEx().
If all you need is a quick check to see if a given file exists or not you can use
GetFileAttributes() which returns a special value if the file doesn't exist.
Also...
There are better ways to load a file into memory than getch()
Making large file buffers on the stack is begging for a program crash, you should use malloc() instead.
and
You should know that scanf() will stop on the first space it encounters...
Try something like this...
#include <windows.h>
#include <stdio.h>
char fname[MAX_PATH]; // user's file name
unsigned char* data; // file contents in memory
WIN32_FILE_ATTRIBUTE_DATA fad; // data for getfileattributesex
FILE* fh; // file handle
// get filename from user and other stuff...
//check file and get size
if ( ! GetFileAttributesEx( fname, GetFileExInfoStandard, &fad) )
{ // file not found, handle error }
// open the file
fh = fopen(fname, "rb"); // open in binary mode
if ( ! fh )
{ // file not open, handle error }
// reserve memory for the file
data = malloc( fad.nfilesizelow + 5); // always get a little extra
// load into memory
fread(data, fad.nfilesizelow, 1, fh);
// close the file
fclose (fh);
// do what you gotta do with the file data
free(data);
Hope this helps....