Hey everyone,
I am trying to read every line in a text file and write it to a binary file.
Each line has a string without spaces followed by a tab and a non-negative number, such as:
a 74
abc 12
thisisastring 4192
There are some restrictions in writting the binary file but I d rather not go into any details.
My question is how do I make the struct create a char * of length "string" and not of length "line". "line" being each individual line (ie: abc 12)
When I try declaring "string" before the struct definition the compiler complaints.
The structs are to be of different size inside the binary file, that s why I cant just set the string variable to something like char text[255]
Thanks
Code:
while(fgets(line, MAX_LEN, finp) != NULL) { //read a line from the text file
struct node {
unsigned char stringLen;
char text[strlen(line)]; //how do i make this text[strlen(string)]
int number;
};
typedef struct node MyNode;
MyNode dummy;
string = (char *)strtok(line,"\t"); //holds just the string of chars
numberString = (char *)strtok(NULL, " \n\t"); //holds the number
dummy.stringLen = strlen(string); //length of the string of chars
num = atoi(numberString); //convert to int
dummy.number = num;
strcpy(dummy.text, string);
fwrite((const void *) &dummy, sizeof(MyNode), 1, fop); //write to binary file