#include #include // This example prompts the user 3 times for a name and then writes them to myfile.txt // each one in a line with a fixed length (a total of 19 characters + newline). // Two format tags are used: // %d : Signed decimal integer // %-10.10s : left-justified (-), minimum of ten characters (10), // maximum of ten characters (.10), string (s). int main() { FILE *pFile; int n; char name[100]; char *c; pFile = fopen("myfile.txt","w"); for (n=0 ; n<3 ; n++) { puts("please, enter a name: "); fgets(name, sizeof(name), stdin); // fgets includes the newline (i.e., \n) in the name // we replace the \n with \0 to terminate the string in // the array name c = strrchr(name, '\n'); if (c != NULL) *(c) = '\0'; fprintf(pFile, "Name %d [%-10.10s]\n", n, name); } fclose(pFile); return 0; }