Hi all!
I have a txt file that has some strings in the following format:
TAG=MyString
Eg:
Now what I'm doing is search the TAGs in the file and storing the corresponding string associated with the TAG in a global string (1 for every tag) Something like this:
So I have to have like 200 if-else statements to accomplish this task. Is there a shorter way to do this? Also, any tag found at the end of the file will lead to execution of all the strcmp statements before being matched successfully. I dont think this is a good approach. How would you pros do it?
Thanks in advance,
Sid
I have a txt file that has some strings in the following format:
TAG=MyString
Eg:
Code:
FRUIT=I like mangoes CARS=I like red cars . . . PUSHUPS=I can do a zillion non-stop
Code:
#include<stdio.h> char *fill_up_this_string_1; . . . . char *fill_up_this_string_100; int main() { /* Open the file */ while( fgets(current_line, MAX_MYSTRING_SIZE + MAX_TAG_SIZE, txtfile)) { /* Search for the tag in line */ if( strcmp("FRUIT", /*the tag found in the line*/) == 0) { /* malloc memory for fill_up_string_1 */ strcpy(fill_up_string_1, /* Mystring foung in line */) } if( strcmp("CARS", /*the tag found in the line*/) == 0) { /* malloc memory for fill_up_string_2 */ strcpy(fill_up_string_2, /* Mystring foung in line */) } if( strcmp("PUSHUPS", /*the tag found in the line*/) == 0) { /* malloc memory for fill_up_string_3 */ strcpy(fill_up_string_3, /* Mystring foung in line */) } . . . . /* About 200 times */ }
Thanks in advance,
Sid
Comment