Hello I was seeing if anybody could help me with the programming of this project...
I want to scan in data information into my program, the data file is in one file with every bit of information separated by a comma,
how can i set up a while and for loop so each bit of information will get stored in its proper location then when it comes upon a comma it will to another array
this is what i have
Ok, so put ch into an array, and keep track of where you're at in the array with some sort of counter.
OK there are at least two ways you can approach this, with binary data files or textual data files. Each has its pros and cons.
Binary data files are probably the easiest and more fool-proof ways of dealing with data files:
So the upside of a binary file is that its easy to read the records in a failsafe manner. The downside is that it is harder to read/edit the datafile by hand if need be.
// this is what the record might look like:
// pretend it is an employee or a student
// assuming the records are all uniform size we can figure out the number
// of records in the data file....
// the file is already open...
fseek (pFile, 0, SEEK_END);
int nRecordCount = size / sizeof(struct TMyDataRecord);
fseek(pFile, 0, SEEK_BEGIN);
for(nCount = 0; nCount < nRecordCount; nCount++)
struct TMyDataRecord newRecord;
fread(&newRecord, 1, sizeof(struct TMyDataRecord)); // read whole record in one go
// stash the record somewhere, in an array or whatever
Text files on the other hand can be hand-written if need be; the downside is that they can be more problematic to read.
Probably what I would do is group each block of data into records, delimiting them with carriage returns so the above would involve a data file that looked (roughly) like:
In which case you would peel off each record by seeking to the first (next) '\n', then parse each line read by seeking out ','...and as you may have noticed, you hit hard part #1 with the text file as data file paradigm: handling delimiters when they are embedded in the quotes. This further lends weight to the argument of the ease of use of the binary data file where you don't have to worry about it...
21,"Joe Smith", "121 Fooby Lane",
24, "Jane Doe", "4545 Hacienda Street, apt 5",