I have been trying to read input from a text file and print it to the console but it doesn't print anything out. My latest 2 attempts are at the bottom.
Example of the text document contents:
#include <stdio.h>; #include <math.h>; int main() { while(1){ int gal, miles, mpg; char read[10]; puts("Gallons filled:"); scanf("%i", &gal); puts("Miles on tank:"); scanf("%i", &miles); mpg = miles/gal; time_t now = time(NULL); struct tm *t = localtime(&now); char date_time[30]; strftime( date_time, sizeof(date_time), "%x", t); printf( "\n%s\n",date_time ); printf("Gallons: %0.1i\n",gal); printf("Miles: %0.1i\n",miles); printf("MPG: %0.1i\n\n\n",mpg); FILE *ofp; ofp = fopen("FuelLog.txt", "r"); fprintf(ofp, "\n\n%s\n%i\n%i\n%i", date_time, gal, miles,mpg); fclose(ofp); char filename[15] = "FuelLog.txt"; if (ofp){ ofp = fopen("FuelLog.txt", "a"); fprintf(ofp, "\n\n%s\n%i\n%i\n%i", date_time, gal, miles,mpg); fclose(ofp); char filename[15] = "FuelLog.txt"; } else { ofp = fopen("FuelLog.txt", "w"); fprintf(ofp, "%s\n%i\n%i\n%i", date_time, gal, miles,mpg); fclose(ofp); } char c[100]; /* ##### Attempt 1 * * if (ofp == NULL) perror ("Error opening file"); else { puts("test22"); if ( fgets (c , 101 , ofp) != NULL ) puts("test"); puts (c); fclose (ofp); } // return 0; */ // ##### Attempt 2 //int i; strcpy(c, ofp); //strcpy(c, scanf(ofp, "%s")); //for(i=0; ferror(ofp) == 0; i++){ //fscanf(ofp, "%s", c); printf("%s\n test",c); } return 0; // } return 0; }
Example of the text document contents:
01/29/13 1 2 2 01/29/13 1 2 2 01/29/13 1 2 2 01/31/13 2 88 44