I have a text file that holds ID's and names all in one line
314 name1 122 name3 884 name2
the ID is 3 digits (we will deal with it as char
), then we have one space followed by the name which is 8 digits (spaces included), and then comes another person's ID/name and so on...
so I open a the file, read it and want to "slice" it
*the struct
I am about to mention isn't important, just an example, can be found at the bottom
Basically take the first 3 digits and put them in a struct under ID, then ignore the following space, and grab the 8 digits that come after and place them in the same struct under name and so on (can just remove the first 12 digits of the file every time), the problem is simply the slicing
I came from Python where if I have a string
, and only want to keep the first two digits I'd do something like string = string[0-2]
//ProductTree is a struct
char initial_s[sizeof(initialStockFile)];
ProdudctTree* temp = NULL;
fscanf(initialStockFile, "%s", initial_s);
temp = malloc(sizeof(ProductTree));
while( initial_s != '\0' ){
temp->id = initial_s[0-9];
temp->productName = initial_s[10-20];
initial_s = initial_s+21;
temp = temp->right;
}
temp->right = NULL;
return temp;
This is what I have tried but clearly doesn't work
The struct if anyone is curious
typedef struct ProductTree {
char* id; //Product ID number. This is the key of the search tree.
char* productName; //Name of the product.
struct ProductTree *left, *right;
} ProductTree;