Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write to a file using fputs in C

Tags:

c

string

file

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);
like image 942
Exon Avatar asked Sep 11 '17 09:09

Exon


People also ask

How do I write to a file using Fputs?

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.

Is there Fputs in C?

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);

What is Fputs function in C?

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' ).

What is the syntax of Fputs?

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)


2 Answers

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);
like image 152
Stephan Lechner Avatar answered Oct 07 '22 17:10

Stephan Lechner


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 an EOF marker. If there is no EOF, you must use an intervening call to a file positioning function. The file positioning functions are fsetpos, fseek, and rewind. When you switch from writing to reading, you must use an intervening call to either fflush or to a file positioning function.

like image 36
Weather Vane Avatar answered Oct 07 '22 17:10

Weather Vane