Header file:
stdio.h
Synopsis:
int fputs(const char *str, FILE *stream);
Description:
It writes the string str on stream. Returns non-negative integer on success or EOF on failure.
fputs function C example:
#include<stdio.h>
#include<string.h>
int main() {
char str[100];
FILE *fp;
fp = fopen("file.txt", "w");
printf("Enter your input(type quit to exit):\n");
while(1) {
fgets(str, 100, stdin);
// str[strlen(str) -1] = '\0';
fputs(str, fp);
if(strcmp(str, "quit\n") == 0)
#include<string.h>
int main() {
char str[100];
FILE *fp;
fp = fopen("file.txt", "w");
printf("Enter your input(type quit to exit):\n");
while(1) {
fgets(str, 100, stdin);
// str[strlen(str) -1] = '\0';
fputs(str, fp);
if(strcmp(str, "quit\n") == 0)
break;
}
fclose(fp);
return 0;
}
}
fclose(fp);
return 0;
}
Output:
jp@jp-VirtualBox:~/cpgms/exp$ ./a.out
Enter your input(type quit to exit):
Hello world
Hello world
Hello world
quit
jp@jp-VirtualBox:~/cpgms/exp$ cat file.txt
Hello world
Hello world
Hello world
Enter your input(type quit to exit):
Hello world
Hello world
Hello world
quit
jp@jp-VirtualBox:~/cpgms/exp$ cat file.txt
Hello world
Hello world
Hello world
No comments:
Post a Comment