/* * filew3.c */ #include int main(void) { FILE * fp; // declare file pointer: fp char ch_string[21]; // declare int array: ch_string char fname[15]; // declare char array of 15 elements printf("Enter the name of a file to write:");// prompt for input scanf("%s", fname); // read sting into array fname getchar(); // clear newline from input buffer if( (fp=fopen(fname, "w")) == NULL ) // Open file for writing, // assing handle to file pointer // If address is NULL, do block { printf("Can't open file %s \n", fname ); // print error return(1); // exit the program.... but don't use return } printf("Writing from the keyboard to the %s file. \n", fname); while( (fgets(ch_string,21,stdin)) ) // read from stdin until newline/EOF/'\0' // assing to array ch_string // if ch_string does not contain // do block { fputs(ch_string,fp); // write array to file handle at fp until newline fputs("\n",fp); // write newline to file handle at fp. } fclose(fp); // close the file handle printf("File %s is written and chlosed. \n", fname); return 0; }