-1

I need to read, and then write to a file. I don't want to use "r+" because I completely overwrite it anyway. But I am unable to completely close the file. If I try to open the file for the second time, the applications crashes (can't open). Does anyone know how to completely close the file.

And this is not the actually code, just a summary of what I want to do:

FILE* f;
fopen_s(&f, "test.txt", "r");
// read file and edit data
fclose(f);
f = 0;
fopen_s(&f, "test.txt", "w");
fprintf(f, "%c", data);
fclose(f);
tshepang
  • 12,111
  • 21
  • 91
  • 136
  • It's not clear to me, why can't you close the file? You're trying to read, then write from where you left off reading? – Mike Oct 26 '12 at 17:22
  • 1
    What's `fopen_s()`??? With the Standard function [`fopen()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/fopen.html) I'd test the return value to check if the operation suceeded ... and possibly report an error otherwise (the function [`perror()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/perror.html) comes to mind). – pmg Oct 26 '12 at 17:23
  • I think it's the secure version of fopen! – trumpetlicks Oct 26 '12 at 17:24
  • @pmg fopen_s is a safer version of fopen in windows. –  Oct 26 '12 at 17:24
  • @pmg - [fopen_s](http://msdn.microsoft.com/en-us/library/z5hh6ee9.aspx) – Mike Oct 26 '12 at 17:24
  • @Mike I'm trying to completely overwrite the file. –  Oct 26 '12 at 17:25
  • Ah! `fopen_s()` is not very safe though ... I wouldn't call "safe" crashing the application :) – pmg Oct 26 '12 at 17:27
  • @pmg It doesn't crash the applications, fprintf/fclose does. I could have checked if the file was available, but I presume it is available, because I have closed the file less than a millisecond ago. –  Oct 26 '12 at 17:34
  • fopen_s is (optional) C11 standard – user411313 Oct 26 '12 at 17:34
  • Safe simply means that it validates all input parameters to be valid, not that it will run correctly, especially if called incorrectly. – trumpetlicks Oct 26 '12 at 17:35
  • The best solution was "restart your computer", visual studio was acting crazy. –  Oct 26 '12 at 17:44
  • 1
    here's a whack-idea: maybe check the return value of some of those functions. errnot_t is returned from the f*_s() family for a *reason*. – WhozCraig Oct 26 '12 at 17:47
  • One thing to note with files on Windows (saw this more with opening files over a network, but same issue applies) was that sometimes when I would close a file and then try to quickly opening it for writing/ move it/ delete it I would get an error code indicating the file was in use. If I added a sleep of 50 ms and then retried it would many times succeed on the retry. – pstrjds Oct 26 '12 at 19:13
  • Since this snippet is just a summary and not actual code -- are you sure the second `fopen` isn't inside a conditional, and the second `fclose` outside it? (Not even sure that would give you the kind of crash you report.) – kbshimmyo Feb 16 '14 at 18:07

1 Answers1

2

fclose() closes the file. It doesn't half close it. The notion of "completely closing" a file doesn't exist. A file is either open or closed. There's no in-between.

Although in your code you're using fopen_s() to open the file. I've no idea what that function is. I assume it works like fopen(), but instead of returning a FILE pointer, it instead stores it in its argument.

So the answer to your question is: to "completely" close a file, use fclose(). As you already do. That means the problem you're having lies elsewhere.

Nikos C.
  • 50,738
  • 9
  • 71
  • 96