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