Hi,

What would be the best way to extract an array of strings (only the first strings separated by a "," ) encapsulated within the curly braces ( { } )from within a file...?

Eg: Test.txt
***************************
This is also in the text file

{ abc def ghi ,
jkl mno pqr ,
stu vwl yza ,
}

This is also in the text file
*****************************


I only want "abc" "jkl" and "stu" in an array..

Thanks,
Faez

Recommended Answers

All 4 Replies

If all those spaces are consistently in the place you show them, use the >> operator when reading the file. It will read everything between the spaces, one set at a time.

Ok Here is my code....

int main()
{
	char fileOrig[32] = "CANCLT.DCL";
	char fileDupl[32] = "NEWCANCLT.DCL";
	char *buf[120], *tok;



	FILE *fp1,*fp2;
	fp1=fopen(fileOrig,"r+");
	fp2=fopen(fileDupl, "w+");

	while(fgets(buf, bufsize, fp1) != NULL)
	{
		tok = strtok(buf,"  ( ,) *01\n.");
		if (tok != NULL)
		fprintf(fp2,"Token: %s\n", tok);
	}

	fclose(fp2);
	fclose(fp1);

	return 0;
}

My new o/p file looks like
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

But I want the strings only after EXEC and before END-EXEC... ie from POLICY_NO to CAN_PROCESSED... I tried using while(strcmp("EXEC",tok)) but it ain't working .. :-/

Thanks in advance,
Faez

But I want the strings only after EXEC and before END-EXEC... ie from POLICY_NO to CAN_PROCESSED...

So read the strings up to EXEC and don't do anything with them.
Then read the strings up to END-EXEC and use them.

Hint: two loops.

This code works... :-D

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);
        }
    }
}

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: THE

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] /* "Strings which I need aren't always preceeded with CN-" Its for this file only */

Any kinda help wud be appreciated.. :)

Thanks,
Faez

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.