The use of "r+" in fopen on windows vs linux

Asked
Active3 hr before
Viewed126 times

8 Answers

90%

When the "r+", "w+", or "a+" access type is specified, both reading and writing are allowed (the file is said to be open for "update"). However, when you switch between reading and writing, there must be an intervening fflush, fsetpos, fseek, or rewind operation. The current position can be specified for the fsetpos or fseek operation, if desired.,So, you should always call fseek() (as, eg. fseek(..., 0, SEEK_CUR)) when switching between reading and writing from a file., 3 I'm not sure if that's the issue, but you might need to call fseek when switching between reading and writing from the file – Hasturkun Feb 14 '13 at 15:48 ,EDIT: Seems even at Microsoft's site they say "r+" should open for reading and writting. They also made this note:

Before performing output after input, an fflush() isn't any good - you need to perform a seek operation. Something like:

fseek(fp, ftell(fp), SEEK_SET); // not fflush(fp);
88%

I was toying around with some code which was opening, reading, and modifying a text file. A quick (simplified) example would be:,EDIT: Seems even at Microsoft's site they say "r+" should open for reading and writting. They also made this note:,So, you should always call fseek() (as, eg. fseek(..., 0, SEEK_CUR)) when switching between reading and writing from a file.,Am I missing something obvious, or is the fact that I get no errors, but also no output just an undocumented "feature" of using r+ with fopen() on Windows?

I was toying around with some code which was opening, reading, and modifying a text file. A quick (simplified) example would be:

#include <stdio.h>
int main()
{
    FILE * fp = fopen("test.txt", "r+");
    char line[100] = {''};
    int count = 0;
    int ret_code = 0;
    while(!feof(fp)){
        fgets(line, 100, fp);
        // do some processing on line...
        count++;
        if(count == 4) {
          ret_code = fprintf(fp, "replaced this linen");
          printf("ret code was %dn", ret_code);
          perror("Error was: ");
        }
    }
    fclose(fp);
    return 0;
}

On Linux my file has full permissions:

-rw - rw - rw - 1 mike users 191 Feb 14 10: 11 test.txt

So I tried:

        ...
        if (count == 4) {
           fflush(fp);
           ret_code = fprintf(fp, "replaced this linen");
           fflush(fp);
           printf("ret code was %dn", ret_code);
           ...
load more v
72%

Difference in the fopen modes r+, rw+ and w+ in PHP,r+: Opens a file in read and write mode. File pointer starts at the beginning of the file.,How to calculate the difference between two dates in PHP?,rw+: Opens a file in read and write mode. File pointer starts at the beginning of the file. This mode does not exists in the PHP documentation but it works well.

Syntax:

$<variable_name> = fopen(<file source path>,<access mode>)
load more v
65%

For details of in-depth Linux/UNIX system programming training courses that I teach, look here. , HTML rendering created 2021-08-27 by Michael Kerrisk, author of The Linux Programming Interface, maintainer of the Linux man-pages project. ,Copyright and license for this manual page

FOPEN(3) Linux Programmer 's Manual               FOPEN(3)
load more v
75%

fopen supports Unicode file streams. To open a Unicode file, pass a ccs flag that specifies the desired encoding to fopen, as follows.,Opens a file. More-secure versions of these functions that perform additional parameter validation and return error codes are available; see fopen_s, _wfopen_s.,The character string mode specifies the kind of access that is requested for the file, as follows.,BOM detection only applies to files that are opened in Unicode mode (that is, by passing the ccs flag).

Syntax

FILE * fopen(
   const char * filename,
      const char * mode
);
FILE * _wfopen(
   const wchar_t * filename,
      const wchar_t * mode
);
load more v
40%

On the Windows platform, be careful to escape any backslashes used in the path to the file, or use forward slashes. , The optional third use_include_path parameter can be set to '1' or true if you want to search for the file in the include_path, too. , The default translation mode is 'b'. You can use the 't' mode if you are working with plain-text files and you use \n to delimit your line endings in your script, but expect your files to be readable with applications such as old versions of notepad. You should use the 'b' in all other cases. , Windows offers a text-mode translation flag ('t') which will transparently translate \n to \r\n when working with the file. In contrast, you can also use 'b' to force binary mode, which will not translate your data. To use these flags, specify either 'b' or 't' as the last character of the mode parameter.

load more v
22%

The fopen() function shall open the file whose pathname is the string pointed to by filename, and associates a stream with it.,The fopen() function shall fail if:,The following example tries to open the file named file for reading. The fopen() function returns a file pointer that is used in subsequent fgets() and fclose() calls. If the program cannot open the file, it just ignores it.,The mode argument points to a string. If the string is one of the following, the file shall be opened in the indicated mode. Otherwise, the behavior is undefined.

The following example tries to open the file named file for reading. The fopen() function returns a file pointer that is used in subsequent fgets() and fclose() calls. If the program cannot open the file, it just ignores it.

#include <stdio.h>
...
FILE *fp;
...
void rgrep(const char *file)
{
...
    if ((fp = fopen(file, "r")) == NULL)
        return;
...
}

60%

Opens a file to update both reading and writing. The file must exist.,Creates an empty file for both reading and writing.,Appends to a file. Writing operations, append data at the end of the file. The file is created if it does not exist.,Opens a file for reading and appending.

Following is the declaration for fopen() function.

FILE * fopen(const char * filename,
   const char * mode)
load more v

Other "undefined-undefined" queries related to "The use of "r+" in fopen on windows vs linux"