Header file:
stdio.h
Synopsis:
char *gets(char *str);
Description:
Gets the given input string into the character array str. Returns str on success. Otherwise, EOF or NULL is returned.
gets function C example:
#include<stdio.h>
#include<string.h>
int main() {
char str[100];
FILE *fp;
fp = fopen("input.txt", "w");
printf("Enter your input(type quit to exit):");
while(1) {
gets(str);
fputs(str, fp);
fputc('\n', fp);
if(strcmp(str, "quit") == 0)
break;
}
fclose(fp);
return 0;
}
#include<string.h>
int main() {
char str[100];
FILE *fp;
fp = fopen("input.txt", "w");
printf("Enter your input(type quit to exit):");
while(1) {
gets(str);
fputs(str, fp);
fputc('\n', fp);
if(strcmp(str, "quit") == 0)
break;
}
fclose(fp);
return 0;
}
Output:
jp@jp-VirtualBox:~/cpgms/exp$ ./a.out
Enter your input(type quit to exit):
Hello World
This is fgets example in C
quit
jp@jp-VirtualBox:~/cpgms/exp$ cat input.txt
Hello World
This is fgets example in C
Enter your input(type quit to exit):
Hello World
This is fgets example in C
quit
jp@jp-VirtualBox:~/cpgms/exp$ cat input.txt
Hello World
This is fgets example in C
No comments:
Post a Comment