Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Test 1st Character

Status
Not open for further replies.

YOUNGCODGER

Programmer
Jul 9, 2007
102
GB

Hi,

I am now coming back to 'C' after years away. My first task is to process an ASCII file of data. Only records that start with one of ten characters are required - the rest can be disregarded. So after doing an 'fgets' I need to check the first character. Testing one at a time against the ten seems messy - is there an elegant solution?

Regards,

YoungCodger [bigglasses]
 
hi,
however you've to read all records and all chars in arecord;
in plain text files, records is a Human assuption, the APIs see the file as a stream of chars;


.....

{
char buffer[ENOUGH] ;

while( fgets( buffer, sizeof(buffer), stream ) != NULL )
{
if( !IsMychar(buffer[0]) )
continue ;

buffer[strlen(buffer)-1] = 0 ; // if you want del NL

BODY
}

//test here if exit from loop for EOF or other

}

//-----------------------------
BOOL IsMyChar( char c )
{
switch c
{
case 'A' :
case 'B' :
case 'C' :
.......
case 'D' : return 1;
}
return 0 ;
}


bye
vic
 
Code:
const char* wanted = ".....";
...
while (fgets(line...)) {
    if (strchr(wanted,*line)) {
        /* that's it! */
    }
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top