chr []的问题 [英] problems with chr[]

查看:117
本文介绍了chr []的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我想逐行读取文件并将内容标记化。

我的代码:

ifstream配置;

Configuration.open(" config.cfg");


//假设最大值。配置文件中的行长度为120

//字符

char buffer [120];

char * tokenPtr;
< (&!$!
)(!Configuration.eof())

{

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

//检查行是不是注释或空行还是开始

//带空格,如果有的话跳过

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

{

//开始缓冲区的标记化

tokenPtr = strtok(缓冲区,"");


//处理内存规范

if(strcmp(tokenPtr," MEMORY_AREA:")== 0)

{

//读取第一个令牌

tokenPtr = strtok(NULL,"");

if(tokenPtr!= NULL)

{

.. 。

}

}


//处理CPU规格

else if(strcmp( tokenPtr, CPU:)== 0)

{

//读取第一个令牌

}

} < br $>
}


我的配置文件类似于:

#内存规格

MEMORY_AREA:0x2000000 0x3000000


CPU:150

[...]


读取文件的第一行#是正确跳过。

另外以MEMORY_AREA开头的第二行是正确的

分析。但不是跳过第三行(因为它是一个空白区域)

我得到了分段错误。为什么?


Thx

克里斯


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


推荐答案

Christian Christmann写道:
Christian Christmann wrote:


我想逐行阅读文件并将内容标记化。
我的代码:
ifstream配置;
Configuration.open(" config.cfg");

//假设最大值配置文件中的行长度为120
//字符串缓冲区[120];
char * tokenPtr;

while(!Configuration.eof())
{
Configuration.getline(buffer,120,''\ n'');
//检查行是否不是注释或空行或开头
//有空格,如果是这样的话,请跳过
if(buffer [0]!=''#''|| buffer [0]!=''\ n''|| buffer [0]!=''' ')
//开始缓冲区的标记化
tokenPtr = strtok(缓冲区,"");

//处理内存规范
if(strcmp(tokenPtr," MEMORY_AREA:")== 0)
{
//读取第一个令牌
tokenPtr = strtok(NULL,"");
if(tokenPtr!= NULL)
{
...
}
}
//处理CPU规格
if(strcmp(to。) kenPtr,CPU:)== 0)
{
//读取第一个令牌
}
}
}

我的配置文件看起来像:
#Memory specs
MEMORY_AREA:0x2000000 0x3000000

CPU:150
[...]

在读取文件时,##的第一行被正确跳过。
另外,以MEMORY_AREA开头的第二行正确分析。但是我没有跳过第三行(因为它是一个白色的空间),而是出现了分段错误。为什么?

谢谢
Chris
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




我建议您使用调试器并跟踪执行情况吗?

-

如果我们的假设是关于任何事情而不是关于某一个或多个特定事物,那么我们的推论就是数学。因此,数学可能被定义为我们永远不知道我们所讨论的是什么,以及我们所说的是否属实的主题.- Bertrand Russell


Christian Christmann写道:
Christian Christmann wrote:


我想逐行读取文件并将其标记为内容。
我的代码:
ifstream配置;
Configuration.open(" config.cfg");

//假设最大值。配置文件中的行长度为120
//字符串缓冲区[120];
char * tokenPtr;

while(!Configuration.eof())
{
Configuration.getline(buffer,120,''\ n'');
//检查行是否不是注释或空行或开头
//有空格,如果是这样的话,请跳过
if(buffer [0]!=''#''|| buffer [0]!=''\ n''|| buffer [0]!=''' ')
//开始缓冲区的标记化
tokenPtr = strtok(缓冲区,"");

//处理内存规范
if(strcmp(tokenPtr," MEMORY_AREA:")== 0)
{
//读取第一个令牌
tokenPtr = strtok(NULL,"");
if(tokenPtr!= NULL)
{
...
}
}
//处理CPU规格
if(strcmp(to。) kenPtr,CPU:)== 0)
{
//读取第一个令牌
}
}
}

我的配置文件看起来像:
#Memory specs
MEMORY_AREA:0x2000000 0x3000000

CPU:150
[...]

在读取文件时,##的第一行被正确跳过。
另外,以MEMORY_AREA开头的第二行正确分析。但是我没有跳过第三行(因为它是一个白色的空间),而是出现了分段错误。为什么?

谢谢
Chris
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




另一个建议。尝试将您的数据表示为C ++类类型(

包括struct),并使用C ++ I / O,字符串和字符串流来读取令牌中的
。我自己并不精通艺术,但我相信这是一个值得开发的基石,可能会改进你的代码。

-

如果我们的假设是关于任何事情而不是关于某一个或多个特定事物,那么我们的推论就是数学。因此,数学可能被定义为我们永远不知道我们所讨论的是什么,以及我们所说的是否属实的主题.- Bertrand Russell


Christian Christmann写道:
Christian Christmann wrote:


我想逐行读取文件并将其标记为内容。
我的代码:
ifstream配置;
Configuration.open(" config.cfg");

//假设最大值。配置文件中的行长为120
//缓冲区[120];


为什么不使用std :: string?然后你不需要做出这样的假设。

char * tokenPtr;

while(!Configuration.eof())


这是错的。 eof()将返回true,_after_您尝试读取文件的

结尾。因此循环将经常运行一次。还有什么,如果

在阅读过程中出现错误?

而不是上述内容,将其与下面的getline结合使用:


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


有关详细信息,请参阅常见问题解答。

{
Configuration.getline(buffer,120,''\ n'');
//检查行是否不是注释或空行或开始
//白色空格,若是,则跳过
if(buffer [0]!=''#''|| buffer [0]!=''\ n''|| buffer [0]!='''')


A''\ n''将永远不会出现在那里,因为getline没有将它放入

缓冲区。

{
//开始缓冲区的标记化
tokenPtr = strtok(缓冲区,"");

//处理内存规格
if(strcmp (tokenPtr," MEMORY_AREA:")== 0)
{
//首先阅读tok en
tokenPtr = strtok(NULL,"");
if(tokenPtr!= NULL)
{
...
}
}

//处理CPU规格
如果(strcmp(tokenPtr," CPU:") )== 0)
//
//读取第一个令牌
}
}

我的配置文件如下所示: br /> #Memory specs
MEMORY_AREA:0x2000000 0x3000000

CPU:150


当读取文件的第一行时#被正确跳过。
还有以MEMORY_AREA开头的第二行正确分析。但是我没有跳过第三行(因为它是一个白色的空间),而是出现了分段错误。这是为什么?
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?




因为那行是空的,但你上面的if()并不算作一个。

那么,你的第一个strtok将返回一个空指针,你随后在strcmp中使用
。这可能是导致段错误的原因。



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.


这篇关于chr []的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆