473,387 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

problems with chr[]

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


Jul 23 '05 #1
11 2235
Christian Christmann wrote:
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


May I suggestion you use a debugger and trace the execution?
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #2
Christian Christmann wrote:
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


One other suggestion. Try representing your data as C++ class types (that
includes struct), and use C++ I/O, string and perhaps stringstream to read
in your tokens. I'm not proficient at the art myself, but I believe it is a
sill worth developing, and would probably improve your code.
--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #3
Christian Christmann wrote:
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];
Why not use std::string? Then you don't need to make such assumptions.
char *tokenPtr;

while ( !Configuration.eof() )
This is wrong. eof() will return true, _after_ you tried to read past the
end of the file. So the loop will be run once to often. Also what is if
there is an error during reading?
Instead of the above, combine this with the getline below and make it:

while (Configuration.getline( buffer, 120, '\n' ))

For more about this, see the FAQ.
{
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] != ' ' )
A '\n' will never be in there, since getline does not put it into the
buffer.
{
// 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?


Because that line is empty, but your if() above doesn't count it as one.
Then, your first strtok will return a null pointer, which you subsequently
use in strcmp. This is probably what causes the segfault.

Jul 23 '05 #4
> Why not use std::string? Then you don't need to make such assumptions.

Thank you for your answer. But it seems that getline is not accepting
strings as parameter.
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?


Because that line is empty, but your if() above doesn't count it as one.
Then, your first strtok will return a null pointer, which you subsequently
use in strcmp. This is probably what causes the segfault.


If added an if() to detect the empty line:
[...]
else if ( strcmp( tokenPtr, "CPU:" ) == 0 )
{
// read first token
}

else if (strcmp(tokenPtr, " ") == 0)
cout << "\nempty\n";

But this cout is never executed.
Why?

Chris

Jul 23 '05 #5
> if ( buffer[0] != '#' || buffer[0] != '\n' || buffer[0] != ' ' ) {

I also just figured out that this if() is always true even if the line
begins with an #.
Why is the first character of buffer which represents the first
char of a new line not recognized as # ?

Chris
Jul 23 '05 #6
Christian Christmann wrote:
Why not use std::string? Then you don't need to make such assumptions.


Thank you for your answer. But it seems that getline is not accepting
strings as parameter.


You're using the wrong getline. The one that takes std::string as parameter
is not a member of the stream. Try std::getline(thestream, thestring).
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?


Because that line is empty, but your if() above doesn't count it as one.
Then, your first strtok will return a null pointer, which you
subsequently use in strcmp. This is probably what causes the segfault.


If added an if() to detect the empty line:
[...]
else if ( strcmp( tokenPtr, "CPU:" ) == 0 )
{
// read first token
}

else if (strcmp(tokenPtr, " ") == 0)
cout << "\nempty\n";

But this cout is never executed.
Why?


Because you are comparing your line with a line that contains a space
character, not with an empty line. Note that a space character is - even
thought not visible - a character and does not count as nothing.

Jul 23 '05 #7
Christian Christmann wrote:
if ( buffer[0] != '#' || buffer[0] != '\n' || buffer[0] != ' ' ) {


I also just figured out that this if() is always true even if the line
begins with an #.
Why is the first character of buffer which represents the first
char of a new line not recognized as # ?


It is. But it's not at the same time a newline and a space charater. Your
condition will only be false if the first character is a #, a newline and a
space at the same time, which is of course not possible.
Check your logic.

Jul 23 '05 #8
"Christian Christmann" <pl*****@yahoo.de> wrote in message
news:42***********************@newsread4.arcor-online.net...
if ( buffer[0] != '#' || buffer[0] != '\n' || buffer[0] != ' ' ) {


I also just figured out that this if() is always true even if the line
begins with an #.
Why is the first character of buffer which represents the first
char of a new line not recognized as # ?

Think about it: the if statement will be true, if the first char is
not #, or if it is not \n. Now invert that and you get: the if statement
will be false, if the first char is # and if the first char is \n.
Because one char can never be two values at the same time, the if
statement will never be false, thus it will always be true.
Solution: replace the || with &&, because you do not want it to be #
*and* you do not want it to be \n either.

hth
--
jb

(reply address in rot13, unscramble first)
Jul 23 '05 #9
Dear Chris,
I tested your program and I found your logic to be incorrect. I think

"if ( buffer[0] != '#' || buffer[0] != '\n' || buffer[0] != ' ' ) "

this line should be

"if ( buffer[0] != '#' && buffer[0] != '\n' && buffer[0] != ' ' )"
..
Once I changed the above mentioned line your program worked fine.

Pls revert back if you have any questions.

Thanks & Regards
Ambar
Christian Christmann wrote:
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


Jul 23 '05 #10
This should be the structure of your parser, with the appropriate C++
tools. No need for strtok, it's cumbersome.

#include <iostream>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <string>
using namespace std;

// g++ -o parse parse.C
// ./parse config.cfg
int main(int argc, char * argv[])
{
string line, token;
fstream in(argv[1], ios::in); // argv[1]=config.cfg

do{
// Get the line.
getline(in, line);

// Parse it.
istringstream instr(line); // put it into a stringstream
while(instr >> token){ // extract 1 token at the time.

cerr << token << endl; // see what did.

// process token

}
}while(!in.eof());

in.close();

return 0;
}
Jul 23 '05 #11
Amadeus W. M. wrote:

[...]
}while(!in.eof());

in.close();

return 0;
}

According to what I just read in TC++SL §13.9, there is no need to
explicitly close a file when using fstream if you leave the scope without
trying to use it again. The destructor on fsteam will do that for you.

http://www.josuttis.com/libbook/

--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Karl Pech | last post by:
Hi all, I'm trying to write a program which can read in files in the following format: sos_encoded.txt: --- begin-base64 644 sos.txt UGxlYXNlLCBoZWxwIG1lIQ== ---
8
by: Bart Plessers \(artabel\) | last post by:
Hello, I have problems with the quotation mark and strings in my asp script. I made a general FORM (myform.asp) to read out data from a dbase Some vars are defined in the FORM: SQL_DBASE...
3
by: kathyk | last post by:
Hi All, I am using Access 2003 on machines with windows 2000 and XP. The problem I'm having started only after we got a new image for our PC's. This database app has been around for awhile and...
3
by: Andi Twine | last post by:
Hi all, I really hope someone here can help ! I have designed and built an ASP.NET web service with Visual Studio .NET. The web service outputs some dummy XML data when its called with some...
4
by: Tim::.. | last post by:
Can someone please help.... I'm having major issues with a user control I'm tring to create! I an trying to execute a sub called UploadData() from a user control which I managed to do but for...
0
by: Tim::.. | last post by:
Can someone please help.... I'm having major issues with a user control I'm tring to create! I an trying to execute a sub called UploadData() from a user control which I managed to do but for...
18
by: james | last post by:
Hi, I am loading a CSV file ( Comma Seperated Value) into a Richtext box. I have a routine that splits the data up when it hits the "," and then copies the results into a listbox. The data also...
3
by: Jerry Spence1 | last post by:
I am trying to get a TCP client working. I am using an example code as follows: Dim tcpCli As New TcpClient("192.168.0.6", 1001) ' Retrieve the stream that can send and receive data. Dim ns...
0
by: Solius | last post by:
I have been struggling for 4 days to write a connection to an XML REST API. I can't figure out what is wrong with my code that it won't connect propertly. The goal is to make a web service that...
0
by: Thore Harald =?iso-8859-1?Q?H=F8ye?= | last post by:
I have made this testcase: ----------------------- #!/usr/bin/perl #use locale; #use encoding 'iso-8859-1'; use utf8; binmode(STDOUT, ":utf8"); print "\\x{00D8}:\n"; test("\x{00D8}");
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.