Dec 21, 2009 at 1:59pm Dec 21, 2009 at 1:59pm UTC
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 Dec 22, 2009 at 12:03pm Dec 22, 2009 at 12:03pm UTC
Dec 21, 2009 at 2:35pm Dec 21, 2009 at 2:35pm UTC
Which OS? What do you want to do with it?
Dec 21, 2009 at 3:04pm Dec 21, 2009 at 3:04pm UTC
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 Dec 21, 2009 at 3:58pm Dec 21, 2009 at 3:58pm UTC
Dec 21, 2009 at 4:27pm Dec 21, 2009 at 4:27pm UTC
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 fstream
s instead of FILE*
s
Dec 21, 2009 at 4:59pm Dec 21, 2009 at 4:59pm UTC
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 Dec 21, 2009 at 5:03pm Dec 21, 2009 at 5:03pm UTC
Dec 21, 2009 at 5:39pm Dec 21, 2009 at 5:39pm UTC
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.
Dec 22, 2009 at 9:03am Dec 22, 2009 at 9:03am UTC
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 Dec 22, 2009 at 9:20am Dec 22, 2009 at 9:20am UTC
Dec 22, 2009 at 9:31am Dec 22, 2009 at 9:31am UTC
newname is incorrect I believe. You have a random + 1 at the end, not really sure what that is for.
Dec 22, 2009 at 10:48am Dec 22, 2009 at 10:48am UTC
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?