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