I've done this before in XP. I've got Vista now and the same function just doesn't work. There's nothing more to it than just:
Code:
remove("MainFile.txt");
rename("Temp.txt", "MainFile.txt");
This just doesn't seem to work now. I think it may have something to do with Vista trying to prevent unauthorized accessess to files. But I turned off the UAC and that still didn't work. And everything was created under my user name which is also the administrator. I even re-created the main file while in VE and it still fails. If it was something simple, I wouldn't be posting on here. I've tried everything up to looking up similar problems on google, to changing folder ownerships. This seems to be a common problem:
http://www.followsteph.com/2007/06/1...sta-read-only/
I just don't know how to get by this. Unless there's an easy way to open a text file, edit, and then close it without having to delete the file.
Through experimenting, files that are created in my program CAN be removed with the remove() function. I'll try another experiment. I'll try creating the file, edit it manually and rename it as my main file and delete the old one, and try the same functions again to see if that works.
result from experiment: still fails.
Ok, here's the weird thing. I just made this program and it worked.
1. create a text file called test.txt and put whatever you want in it.
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *fpFileTo = {NULL};
FILE *fpFileFrom = {NULL};
char readLine[80] = {0};
if ((fpFileFrom = fopen("test.txt", "r")) == NULL)
{
printfc(RED, "Could not find test.txt file.\n");
system("PAUSE");
return 0;
}
fpFileTo = fopen("temp.txt", "w");
// write to the temp file
fprintf(fpFileTo, "test1.\n");
fprintf(fpFileTo, "test2.\n");
fprintf(fpFileTo, "test3.\n");
// add data from the test file
while (fgets(readLine, 80, fpFileFrom))
fprintf(fpFileTo, "%s", readLine);
// close the files
fclose(fpFileFrom);
fclose(fpFileTo);
// try the part that fails
printf("remove: %d ", remove("test.txt"));
perror("error:");
printf("rename: %d ", rename("temp.txt", "test.txt"));
perror("error:");
system("PAUSE");
}
This tells me there must be somthing specific with my file. The weirder thing still is that I have already deleted the file and tried it from scratch and it didn't work. In the meantime, I guess if anyone needs to know how to edit a file, this program above is a good example.
Confirmed: I deleted my original file. Created a new one, with a different name. Changed it properly in my program. Re-ran the program and it still fails with a permission denied.
New Discovery: This works in main, but not in my subfunction in main. Will have to look more into that.
Solution: Found out what the problem was. I had an earlier function that would open the file to read information. Turns out I never closed it. I was always under the belief that the file closes automatically when leave the function. I guess that only applies in main. From now on I will fclose() all my files just to be safe. Sorry for bothering everyone. Hopefully, some people can use my example.