C Function Fputs
## C Library Function - fputs()
The C library function `int fputs(const char *str, FILE *stream)` writes a null-terminated string to the specified stream. It does not write the terminating null character (`'\0'`) to the destination.
---
## Declaration
Below is the standard declaration for the `fputs()` function, defined in the `` header file:
```c
int fputs(const char *str, FILE *stream);
```
---
## Parameters
* **`str`** -- This is a pointer to a null-terminated character array (string) that you want to write to the stream.
* **`stream`** -- This is a pointer to a `FILE` object that identifies the output stream where the string will be written (e.g., a file pointer or `stdout`).
---
## Return Value
* On **success**, the function returns a non-negative value.
* On **failure**, the function returns `EOF` (End of File) and sets the error indicator for the stream.
---
## Code Examples
### Example 1: Writing Strings to a File
The following example demonstrates how to use `fputs()` to write text to a file.
```c
#include
int main()
{
FILE *fp;
// Open file for writing and reading
fp = fopen("file.txt", "w+");
if (fp == NULL) {
perror("Error opening file");
return -1;
}
// Write strings to the file using fputs
fputs("This is C programming language.\n", fp);
fputs("It is a powerful system programming language.\n", fp);
// Close the file
fclose(fp);
return 0;
}
```
**Output:**
If you compile and run the program above, it will create a file named `file.txt` in the working directory with the following content:
```text
This is C programming language.
It is a powerful system programming language.
```
---
### Example 2: Reading and Displaying the File Content
The following program demonstrates how to open the file created in the previous example and read its contents character by character using `fgetc()`.
```c
#include
int main()
{
FILE *fp;
int c;
// Open the file in read-only mode
fp = fopen("file.txt", "r");
if (fp == NULL) {
perror("Error opening file");
return -1;
}
// Read and print characters until the end of the file is reached
while (1)
{
c = fgetc(fp);
if (feof(fp))
{
break;
}
printf("%c", c);
}
// Close the file
fclose(fp);
return 0;
}
```
---
## Considerations & Best Practices
### 1. `fputs()` vs. `puts()`
* **Newline Character:** Unlike `puts()`, which automatically appends a newline character (`\n`) to the output, `fputs()` writes the string exactly as-is. If you want a newline when using `fputs()`, you must explicitly include `\n` in your string.
* **Stream Flexibility:** `puts()` always writes to standard output (`stdout`), whereas `fputs()` allows you to specify any output stream, including files (`fp`), standard output (`stdout`), or standard error (`stderr`).
### 2. Null-Terminator Behavior
`fputs()` expects a null-terminated string (`\0`). It uses the null character to determine where the string ends, but it does **not** write the null character itself to the output stream.
### 3. Error Handling
Always check the return value of `fputs()`. If it returns `EOF`, it indicates that a write error occurred. You can use `ferror()` to further diagnose stream errors.
YouTip