Could someone tell me why the file doesn't change? It works when I use rewind
or fseek
but not otherwise.
What's the standard way of using fputs
after fgets
. The file indicator is at position 9 so fputs
must write after that, but it doesn't do anything.
In file:
abcd efgh ijkl mnor
In source code:
char c;
char str[15];
FILE *fp = fopen("d:\\data.txt","r+");
fgets(str, 10, fp);
// fseek(fp, 9, SEEK_SET);
// rewind(fp);
printf("%d\n", ftell(fp));
// ftel shows that it's in "9".
printf("%s", str);
fputs(str, fp);
// why its not working
fclose(fp);
fputs() writes a single line of characters in a file. fputs(const *char str, FILE *fp); where str is a name of char array that we write in a file and fp is the file pointer.
fputs() is a standard C library function that is used to write a string of characters to a file at the location indicated by the file pointer. Below is the declaration of the fputs() function: int fputs (const char * str, FILE * stream);
fputs is a function in C programming language that writes an array of characters to a given file stream. fputs stands for file put string. It is included in the C standard library header file stdio. h . The function fputs terminates after reaching terminating null character ( '\0' ).
Writing File : fputs() function The fputs() function writes a line of characters into file. It outputs string to a stream. Syntax: int fputs(const char *s, FILE *stream)
Regarding the definition of fopen/'+'
in the C standard (e.g. as in this online C standard draft), switching from reading to writing requires an intermediate call to a file positioning function (emphasis are mine):
7.21.5.3 The fopen function
(7) When a file is opened with update mode ('+' as the second or third character in the above list of mode argument values), both input and output may be performed on the associated stream. However, output shall not be directly followed by input without an intervening call to the fflush function or to a file positioning function (fseek, fsetpos, or rewind), and input shall not be directly followed by output without an intervening call to a file positioning function, unless the input operation encounters end- of-file. Opening (or creating) a text file with update mode may instead open (or create) a binary stream in some implementations.
So I'd suggest you write the following code to overcome your problem:
fseek ( fp , 0, SEEK_CUR);
fputs(str, fp);
The MS documentation for fopen
says this:
When the
"r+"
,"w+"
, or"a+"
access type is specified, both reading and writing are enabled (the file is said to be open for "update"). However, when you switch from reading to writing, the input operation must encounter anEOF
marker. If there is noEOF
, you must use an intervening call to a file positioning function. The file positioning functions arefsetpos
,fseek
, andrewind
. When you switch from writing to reading, you must use an intervening call to eitherfflush
or to a file positioning function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With