Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

2d string arrays

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hello. No definite question here, just looking for experienced users ideas on the best way to read in various types of input from a file (ie. quantity of a product, followed by the product name), and then manipulate this data.

I've only a fundamental knowledge of C, and can't use anything more complicated than, say, 2d string arrays - which are my current solution.

This question is more conceptual than anything else - I'm not looking for huge blocks of code, but if they're forthcoming, I'll be sure to consider them carefully. :)
 
fscanf() or fgets()(in this case use atoi or some other mehtod to convert the quantity into integer) should be fine!
You can have two different pointer arguments. In ur case the first one is an int array to store the quantity and the second is the char array to store the prodcut.

For Ex.
int quantity[100];
char product[100][100];
int row = 0, col = 0;

while(fscanf(fptr,"%d%s",&quantity[row],product[row]) != NULL) {
.
.
.
.
row++;
}

There is no way wherein u can get the entire data in one shot!!

Hope this helps!!

cheers
 
This is greatly dependent on the particular format of the file and what you want to do with it. Generally, fgets() is probably the most straightforward way of reading text files line by line.

If you need to parse each line retrieved by fgets(), check out sscanf(), strtok(), strtol(), strtoul(), strtod() and several other functions in <string.h>.

Error recovery using fscanf() is difficult at best. So, unless the file is in the *exact* format that you expect it to be in (this assumption is never valid if you want your program to be robust), you're probably better off staying away from it.
Russ
bobbitts@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top