-
Data file question
Hi folks :).
For the second part of my assignment at university, I need to start storing values in a file.
Basically, I'm not too keen on posting my code as the assignment is still active, and I'm sure other people will end up finding this place when looking for assistance with their code. I've still got the best part of a month to get this finished, so expect a flurry in 3-4 weeks time ;).
I need to store the contents of a multidimensional array in the file, and a couple of other variables. I'm just wondering whether anyone can give me any pointers as to where I should start? I've had a look through a couple of books and the file examples on this site, but they seem to be geared toward just reading files, rather than reading individual values from the file.
My file needs to contain the following:-
A list of distances, to be read and stored in a multidimensional array - possibly edited and then saved back out to the file.
A cost per mile value, possibly edited and then possibly saved out to the file again.
I don't want any routines from people on here, all I'd like are a few hints as to the best way to store the data in the file, and a hint as to how I could read the data into the values within my code :).
Sorry, I guess my post is fairly babbling. I'll return to my little rock now :).
Thanks in advance!
--Rich
-
>I'm not too keen on posting my code
Then it will be hard to help you with specific code problems later.
>A list of distances, to be read and stored in a multidimensional array
Fixed size or variable? If it's fixed then the program will be easy:
Code:
/*
Assuming file formatted like so:
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
15 16 17 18 19
20
*/
FILE *in;
double dist[M][N];
double cpm;
int i, j;
/* Open the file for reading */
for ( i = 0; i < M; i++ ) {
for ( j = 0; j < N; j++ )
fscanf ( "%lf", &dist[i][j] );
}
fscanf ( "%lf", &cpm );
/* Close the file */
/* Make changes */
/* Open the file for writing */
for ( i = 0; i < M; i++ ) {
for ( j = 0; j < N; j++ )
fprintf ( "%f ", dist[i][j] );
fprintf ( "\n" );
}
fprintf ( "\n%f\n", cpm );
/* Close the file */
-
Thanks for that :). I'll post my code if I need specific help - though I'm keen to avoid it, if at all possible.
Regarding formatting the file data, the data should have a static number of values. But I'd like to go slightly above and beyond, and create it so further values can be added at a later date. This will mess up the reading of the file when using the above template - is it possible to create the data file with a data structure similar to below?
Code:
Array = 1 3 67 32 6546 454 2323 6565 etc etc 4543 22 78 5
Cost = 100
I'm not too sure if that is understandable or not... it's been a long day :).
Thanks for your help :).
--Rich
-
>is it possible to create the data file with a data structure similar to below?
Of course, but you will need to consider both reading and writing the "Array = " and "Cost = " parts properly. Since you want to allow for a variable length matrix, marking the beginning of new data is a good thing, but complicates your parsing algorithm. But before getting into that, write the simplest program that works and meets the requirements. Then you can make it better. :)
-
Fair point, I'll have a crack at implementing it later today :). Thanks for your thoughts.
--Rich