views:

108

answers:

4

Hi, i tried fopen in C, the second parameter is open mode. two modes "r" and "rb" tricks me a lot...it seems they r the same...sometimes it better to use "rb", so, why does "r" exist?? can expain it to me, more detailed or examples thanx

+6  A: 

You should use "r" for opening text files. Different operating systems have slightly different ways of storing text, and this will perform the correct translations so that you don't need to know about the idiosyncracies of the local operating system. For example, you will know that newlines will always appear as a simple "\n", regardless of where the code runs.

You should use "rb" if you're opening non-text files, because in this case, the translations are not appropriate.

caf
This is platform-dependent. On Unix, text-mode does nothing special, so it still won't handle `\r\n` as `\n`.
Chris Jester-Young
The idea is that `"r"` will correctly open a "text file" on the same system. If you're opening a Windows text file on Linux with `"r"`, you have to take care of different line ending conventions yourself, of course.
Alok
plus:in windowsmy txt content is like `"a[b[c[html["` `[ is a return`but when i was using fread after fopened with "r", i got `"a\nb\nc\nhtml\nht\n"` you can see that at the end of the memory is kinda messed up...why ? thanx
Macroideal
If you open a file written in Windows on a linux system, you will see the behavior. You need to convert the file to a unix text file. You can easily do it with a program called `dos2unix`, or in vim, after opening a file, type, `:se ff=unix`, hit return, and then save.
Alok
Aye. As Alok says, `"r"` is for opening text files as defined by the local custom - ie opening Linux text files on Linux, Windows text files on Windows and OS/390 text files on OS/390. If you're going to move text files between PCs, it's up to you to translate them to the native format.
caf
@caf, all my file and operations r all window-typed...you can try my `plus` example.....
Macroideal
If you want help with that specific example, you'll have to post the code that has the problem (preferably, as a new question).
caf
@Macroideal: Shouldn't happen. If I were to guess, maybe that's because you tried to determine file size using `fseek`, and then ignored the return value from `fread`? I am just guessing here. See my answer to your question and http://stackoverflow.com/questions/260235/why-is-my-simple-c-program-displaying-garbage-to-stdout/2076043#2076043 for details.
Alok
...try loading a picture using plain 'r' and displaying it. The corruption resulting from conversion should make the need for 'rb' apparent...
SF.
+1  A: 

use "rb" to open a binary file. Then the bytes of the file won't be encoded when you read them

sagie
+1  A: 

This makes a difference on Windows, at least. See that link for details.

Alex
+3  A: 

On Linux, and Unix in general, "r" and "rb" are the same. More specifically, a FILE pointer obtained by fopen()ing a file in in text mode and in binary mode behaves the same way on Unixes. On windows, and in general, on systems that use more than one character to represent "newlines", a file opened in text mode behaves as if all those characters are just one character, '\n'.

If you want to portably read/write text files on any system, use "r", and "w" in fopen(). That will guarantee that the files are written and read properly. If you are opening a binary file, use "rb" and "wb", so that an unfortunate newline-translation doesn't mess your data.

Note that a consequence of the underlying system doing the newline translation for you is that you can't determine the number of bytes you can read from a file using fseek(file, 0, SEEK_END).

Finally, see What's the difference between text and binary I/O? on comp.lang.c FAQs.

Alok