where is the difference between these two file modes ?

where is the difference between w and w+ in c ? they both erase the file where is the point of using w+ in c language ?
67 Replies
.maverk
.maverkOP2y ago
i have checked :
https://en.cppreference.com/w/c/io/fopen#File_access_flags
https://en.cppreference.com/w/c/io/fopen#File_access_flags
but it is still not clear to me where the point of using w+ is
Eakam
Eakam2y ago
w is writing only, w+ is reading and writing
.maverk
.maverkOP2y ago
how reading i don't understand ? when i use w+ the file will be erased even with only w where is the point here i am confused
Eakam
Eakam2y ago
If you use w, you can only write to the file If you use w+, you can write to the file and read from the file Using the file stream pointer that is returned
.maverk
.maverkOP2y ago
wait let address this little more better: the file will be erased before even reading it can you show me an example ? i used both w and w+ the file will be erased i cannot even read
#include <stdio.h>

int main(){
FILE *file = fopen("test.txt", "w+");

char buffer[992];


fscanf(file, "%s", buffer);

printf("%s", buffer);

}
#include <stdio.h>

int main(){
FILE *file = fopen("test.txt", "w+");

char buffer[992];


fscanf(file, "%s", buffer);

printf("%s", buffer);

}
result:
░dVJf☺
░dVJf☺
Eakam
Eakam2y ago
Do this: - Open a while in w mode - Write to it - Try to read from it All using the same file stream pointer that is returned
.maverk
.maverkOP2y ago
that doesn't work
Eakam
Eakam2y ago
Then try to do it using w+
.maverk
.maverkOP2y ago
No description
.maverk
.maverkOP2y ago
absolutely no output
CaramelCorn
CaramelCorn2y ago
you need to rewind the pointer the pointer is still at the end of the write when you go to read, so it reads nothing otherwise yeah between w+ and w there will be no difference if you don't move the pointer around
.maverk
.maverkOP2y ago
very nice it works as expected
Eakam
Eakam2y ago
w shouldn't allow you to read the file using the same file pointer
CaramelCorn
CaramelCorn2y ago
yep it won't
Eakam
Eakam2y ago
I thought it would fail, but it seems to fail silently Like just returns -1
.maverk
.maverkOP2y ago
wait a second it works as expected but there is one thing confusing me
CaramelCorn
CaramelCorn2y ago
yeah C doesn't have the same try->catch of newer langs so the return values are how you need to handle errors if it crashed outright that would be messy
Eakam
Eakam2y ago
I had forgotten, am too used to errors being thrown
CaramelCorn
CaramelCorn2y ago
hah same
.maverk
.maverkOP2y ago
#include <stdio.h>

int main(){
FILE *file = fopen("test.txt", "w");

fprintf(file, "hello world");


char buffer[443];
fscanf(file, "%s", buffer);
printf("%s", buffer);
}
#include <stdio.h>

int main(){
FILE *file = fopen("test.txt", "w");

fprintf(file, "hello world");


char buffer[443];
fscanf(file, "%s", buffer);
printf("%s", buffer);
}
there are 2 operations going on here right ? it opens the file twice first opening a file and writing to it hello world and then opening the file again for reading am i correct ?
CaramelCorn
CaramelCorn2y ago
nope
.maverk
.maverkOP2y ago
what ??? why
CaramelCorn
CaramelCorn2y ago
the file stays opened, the pointer is a reference to the file you need to manually close the file when you're done the file is opened once in fopen then you perform operations on the open file
.maverk
.maverkOP2y ago
aaaah oky oky i understand but why hello world get erased ?
CaramelCorn
CaramelCorn2y ago
it doesn't get erased, the file pointer is pointing to the end of the last thing you wrote at the end of fprintf, C doesn't reset the file pointer after each operation, which is actually very helpful you're just reading from this point: Hello World the is essentially where the pointer is when you then go to read from that point on
.maverk
.maverkOP2y ago
yes i understand this very well i understand the cursor never goes back to the begining after any operation but what i don't understand is my hello world gets erased and i don't find it in the file when i look into it
CaramelCorn
CaramelCorn2y ago
oh really? that is odd shouldn't be happening I'd have to play around with it myself ig, but that behaviour doesn't make sense
Eakam
Eakam2y ago
Weird, it didn't disappear for me
CaramelCorn
CaramelCorn2y ago
that's what I assume would be the case
Eakam
Eakam2y ago
No description
Eakam
Eakam2y ago
Maybe it disappears on windows?
CaramelCorn
CaramelCorn2y ago
I'll test it on my desktop, it's running windows rn
.maverk
.maverkOP2y ago
what is odd ? why ? should the content of the file not be removed ?
CaramelCorn
CaramelCorn2y ago
no it shouldn't be removed after that cose is run, not if the code is executed correctly
.maverk
.maverkOP2y ago
wait a minute is that a ub ?
CaramelCorn
CaramelCorn2y ago
I'm getting an error on windows that causes a crash when I attempted to read from the end of the file, which causes the file to not be closed properly so it doesn't save the print
Eakam
Eakam2y ago
Yes, ubuntu using Windows subsystem for linux Same, it is because the file pointer is at the end of the file when you read
CaramelCorn
CaramelCorn2y ago
yep
Eakam
Eakam2y ago
I managed to get it to save to the file by adding a rewind()
.maverk
.maverkOP2y ago
ok hhh this will cause some confusion
CaramelCorn
CaramelCorn2y ago
yep yep, the crash would stop it from saving the write to the file
Eakam
Eakam2y ago
It is basically ubuntu running in a terminal in windows
.maverk
.maverkOP2y ago
i see
CaramelCorn
CaramelCorn2y ago
we do love VMs
.maverk
.maverkOP2y ago
who doesn't carmen
Eakam
Eakam2y ago
I still don't know if there is an easier way to compile and run c on windows other than visual studio So I love ubuntu for this
CaramelCorn
CaramelCorn2y ago
you can use mingw
Eakam
Eakam2y ago
Just gcc and run
CaramelCorn
CaramelCorn2y ago
but yeah VS is definitely the easiest way on windows
Eakam
Eakam2y ago
Yea, I looked at it once and the whole process sounded like a lot It is funny though this particular code saves the file on linux but fails on windows
.maverk
.maverkOP2y ago
u can use gcc filename.c
Eakam
Eakam2y ago
That is what I did on ubuntu
.maverk
.maverkOP2y ago
but
Eakam
Eakam2y ago
Can you use gcc on windows?!
.maverk
.maverkOP2y ago
why don't u just use the extension with code runner ? or sublime with a system build i can share you can run c codes with f7 with one button
CaramelCorn
CaramelCorn2y ago
via mingw as well u can use gcc on windows
Eakam
Eakam2y ago
Ohhhh, that's a thing
CaramelCorn
CaramelCorn2y ago
but yeah
Eakam
Eakam2y ago
I have never used c with visual studio code so I didn't know Thanks
.maverk
.maverkOP2y ago
No description
.maverk
.maverkOP2y ago
hhhh with one click ooh yeah
Eakam
Eakam2y ago
Well it is one click with visual studio as well but I just hate going through the setup The code runner thing looks interesting
.maverk
.maverkOP2y ago
yeaah it can hide the file path too i use vs code when working with stdin and stdout
Eakam
Eakam2y ago
(Frantically looking back to see if I accidentally showed the entire file path in the screenshot) Ah, I have only used vs code with c++
.maverk
.maverkOP2y ago
it doesn't matter while no one has access to those sensitive data
Eakam
Eakam2y ago
Yea, the most you would get is my silly foder organization
.maverk
.maverkOP2y ago
i see

Did you find this page helpful?