/* * filew1.c */ #include #include int main(void) { FILE * fp; // declare file pointer: fp char cc; // declare int: cc 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 fp=fopen(fname, "w" ); // Open file for writing, and assing handle to file pointer if( fp == NULL) // end when fp contains NULL address. { printf("Can't open file %s \n", fname ); // print error exit(1); // exit the program.... but don't use return } printf("Writing from the keyboard to the %s file. \n", fname); while( (cc=getchar()) != EOF ) // read from stdin // assing to cc // while cc not EOF, // do block { fputc(cc,fp); // put value at cc into file handle fp } fclose(fp); // close the file handle printf("File %s is written and chlosed. \n", fname); return 0; }