//* The idea of configuration file *

#ifdef _MSC_VER // for MS Visual Studio compilers - to allow "classic" insecure printf and scanf #define _CRT_SECURE_NO_WARNINGS #endif #include <stdio.h> #include <ctype.h> // tolower(.), toupper(.) void chomp(char str[]) { while( *str!='\0' && *str!='\n') ++str; *str='\0'; } int main() { const char ConfigFileName[] = "config.txt"; FILE *logfile; char name[80]; char email[80]; char ans = 'y'; logfile = fopen(ConfigFileName, "r"); if ( logfile!=0 ) { fgets(name, 80, logfile); chomp(name); fgets(email, 80, logfile); chomp(email); // do not use complex formatting with fscanf // because of unpredictable position in file // don't: fscanf(logfile, " %79[^\n]s\n", name); // don't: fscanf(logfile, " %79[^\n]s\n", email); fclose(logfile); printf("Hello %s <%s>!\n", name, email); printf("Do you want to change the name or email? (y/n) "); scanf(" %c", &ans); while( getchar()!='\n' ) {} // this loop disposes of the reminder of the line // to prevent reading it by the next scanf of an entire line } if ( tolower(ans)!='n' ) { printf("What is your name please? "); scanf(" %79[^\n]s\n", name); printf("What is your email please? "); scanf(" %79[^\n]s\n", email); logfile = fopen(ConfigFileName, "w"); if ( logfile!=0 ) { fprintf(logfile, "%s\n%s\n", name, email); fclose(logfile); } // else ?? } printf("Hello %s!\n", name); // .. the actual program return(0); }