Jump to content

Issues opening a file in C

FILE *file = fopen(filename, "w+");

I need to open a file and overwrite whatever's in it, which the above should do. But it just ends up appending the file with whatever I print to it next. 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
https://linustechtips.com/topic/454832-issues-opening-a-file-in-c/
Share on other sites

Link to post
Share on other sites

I've read through that, and what I did should open the file with the stream at the beginning of the file and ready to overwrite whatever's in the file. 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to post
Share on other sites

You are one character away from being correct.

FILE *file = fopen(filename, "w");

This is how it should go.

 

 

I've read through that, and what I did should open the file with the stream at the beginning of the file and ready to overwrite whatever's in the file.

That's wrong because if there is less content than there was before, you will have a trail of garbage from the last write. If you try to fix it by overwriting every following character, let's say, with space, you will waste space and compute resources, nobody wants/cares about a file half full with trailing spaces.

Link to post
Share on other sites

You are one character away from being correct.

FILE *file = fopen(filename, "w");

This is how it should go.

 

That's wrong because if there is less content than there was before, you will have a trail of garbage from the last write. If you try to fix it by overwriting every following character, let's say, with space, you will waste space and compute resources, nobody wants/cares about a file half full with trailing spaces.

that still doesn't overwrite the file, it just appends (prepends*, like w+) to it.

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to post
Share on other sites

that still doesn't overwrite the file, it just appends (prepends*, like w+) to it.

Are you sure?

I have this code :

 

#include <stdio.h>int main(){    FILE *f = fopen("text.txt", "w");    fprintf(f, "Hello there world ");}

And it works just like you describe it should.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to post
Share on other sites

Are you sure?

I have this code :

 

#include <stdio.h>int main(){    FILE *f = fopen("text.txt", "w");    fprintf(f, "Hello there world ");}

And it works just like you describe it should.

 

"w+" access mode should also create a new file on each run. Try to post the complete code you are using or at least the part where it writes content to the file.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×