/* Tasks - read and write to files 1) create a function to write a string to a file 2) create a function to read from a file and return a string 3) create a function to append a string to a file 4) create a function to that returns whether a file exists or not Practice programs: 1) Add line numbers to a text file e.g. given the lines "Today is Tuesday\nCprE 185" change the text to "1 Today is Tuesday\n2 CprE185" and write the result back to the same file. 2) Backup a text file by adding the lines of the two files together, and separate them by the # sign. (assume both files have the same number of lines) e.g line 1 of file 1 # line 1 of file 2 line 2 of file 1 # line 2 of file 2 ... */ #include #include #define BUFSIZE 1024 //write the string in 'str' to the file specified void writeToFile(char filename[], char str[]) { FILE *fp = NULL; //if the file does not exist yet, it is created and opened for writing. //if the file exists, the contents of the current file are erased and //overwritten with the contents of 'str' fp = fopen(filename, "w"); fputs(str, fp); fclose(fp); } //write the string in 'str' to the file specified void appendToFile(char filename[], char str[]) { FILE *fp = NULL; //if the file does not exist yet, it is created and opened for writing. //if the file exists, the contents of 'str' are appended to the end fp = fopen(filename, "a"); fputs(str, fp); fclose(fp); } //return 1 if file exists, otherwise 0 int doesFileExist(char filename[]) { FILE *fp = NULL; //With the "r" operation to read from a file, the file is NOT created if //it does not already exist. Instead, fp remains equal to NULL. Check for //NUL to determine if the file exists. fp = fopen(filename, "r"); if(fp == NULL) return 0; return 1; } //put contents of the file into 'FILECONTENTS'... //The contents 'FILECONTENTS' are overwritten. void readFile(char filename[], char FILECONTENTS[]) { FILE * fp = NULL; char BUFFER[BUFSIZE]; fp = fopen(filename, "r"); if(fp != NULL) { //to ensure the character array was initialized and that the correct //string is returned FILECONTENTS[0] = '\0'; while( fgets(BUFFER, BUFSIZE, fp) != NULL) { strcat(FILECONTENTS, BUFFER); } fclose(fp); } } //test the four functions void test() { char filename[BUFSIZE]; char BUFFER[BUFSIZE]; //write to a file printf("input the name of the file to write\n"); gets(filename); printf("input the string to write to the file\n"); gets(BUFFER); writeToFile(filename, BUFFER); //read from a file printf("input the name of the file to read\n"); gets(filename); if(!doesFileExist(filename)) { printf("File does not exist."); } else { readFile(filename, BUFFER); printf("Contents of the file:\n%s\n", BUFFER); } //append to a file without creating the file if the file does not exist printf("input the name of the file to append to\n"); gets(filename); if(!doesFileExist(filename)) { printf("File does not exist. Will not create the file in this case."); } else { appendToFile(filename, BUFFER); readFile(filename, BUFFER); printf("The contents of the file are now:\n%s\n", BUFFER); } } //add line numbers to a file //this function gets all of the file contents, then modifies the string, and then //writes the modified string back to the file. void AddLineNumbersToFile_Method1(char filename[]) { char BUFFER[BUFSIZE]; char curline[BUFSIZE]; char modified[BUFSIZE]; int linecount=2; //start lines at 1 int i, idx=0; if(!doesFileExist(filename)) { printf("%s does not exist. Could not modify.", filename); return; } //get all of the file contents readFile(filename, BUFFER); //modify the string if(strlen(BUFFER) > 0) { modified[idx++] = '1'; modified[idx++] = ' '; for(i=0; i