C
Christian Christmann
Hi,
I want to read a file line by line and tokenize the content.
My code:
ifstream Configuration;
Configuration.open("config.cfg");
// assume that max. length of line in configuration file is 120
// characters
char buffer[120];
char *tokenPtr;
while ( !Configuration.eof() )
{
Configuration.getline( buffer, 120, '\n' );
// check whether line is not a comment or empty line or beginning
// with white space, if so skip
if ( buffer[0] != '#' || buffer[0] != '\n' || buffer[0] != ' ' )
{
// begin tokenization of buffer
tokenPtr = strtok( buffer, " " );
// dealing with memory specifications
if ( strcmp( tokenPtr, "MEMORY_AREA:" ) == 0 )
{
// read first token
tokenPtr = strtok ( NULL, " " );
if ( tokenPtr != NULL )
{
...
}
}
// dealing with CPU specs
else if ( strcmp( tokenPtr, "CPU:" ) == 0 )
{
// read first token
}
}
}
My config file look something like:
# Memory specs
MEMORY_AREA: 0x2000000 0x3000000
CPU: 150
[...]
When reading the file the first line with # is correctly skipped.
Also the second line beginning with MEMORY_AREA: is correctly
analyzed. But instead of skipping the third line (since its an white space)
I get a Segmentation fault. Why is that?
Thx
Chris
I want to read a file line by line and tokenize the content.
My code:
ifstream Configuration;
Configuration.open("config.cfg");
// assume that max. length of line in configuration file is 120
// characters
char buffer[120];
char *tokenPtr;
while ( !Configuration.eof() )
{
Configuration.getline( buffer, 120, '\n' );
// check whether line is not a comment or empty line or beginning
// with white space, if so skip
if ( buffer[0] != '#' || buffer[0] != '\n' || buffer[0] != ' ' )
{
// begin tokenization of buffer
tokenPtr = strtok( buffer, " " );
// dealing with memory specifications
if ( strcmp( tokenPtr, "MEMORY_AREA:" ) == 0 )
{
// read first token
tokenPtr = strtok ( NULL, " " );
if ( tokenPtr != NULL )
{
...
}
}
// dealing with CPU specs
else if ( strcmp( tokenPtr, "CPU:" ) == 0 )
{
// read first token
}
}
}
My config file look something like:
# Memory specs
MEMORY_AREA: 0x2000000 0x3000000
CPU: 150
[...]
When reading the file the first line with # is correctly skipped.
Also the second line beginning with MEMORY_AREA: is correctly
analyzed. But instead of skipping the third line (since its an white space)
I get a Segmentation fault. Why is that?
Thx
Chris