/* file_I_O.c demonstrates file I/O * BJ Furman 15OCT2012 */ #include #include int main(void) { int i, j, k, num_elem; double x[20], y[20], z[20]; FILE *infile, *outfile; /* Open the file we want to read from. Simply putting the filename.type * between double quotes assumes that the file to open resides in the * SAME directory where this program resides. If the file you want to read * from resides somewhere else, you'll need to put the full file path between * the double quotes. An example of this is given below for the output file. */ infile = fopen ("C6_3_in.txt","r"); //infile = fopen ("C:/Users/Buff/Documents/SJSU/Course related/ME 30/Lecture/C6_3_in.txt", "r"); //infile = fopen ("C:\\Users\\Buff\\Documents\\SJSU\\Course related\\ME 30\\Lecture\\C6_3_in.txt", "r"); /* Check that file exists and is opened properly */ if(infile == NULL) { printf("Error: can't open file to read\n"); return (1); } else printf("File C6_3_in.txt opened successfully to read\n"); /* Open the file that we want to write to. Putting "filename.type" * means that the file will be created in the SAME directory * where this program resides. If you want to put the file in a different * location, give the complete file path between the double quotes, which * you can find using Windows Explorer (Navigate to the directory you want, and * left-click in the URL bar at the top of the window.) * For example, the path to my Desktop is, C:\Users\Buff\Desktop * The backslashes will need to be 'escaped' themselves, so that they are not * confused with escape sequences or use a forward slash /. So the tweaked path is: * "C:\\Users\\Buff\\Desktop\\C6_3_out.txt" or "C:/Users/Buff/Desktop/C6_3_out.txt" */ outfile = fopen ("C:\\Users\\Buff\\Desktop\\C6_3_out.txt","w"); /* Check that file exists and is opened properly */ if(outfile == NULL) { printf("Error: can't open file to write\n"); return 1; } else printf("C6_3_out.txt opened successfully to write\n"); /* Read the first row. k captures the number values read in by fscanf() */ k = fscanf(infile,"%lf %lf",&x[0],&y[0]); /* Print the number of values read in by fscanf() */ fprintf (outfile, "k = %d\n",k); /* Print the value of EOF just so you can see what it is */ fprintf (outfile,"Value of EOF = %d\n", EOF); i = 1; // row counter variable /* Get the rest of the values in the input file and print them out */ while ( fscanf(infile,"%lf %lf",&x[i],&y[i]) != EOF) { printf("x[%d] == %lf y[%d] == %lf\n", i,x[i],i,y[i]); i++; } num_elem = i; fprintf(outfile," x[i] y[i] z[i]\n"); /* Do some math on the values */ for (j=0;j