Write a program which read characters from keyboard and write in a file until user
input “enter key” from keyboard.
#include <stdio.h>
int main(void) {
FILE *fptr;
char ch;
// open the file in write mode
fptr = fopen("username.txt", "w");
// take user input
printf("Enter your name: ");
// keep reading the user input from the terminal
// till Return (Enter) key is pressed
while( (ch = getchar()) != '\n' ) {
// write character ch in file
putc(ch, fptr);
}
fclose(fptr);
return 0;
}
Comments
Leave a comment