working directory / file copying

hi! need some more help

is there an instruction to get the working directory -I mean the directory in which the program is to be executed-?

thank you!
Last edited on
Which OS? What do you want to do with it?
OS = windows

i want to get a file, change its name and copy it in another directory.


newname = "mynewfile.txt";
path = "C:\\work\\" // here I should get the working directory

// read file to be copied
oldfile = fopen(filename, "r");
if (oldfile==NULL) {cout<<"File error";}

// get size of file
fseek (oldfile , 0 , SEEK_END);
filesize = ftell (oldfile);
rewind (oldfile);

// memo allocation
buffer = (char*) malloc (filesize);
if (buffer == NULL) {cout<<"Memo error";}

// store file content on buffer
result = fread (buffer,1,filesize ,oldfile)
if (result != filesize) {cout<<"Reading error";}

// copy buffer in new file
newfile = fopen( strcat(path, newname), "w");
fwrite(buffer, 1, filesize, newfile);

// end
fclose (oldfile);
fclose (newfile);
free (buffer);

in addition to the fact that i can't get the working directory, it is not copying properly!!! I always get reading error...

any advice?
Last edited on
If you don't specify any directory or you use "." as path you'll get the working directory
You can get and put single characters from the old to the new files instead of copying the entire file at once.
If you are writing C++ you should use fstreams instead of FILE*s
it doesn't work for the directory!

i'm working on c:\ and wanna store on C:\work (which is an already-existing directory).

i tried path = ".\\fs\\", path = "fs\\", path = "..\\fs\\" and none of them works

and how should I copy single characters? a loop? shall i use a buffer for this too?

sorry but i'm quite rust...

:s
Last edited on
What does fs have to do with \work?
By the way, you can use / instead of \\. The kernel doesn't care, but it's a shame that the UI does.
and how should I copy single characters? a loop?

Open the two files
in a loop:
read a character from input file
check if the file is ok ( no EOF )
write the character in the output file

sorry, fs is the directory i wanna move to. i'm getting a bit confused with so many names

thanks for the hints, it's helping a lot, but i'm yet not done :)

so i tried to copy using:

newname = strchr(filename,'-') + 1;
path = "C:\\work\\fs\\";

oldfile = fopen(filename, "r");
if (oldfile==NULL) {cout<<"FILE ERROR\n";}
rewind (oldfile);

newfile = fopen( strcat(path, newname), "w");
if (newfile==NULL) {cout<<"FILE ERROR\n";}
rewind(newfile);

do{ c = getc(oldfile);
fputc ( c, newfile );
} while(c != EOF);

fclose (oldfile);
fclose (newfile);

and it ALMOST woeks! i means it copies everything... but then there is a weird character (ÿ) at the end of the new file... foes anyone know why?

and i know i am still doing sthg wrong because i cannot repeat it (tell the program to take another file and copy it under a new name) without reseting the program. i get a memory error (acces violation at address XXXXX in module .exe) I THINK ITS STRCAT CAUSING THE PROBLEM

????
Last edited on
newname is incorrect I believe. You have a random + 1 at the end, not really sure what that is for.
strcat(path, newname)
path points to a read-only char array, but strcat() needs to be able to write to the first parameter.

The 'ÿ' is 0xFF, or -1, or EOF. Check for EOF before the loop body, not after.
the +1 in newname is because the name of oldfile is sthg like XXX-YYY.txt, and newfile should be YYY.txt, so i look for the '-' and then get what is right after (next character -> pointer+1)

i checked EOF before the loop, with the same result.

why should path be a read-only array?
Topic archived. No new replies allowed.