/* Author : Michael Robinson Program : files.c Purpose : To open, read, write and close files, and present the if, else if, and else commands Updated : Jul 1, 2014 Purpose : - opens and reads data from an existing file (input) - creates an output file (output) - writes data from input file into output file - closes both files */ #include #include #include FILE *inp; // pointer to the file inp FILE *outp; // pointer to the file outp float miles = 0; char bases = '\0'; //To pause program and ask use to press enter to continue. void pause() { printf("Press any key to continue ...\n") ; getchar() ; } void writePa01Backwards() { inp = fopen("pa01Probes.dna", "r"); int amtOfRecords = 0; char array[1500]; char record[50]; while (fgets(record, 100, inp) != NULL) { printf("%s", record); strcpy(&array[ amtOfRecords ], record); // printf( "%s", &array[amtOfRecords] ); amtOfRecords++; pause(); } outp = fopen("backwards.dna", "w"); int x = 0; for(x = amtOfRecords -1 ; x > -1; x--) { //fprintf( outp, "%s\n", array[x] ); printf( "[%s]\n", &array[x] ); printf( "%s", &array[amtOfRecords] ); } /* out = fopen("reverse.txt", "w"); //Prints in reverse onto the file for( i = x; i > -1; i--) { fprintf(out, "%c\n", array[i]); } */ } void read1200dna() { inp = fopen("distance.txt", "r"); } void readAndWriteEntireFile() { inp = fopen("distance.txt", "r"); int record = 0; while(fscanf(inp,"%f", &miles) != EOF) { printf("Record %d = %.2f\n", record++, miles); } printf("\n"); fclose(inp); } void readTextFileAndCompute() { inp = fopen("distance.txt", "r"); //open file for reading outp = fopen("distance.out", "w"); //open file for writting while( fscanf(inp,"%f", &miles) != EOF) { fprintf(outp, "The distance in miles is %.2f\n", miles); //write data to outp file pointer printf("The distance in miles is %.2f\n", miles); //display data } fclose(inp); //close inp fclose(outp); //close outp inp = fopen( "1200.dna", "r" ); fscanf(inp, "%s", &bases); //printf("\ndna = %s\n\n", bases); }//end void readTextFileAndCompute() int main() { readTextFileAndCompute(); pause(); readAndWriteEntireFile(); pause(); read1200dna(); pause(); writePa01Backwards(); exit(0); }