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
.maverk10mo 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
Eakam10mo ago
w is writing only, w+ is reading and writing
.maverk
.maverk10mo 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
Eakam10mo 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
.maverk10mo 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
Eakam10mo 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
.maverk10mo ago
that doesn't work
Eakam
Eakam10mo ago
Then try to do it using w+
.maverk
.maverk10mo ago
No description
.maverk
.maverk10mo ago
absolutely no output
CaramelCorn
CaramelCorn10mo 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
.maverk10mo ago
very nice it works as expected
Eakam
Eakam10mo ago
w shouldn't allow you to read the file using the same file pointer
CaramelCorn
CaramelCorn10mo ago
yep it won't
Eakam
Eakam10mo ago
I thought it would fail, but it seems to fail silently Like just returns -1
.maverk
.maverk10mo ago
wait a second it works as expected but there is one thing confusing me
CaramelCorn
CaramelCorn10mo 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
Eakam10mo ago
I had forgotten, am too used to errors being thrown
CaramelCorn
CaramelCorn10mo ago
hah same
.maverk
.maverk10mo 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
CaramelCorn10mo ago
nope
.maverk
.maverk10mo ago
what ??? why
CaramelCorn
CaramelCorn10mo 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
.maverk10mo ago
aaaah oky oky i understand but why hello world get erased ?
CaramelCorn
CaramelCorn10mo 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
.maverk10mo 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
CaramelCorn10mo 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
Eakam10mo ago
Weird, it didn't disappear for me
CaramelCorn
CaramelCorn10mo ago
that's what I assume would be the case
Eakam
Eakam10mo ago
No description
Eakam
Eakam10mo ago
Maybe it disappears on windows?
CaramelCorn
CaramelCorn10mo ago
I'll test it on my desktop, it's running windows rn
.maverk
.maverk10mo ago
what is odd ? why ? should the content of the file not be removed ?
CaramelCorn
CaramelCorn10mo ago
no it shouldn't be removed after that cose is run, not if the code is executed correctly
.maverk
.maverk10mo ago
wait a minute is that a ub ?
CaramelCorn
CaramelCorn10mo 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
Eakam10mo 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
CaramelCorn10mo ago
yep
Eakam
Eakam10mo ago
I managed to get it to save to the file by adding a rewind()
.maverk
.maverk10mo ago
ok hhh this will cause some confusion
CaramelCorn
CaramelCorn10mo ago
yep yep, the crash would stop it from saving the write to the file
Eakam
Eakam10mo ago
It is basically ubuntu running in a terminal in windows
.maverk
.maverk10mo ago
i see
CaramelCorn
CaramelCorn10mo ago
we do love VMs
.maverk
.maverk10mo ago
who doesn't carmen
Eakam
Eakam10mo 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
CaramelCorn10mo ago
you can use mingw
Eakam
Eakam10mo ago
Just gcc and run
CaramelCorn
CaramelCorn10mo ago
but yeah VS is definitely the easiest way on windows
Eakam
Eakam10mo 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
.maverk10mo ago
u can use gcc filename.c
Eakam
Eakam10mo ago
That is what I did on ubuntu
.maverk
.maverk10mo ago
but
Eakam
Eakam10mo ago
Can you use gcc on windows?!
.maverk
.maverk10mo 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
CaramelCorn10mo ago
via mingw as well u can use gcc on windows
Eakam
Eakam10mo ago
Ohhhh, that's a thing
CaramelCorn
CaramelCorn10mo ago
but yeah
Eakam
Eakam10mo ago
I have never used c with visual studio code so I didn't know Thanks
.maverk
.maverk10mo ago
No description
.maverk
.maverk10mo ago
hhhh with one click ooh yeah
Eakam
Eakam10mo 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
.maverk10mo ago
yeaah it can hide the file path too i use vs code when working with stdin and stdout
Eakam
Eakam10mo 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
.maverk10mo ago
it doesn't matter while no one has access to those sensitive data
Eakam
Eakam10mo ago
Yea, the most you would get is my silly foder organization
.maverk
.maverk10mo ago
i see