Q: Need to skeep emply lines in file [fgets]

Hello Everyone ,
I am trying to pase csv file and populate my stucture. But some how I have blank line in between and would like to skip those lines.
Can anyone please suggest some solution. Thanks in advance :

------ Code Segment I am using -------


FILE *fCSVALDataFile = fopen(szALDataCSVFile, "rt");

while( fgets(szLine, sizeof(szLine), fCSVALDataFile) )

{


strcpy( pApp->structALLogLocData.acALLogloc , strtok(szLine, ",") );
strcpy( pApp->structALLogLocData.acALLoglocDesc , strtok(NULL, ","));


}

--------------------------------------------------------------------------

File Structure :
Test1,Desc1

Test2 , Desc2

Test3 , Desc3


It seems fairly simple. If not, isn't it just a matter of adding a couple of lines of code at the top of your while loop? Just test the szLine before you copy it. Loop until you find a non-whitespace character and then don't perfrom the strcpy if you detect that the line is blank. By blank I assume it means that szLine is either a null string or it just has a few spaces correct?
Hi ,
Thanks for that reply but still did not get it.

szLine is declared as CHAR szLine[4096]. And as soon as I get a complete blank line my program crush.
I still not sure how to check szLine for a blank line .. Appreciate your help..

Thanks,
newbie ..!
1
2
3
if (strcmp(szLine, "") == 0) {
 // empty strlen(szLine) should work also
}
Last edited on
fgets() will put the newline of an empty line in the buffer, so testing for strlen() == 0 won't work. Check if the first character in szLine is a 0, newline or carriage return.
Topic archived. No new replies allowed.