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