473,657 Members | 2,432 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problems with chr[]

Hi,

I want to read a file line by line and tokenize the content.
My code:
ifstream Configuration;
Configuration.o pen("config.cfg ");

// assume that max. length of line in configuration file is 120
// characters
char buffer[120];
char *tokenPtr;

while ( !Configuration. eof() )
{
Configuration.g etline( 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_ARE A:" ) == 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 2264
Christian Christmann wrote:
Hi,

I want to read a file line by line and tokenize the content.
My code:
ifstream Configuration;
Configuration.o pen("config.cfg ");

// assume that max. length of line in configuration file is 120
// characters
char buffer[120];
char *tokenPtr;

while ( !Configuration. eof() )
{
Configuration.g etline( 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_ARE A:" ) == 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.o pen("config.cfg ");

// assume that max. length of line in configuration file is 120
// characters
char buffer[120];
char *tokenPtr;

while ( !Configuration. eof() )
{
Configuration.g etline( 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_ARE A:" ) == 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.o pen("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.g etline( 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_ARE A:" ) == 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(tokenPt r, " ") == 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(th estream, 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(tokenPt r, " ") == 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.arco r-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.o pen("config.cfg ");

// assume that max. length of line in configuration file is 120
// characters
char buffer[120];
char *tokenPtr;

while ( !Configuration. eof() )
{
Configuration.g etline( 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_ARE A:" ) == 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2561
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
2117
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 SQL_SELECT SQL_TABLE SQL_CONDITION
3
2305
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 the code that is breaking was found on the internet by the developer who created the DB. The first error "Error # 29060 was genereated by migrated Variation Database File not found. Comes up after the user logs on. It is secured with regular...
3
2674
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 simple parameters. I have tested the web service on the PC it was written on (Windows XP, IIS 6, .NET 1.1, VS .NET) through the default HTTP / HTML interface with Internet Explorer. All works fine.
4
1933
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 some reason I keep getting the error: Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
0
1925
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 some reason I keep getting the error: Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
18
4612
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 has some different characters in it that I am trying to remove. The small a with two dots over it and the small y with two dots over it. Here is my code so far to remove the small y: Private Sub Button2_Click(ByVal sender As System.Object, ByVal...
3
1618
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 As NetworkStream = tcpCli.GetStream ' Send a request to my Tibbo Ethernet device to give me the version number. Dim sw As New StreamWriter(ns) sw.WriteLine(chr(255) & chr(2) & "V" & vbcrlf)
0
1523
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 connects directly to the REST API. When I code it as a front end doing a form post it works fine, but I can't code the VB on the backend to do the same thing. Any help would be much appreciated. Also, how do you convert the contents of a filestream...
0
2339
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
8413
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8324
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8740
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8513
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8617
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7352
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2742
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.