C11 A working draft of the N1570 standard said that on page 306 in the section "7.21.5.3 fopen function section"
Opening a file with adding mode ( 'a' as the first character in the mode argument) causes all subsequent entries in the file to be forcibly transferred to the current end-of-file, regardless of incoming calls to fseek ....
Can anyone confirm that not only the fseek function, but also another file positioning function, such as fsetpos and rewind , cannot interfere with the add mode?
Well, the standard on page 338 said that rewinding does the same thing with (void) fseek (stream, 0L, SEEK_SET) , except that the error indicator for the stream is also cleared. But as far as I read on page 337, the standard does not state that fsetpos is similar to fseek.
Source code to illustrate this problem.
#include <stdio.h> // Declare the File Pointer FILE *fp; // Declare the test integer variable int TEST; // declare the test position variable for fsetpos test fpos_t POSITION; void WriteData(int choice){ // Clear the file content fp = fopen("test.bin", "wb"); fclose(fp); // Reopen the file with ab fp = fopen("test.bin", "ab"); // Initialize the test integer variable TEST = 100; // Write five sample data int i; for(i = 1; i <= 5; i++) { fwrite(&TEST, sizeof(TEST), 1, fp); // If two data were written then save the position if( i == 2 ) fgetpos(fp, &POSITION); } // Change the data TEST = 54321; // Declare the test case switch(choice){ case 1 : fseek(fp, (long) 2*sizeof(TEST), SEEK_SET); break; case 2 : rewind(fp); break; case 3 : fsetpos(fp, &POSITION); break; } // Write the data again fwrite(&TEST, sizeof(TEST), 1, fp); // Close the file fclose(fp); } void ReadData(){ // Open the file for read fp = fopen("test.bin", "rb"); printf("\n [OUTPUT]"); // while the data can be read then print it to the console while( fread(&TEST, sizeof(TEST), 1, fp) == 1) printf("\n %d", TEST); // Close the file fclose(fp); } int main(){ /* Test Case Process */ printf("\n\n Intervene using fseek(fp, (long) 2*sizeof(TEST), SEEK_SET);"); WriteData(1); ReadData(); printf("\n\n Intervene using rewind(fp);"); WriteData(2); ReadData(); printf("\n\n Intervene using fsetpos(fp, &POSITION);"); WriteData(3); ReadData(); return 0; }
Desired Result:
Intervene using fseek(fp, (long) 2*sizeof(TEST), SEEK_SET); [OUTPUT] 100 100 100 100 100 54321 Intervene using rewind(fp); [OUTPUT] 100 100 100 100 100 54321 Intervene using fsetpos(fp, &POSITION); [OUTPUT] 100 100 100 100 100 54321
If the result was such, then the standard confirms that not only the fseek function, which cannot intervene in subsequent entries, but also rewind and fsetpos . Otherwise, if the result was not so, the standard does not confirm it.
I tested it on windows 10 using tdm-gcc 4.9.2 and on Ubuntu 16.04 gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1 ~ 16.04.4). As a result, three of them cannot intervene, but I'm not sure of another platform whether it can intervene or not.
Finally, if I can ask my question again. The standard also confirms that not only the fseek function, which cannot interfere with subsequent recordings, but also rewind and fsetpos ?
If the standard confirms , then please explain where the operator is located that indicates the confirmation.
Otherwise, if the standard does not confirm , then please explain where the statement indicating failure is indicated.
About my question, what I need to know is certainty , that the standard confirms that the author of the compiler must do it exactly or do it, etc., to confirm the standard, no more