Searching datas in files



  • Hi, I'm just a begginer so please don't be so hard 🤡.

    I receive every day a file of datas, where I'm searching some special rows. For example I receive a data:

    1641 38 123654786 1000000
    16401 25 456444564 2500000
    16401 26 456654321 2500000

    16401 35 452154642 3500000

    What I need is to delete all rows were I don't have in the 4 column first number "25" so in this example I need to delete the first and the forth row. The problem is that the position of the rows where is "25" is not always the same and also the number of the rows where I have "25" is always not the same.
    So I just want to receive a file, take from him just the rows where is "25" everything other delete and save the file. Please help me !!!! Or just give me some directions.

    Thanks. Miroslav



  • #define MAX_LINESIZE 999
    
    int Is25(char* szLine)
    {
    	//Line has 3 other columns separated by exactly one blank?
    	szLine = strstr(szLine, " ");
    	if(!szLine)
    		return 0;
    	szLine = strstr(szLine, " ");
    	if(!szLine)
    		return 0;
    	//4th column starts with "25"?
    	return strstr(szLine, " 25") ? 1 : 0;
    }
    
    void Only25(const char* szFileName, const char* szFileNameNew)
    {
    	char szLine[MAX_LINESIZE];
    	FILE* pfOutput;
    	FILE* pfInput = fopen(szFileName, "r");
    	if(!pfInput)
    		return;
    	pfOutput = fopen(szFileNameNew, "w");
    	if(!pfOutput)
    	{
    		fclose(pfInput);
    		return;
    	}
    	while(fgets(szLine, sizeof[szLine], pfInput))
    		if(Is25(szLine))
    			fputs(szLine);
    	fclose(pfInput);
    	fclose(pfOutput);
    }
    

Anmelden zum Antworten