So I'm in a C++ class (mostly taught in C though) and I'm falling behind I guess because I'm completely lost on how to do my project.
I need to be able to read in a .dat file, some how take the data out of it (without using an array) then write it to a .txt file in the format he wants (table with mean, range, average, ect. of the numbers in .dat).
He gave us some code to use for reading the .dat but I can't get it to work. I put it in my project and when I type the name of the .dat file (in this case one.dat) it says could not read, closing. I put the one.dat into the debug folder with the .exe and it still can't read the damn file.
I also don't know how to write to the .txt after I read the info in and do all the math I need to do.
Using Visual Studio C++ Express 2010 if that makes a difference.
link to project encase you don't know what I'm looking for:
http://faculty.edcc.edu/paul.bladek/CS131/p2.htm
Code he gave us for read:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <conio.h>
/*
*/
int main(int argc, char *argv[])
{
FILE * inFileHandle = NULL;
FILE * outFileHandle = NULL;
char filename[FILENAME_MAX];
char c = ' ';
if(argc > 1)
{
strncpy(filename, argv[1], FILENAME_MAX);
}
else
{
puts("Enter the name of the file to read (source):");
fgets(filename, FILENAME_MAX, stdin);
if(filename[strlen(filename) - 1] == '\n')
filename[strlen(filename) - 1] = '\0';
else
while(getchar() != '\n')
;
}
inFileHandle = fopen(filename, "r");
if(inFileHandle == NULL)
{
printf("Could not open file %s for input.\n"
"Press any key to Continue", filename );
getch();
return EXIT_FAILURE;
}
if(argc > 2)
{
strncpy(filename, argv[2], FILENAME_MAX);
}
else
{
puts("Enter the name of the file to write (output):");
gets(filename); // not safe! (potential buffer overflow)
}
outFileHandle = fopen(filename, "a");
if(outFileHandle == NULL)
{
fclose(inFileHandle);
printf("Could not open file %s for output.\n"
"Press any key to Continue", filename );
getch();
return EXIT_FAILURE;
}
while((c = (char)getc(inFileHandle)) != EOF)
{
if(c == ' ')
c = '\n';
putc(c, outFileHandle);
}
fclose(inFileHandle);
fclose(outFileHandle);
puts("\nPress any key to Continue");
getch();
return EXIT_SUCCESS;
}
|
Obviously not looking for to have it done for me, just really need some help and explanation as to what I'm doing with all this.