How to parse a file and store few strings in an array
-
Try this:
BOOL bCapture = FALSE; // flag to ignore unwanted tokens
while(fgets(buf, bufsize, fp1) != NULL)
{
tok = strtok(buf,"{ ,}");
if (tok != NULL)
{
if (bCapture == FALSE)
{
// not capturing yet, so check for start of list
if (strcmp(tok, "EXEC") == 0)
bCapture = TRUE;
}
else // bCapture == TRUE
{
// bCapture is true so check for end of
// list, or display token
if (strcmp(tok, "END-EXEC") == 0)
bCapture = FALSE;
else
printf("Token: %s\n", tok);
}
}
}[edit]Fixed the "END-EXEC" string, thanks to Carlo for pointing it out.[/edit]
Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman
-
My 5, though your code, as it stands, wouldn't match the-requirements. :-D
Veni, vidi, vici.
I wrote it over breakfast; what's missing?
Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman
-
I wrote it over breakfast; what's missing?
Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman
-
Nothing is missing, however an underscore is not an hyphen. --Carlo The Nitpick.
Veni, vidi, vici.
CPallini wrote:
Carlo The NitpickEagle-Eye
FTFY :)
Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman
-
CPallini wrote:
Carlo The NitpickEagle-Eye
FTFY :)
Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman
That code was a beauty again .... :-D Actually... the final file after strtok() looks like the one below... :|
Token: DCLGEN
Token: LIBRARY
Token: ACTION
Token: LANGUAGE
Token: NAMES
Token: QUOTE
Token: COLSUFFIX
Token: IS
Token: EXEC
Token: POLICY_NO
Token: REG_NO
Token: EFFECTIVE_DATE
Token: EXPIRY_DATE
Token: CAN_EFF_DATE
Token: CAN_PRO_DATE
Token: RETURN_PREMIUM
Token: CAN_PROCESSED
Token: END-EXEC
Token: COBOL
Token: DCLCANCL
Token: POLICY_NO
Token: CN-POLICY-NO //I need this string too in diff array name tableRow[6][10]
Token: REG_NO
Token: CN-REG-NO //I need this string too in diff array name tableRow[6][10]
Token: EFFECTIVE_DATE
Token: CN-EFFECTIVE-DATE //I need this string too in diff array name tableRow[6][10]
Token: EXPIRY_DATE
Token: CN-EXPIRY-DATE //I need this string too in diff array name tableRow[6][10]
Token: CAN_EFF_DATE
Token: CN-CAN-EFF-DATE //I need this string too in diff array name tableRow[6][10]
Token: CAN_PRO_DATE
Token: CN-CAN-PRO-DATE //I need this string too in diff array name tableRow[6][10]
Token: RETURN_PREMIUM
Token: CN-RETURN-PREMIUM //I need this string too in diff array name tableRow[6][10]
Token: CAN_PROCESSED
Token: CN-CAN-PROCESSED //I need this string too in diff array name tableRow[6][10]
Token: THEYour code cleverly extracted the strings within EXEC and END-EXEC which I stored in
char tableCol[6][10]
But now I am finding it really difficult to store the strings which are immediatedly followed by the "tabCol" string from the file... :( ie CN-POLICY-NO, CN-REG-NO, CN-EFFECTIVE-DATE, CN-EXPIRY-DATE, CN-CAN-EFF-DATE, CN-CAN-PRO-DATE, CN-RETURN-PREMIUM & CN-CAN-PROCESSED which are to be stored in
char tableRow[6][10]
Apologies ...:( Thanks a ton, Faez
-
That code was a beauty again .... :-D Actually... the final file after strtok() looks like the one below... :|
Token: DCLGEN
Token: LIBRARY
Token: ACTION
Token: LANGUAGE
Token: NAMES
Token: QUOTE
Token: COLSUFFIX
Token: IS
Token: EXEC
Token: POLICY_NO
Token: REG_NO
Token: EFFECTIVE_DATE
Token: EXPIRY_DATE
Token: CAN_EFF_DATE
Token: CAN_PRO_DATE
Token: RETURN_PREMIUM
Token: CAN_PROCESSED
Token: END-EXEC
Token: COBOL
Token: DCLCANCL
Token: POLICY_NO
Token: CN-POLICY-NO //I need this string too in diff array name tableRow[6][10]
Token: REG_NO
Token: CN-REG-NO //I need this string too in diff array name tableRow[6][10]
Token: EFFECTIVE_DATE
Token: CN-EFFECTIVE-DATE //I need this string too in diff array name tableRow[6][10]
Token: EXPIRY_DATE
Token: CN-EXPIRY-DATE //I need this string too in diff array name tableRow[6][10]
Token: CAN_EFF_DATE
Token: CN-CAN-EFF-DATE //I need this string too in diff array name tableRow[6][10]
Token: CAN_PRO_DATE
Token: CN-CAN-PRO-DATE //I need this string too in diff array name tableRow[6][10]
Token: RETURN_PREMIUM
Token: CN-RETURN-PREMIUM //I need this string too in diff array name tableRow[6][10]
Token: CAN_PROCESSED
Token: CN-CAN-PROCESSED //I need this string too in diff array name tableRow[6][10]
Token: THEYour code cleverly extracted the strings within EXEC and END-EXEC which I stored in
char tableCol[6][10]
But now I am finding it really difficult to store the strings which are immediatedly followed by the "tabCol" string from the file... :( ie CN-POLICY-NO, CN-REG-NO, CN-EFFECTIVE-DATE, CN-EXPIRY-DATE, CN-CAN-EFF-DATE, CN-CAN-PRO-DATE, CN-RETURN-PREMIUM & CN-CAN-PROCESSED which are to be stored in
char tableRow[6][10]
Apologies ...:( Thanks a ton, Faez
Since they all begin with "CN-", you could add a test:
if (strncmp(tok, "CN-", 3) == 0)
printf ...Alternatively you have to test for each one individually.
Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman
-
That code was a beauty again .... :-D Actually... the final file after strtok() looks like the one below... :|
Token: DCLGEN
Token: LIBRARY
Token: ACTION
Token: LANGUAGE
Token: NAMES
Token: QUOTE
Token: COLSUFFIX
Token: IS
Token: EXEC
Token: POLICY_NO
Token: REG_NO
Token: EFFECTIVE_DATE
Token: EXPIRY_DATE
Token: CAN_EFF_DATE
Token: CAN_PRO_DATE
Token: RETURN_PREMIUM
Token: CAN_PROCESSED
Token: END-EXEC
Token: COBOL
Token: DCLCANCL
Token: POLICY_NO
Token: CN-POLICY-NO //I need this string too in diff array name tableRow[6][10]
Token: REG_NO
Token: CN-REG-NO //I need this string too in diff array name tableRow[6][10]
Token: EFFECTIVE_DATE
Token: CN-EFFECTIVE-DATE //I need this string too in diff array name tableRow[6][10]
Token: EXPIRY_DATE
Token: CN-EXPIRY-DATE //I need this string too in diff array name tableRow[6][10]
Token: CAN_EFF_DATE
Token: CN-CAN-EFF-DATE //I need this string too in diff array name tableRow[6][10]
Token: CAN_PRO_DATE
Token: CN-CAN-PRO-DATE //I need this string too in diff array name tableRow[6][10]
Token: RETURN_PREMIUM
Token: CN-RETURN-PREMIUM //I need this string too in diff array name tableRow[6][10]
Token: CAN_PROCESSED
Token: CN-CAN-PROCESSED //I need this string too in diff array name tableRow[6][10]
Token: THEYour code cleverly extracted the strings within EXEC and END-EXEC which I stored in
char tableCol[6][10]
But now I am finding it really difficult to store the strings which are immediatedly followed by the "tabCol" string from the file... :( ie CN-POLICY-NO, CN-REG-NO, CN-EFFECTIVE-DATE, CN-EXPIRY-DATE, CN-CAN-EFF-DATE, CN-CAN-PRO-DATE, CN-RETURN-PREMIUM & CN-CAN-PROCESSED which are to be stored in
char tableRow[6][10]
Apologies ...:( Thanks a ton, Faez
Faez Shingeri wrote:
...which are to be stored in
char tableRow[6][10]
Which won't be large enough.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
-
Since they all begin with "CN-", you could add a test:
if (strncmp(tok, "CN-", 3) == 0)
printf ...Alternatively you have to test for each one individually.
Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman
No no... It ain't always preceeded with "CN-" string ... The strings which needs to be stored in TabRow array are followed by the second occurrence of each tableCol values... Initially I also thought of using strcat () and append CN- to the tabCol values... But den I realized that it was for this file only... Other files are completely different. Regards, Faez
-
No no... It ain't always preceeded with "CN-" string ... The strings which needs to be stored in TabRow array are followed by the second occurrence of each tableCol values... Initially I also thought of using strcat () and append CN- to the tabCol values... But den I realized that it was for this file only... Other files are completely different. Regards, Faez
If you don't have a common character identifier to select your strings then there is little that can be done. And from what you say you will have different rules for different files so it's difficult to suggest any other options.
Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman
-
If you don't have a common character identifier to select your strings then there is little that can be done. And from what you say you will have different rules for different files so it's difficult to suggest any other options.
Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman
Finally I have come up witha Not-so-good code... But it works.. :) I created another similar situation to parse from END-EXEC to End of File as below...
if (k == 0)
{
if (strcmp(tok, "END-EXEC") == 0)
k = 1;
}
else
{
if (strcmp(tok, "THE") == 0)
{
k = 0;
}
else
{
strcpy(tabRow[m],tok);m++;
fprintf(fp3,"Token: %s\n", tok);}
}
}
}
// Out of all the loops and condition now....j=0,n=0; for(j=0;j<=m;j++) { if(strcmp(tabCol\[n\],tabRow\[j\])==0) { k=j; strcpy(tabRow\[n\],tabRow\[k+1\]); n++; } else ; } for(k=0;k
It wud be nice to get ur comments on this... :-D
Thanks,
Faez -
Finally I have come up witha Not-so-good code... But it works.. :) I created another similar situation to parse from END-EXEC to End of File as below...
if (k == 0)
{
if (strcmp(tok, "END-EXEC") == 0)
k = 1;
}
else
{
if (strcmp(tok, "THE") == 0)
{
k = 0;
}
else
{
strcpy(tabRow[m],tok);m++;
fprintf(fp3,"Token: %s\n", tok);}
}
}
}
// Out of all the loops and condition now....j=0,n=0; for(j=0;j<=m;j++) { if(strcmp(tabCol\[n\],tabRow\[j\])==0) { k=j; strcpy(tabRow\[n\],tabRow\[k+1\]); n++; } else ; } for(k=0;k
It wud be nice to get ur comments on this... :-D
Thanks,
FaezDifficult to say really. Assuming it does what you want then that's fine, but I don't see any checks for the specific strings after the "END-EXEC" that you wish to save. Or perhaps that is what the line
if(strcmp(tabCol[n],tabRow[j])==0)
is doing.
Unrequited desire is character building. OriginalGriff I'm sitting here giving you a standing ovation - Len Goodman