初学者fscanf问题 [英] beginner fscanf question

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

问题描述




我有一个关于fscanf的初学者问题。

首先我有一个文本文件,其中只包含一些

十六进制数:


0C100012

0C100012

....


It很容易将这个值读入整数变量:

FILE * fp = fopen(" test.dat"," r");

int mem_word = 0 ;

while(fscanf(fp,"%x",& mem_word)!= EOF)

....


现在,我有以下格式,其中我的十六进制数

被空格分割。所以我想知道是否有一个简单的方法可以解析这样一条线并且没有做什么

a很多空间移除等等。


0C 10 00 12 // 00000000

0C 10 00 12 // 00000000

....


感谢您的帮助评论。

解决方案

Roman Zeilinger写道:


现在,我有以下格式我的十六进制数字

由空格分割。所以我想知道是否有一个简单的方法来解析这样一条线并且没有做什么

a很多空间移除等等。



我不这么认为。但删除空格然后使用

sscanf()是微不足道的。


0C 10 00 12 // 00000000

0C 10 00 12 // 00000000



或者你可以编写自己的简单解析器


< blockquote> Roman Zeilinger< Ro ************* @ yahoo.dewrote:


Hi


我有一个关于fscanf的初学者问题。

首先我有一个文本文件,其中只包含一些

十六进制数字:


0C100012

0C100012

...


很容易将这些值读入整数变量:


FILE * fp = fopen(" test.dat"," r");

int mem_word = 0;

while(fscanf(fp,"%x") ;,& mem_word)!= EOF)

...


现在,我的格式如下十六进制数

按空格分割。所以我想知道是否有一个简单的方法来解析这样一条线并且没有做了很多的空间移除等等。


0C 10 00 12 // 00000000

0C 10 00 12 // 00000000



如果有'总是 // 00000000在行尾和

从来没有别的东西(除了尾随空白区域)它是相对简单的:


#include< stdio.h>

#include< stdlib.h>


int main(void){

FILE * fp = fopen(sh.dat,r);

unsigned int d [4];

unsigned int x;


if(fp == NULL)

返回EXIT_FAILURE;


while(fscanf(fp,"%) x%x%x%x // 00000000",

d,d + 1,d + 2,d + 3)== 4){

x =((( ((d [0] << 8)+ d [1])<< 8)

+ d [2])<< 8)+ d [3];

printf(" x =%x \ n",x);

}


返回0;

}


但是如果不是这样的话那么你需要多做一些

的工作循环:


while(fscanf(fp,"%x%x%x%x",d,d + 1,d + 2,d + 3)== 4) {

x =(((((d [0]<< 8)+ d [1])<< 8)

+ d [2])<< 8)+ d [3];

printf(" x =%x \ n",x);

while((c = fgetc(fp)) !=''\ n''&& c!= EOF)

/ *空* /;

}


你还需要添加''c''的定义(作为int,而不是

char!)。


你可以使用更简单的东西,如果你可以确定在你的四元组的四元组之后将不会超过12个字符

数字(除了尾随空白字符之外):


#include< stdio.h>

#include< stdlib.h>


int main(void ){

FILE * fp = fopen(" sh.dat"," r");

unsigned int d [4];

unsigned int x;

char buffer [13];


if(fp == NULL)

返回EXIT_FAILURE;


while(fscanf(fp,"%x%x%x%x%12 [^ \ n]",

d,d + 1,d + 2,d + 3,缓冲区)== 5){

x =(((((d [0]<< 8)+ d [1])<< 8)

+ d [2])<< 8)+ d [3];

printf(" x =%x \ n",x);

}


返回0;

}

问候,Jens

-

\ Jens Thoms Toerring ___ jt@toerring.de

\ __________________________ http://toerring.de



您可以使用某些东西更简单,如果你可以肯定会有你的b / b
数字后面不会超过12个字符(除了尾随空白字符之外):


#include< stdio.h>

#include< stdlib.h>


int main(void){

FILE * fp = fopen(" sh.dat"," r");

unsigned int d [4];

unsigned int x;

char buffer [13];


if(fp == NU) LL)

返回EXIT_FAILURE;


while(fscanf(fp,"%x%x%x%x%12 [^ \ n]" ;,

d,d + 1,d + 2,d + 3,缓冲区)== 5){

x =((((((d [0]< < 8)+ d [1])<< 8)

+ d [2])<< 8)+ d [3];

printf(" x =%x \ n",x);

}


返回0;

}



非常感谢Jens。它适用于一行,所以我必须弄清楚

如何解析整个txt文件:)


干杯!


Hi

I have a beginner question concerning fscanf.
First I had a text file which just contained some
hex numbers:

0C100012
0C100012
....

It was easy to read this values into an integer variable:
FILE *fp = fopen("test.dat","r");
int mem_word = 0;
while (fscanf(fp,"%x", &mem_word) != EOF)
....

Now, I have the following format where my hex number
is splitted by spaces. So I wonder if there is
an easy way to parse such a line and not doing
a lot of space removing etc.

0C 10 00 12 // 00000000
0C 10 00 12 // 00000000
....

Thanks for helpful comments.

解决方案

Roman Zeilinger wrote:

Now, I have the following format where my hex number
is splitted by spaces. So I wonder if there is
an easy way to parse such a line and not doing
a lot of space removing etc.

I don''t think so. But it''s trivial to remove the spaces and then use
sscanf().

0C 10 00 12 // 00000000
0C 10 00 12 // 00000000

Or you could write your own simple parser


Roman Zeilinger <Ro*************@yahoo.dewrote:

Hi

I have a beginner question concerning fscanf.
First I had a text file which just contained some
hex numbers:

0C100012
0C100012
...

It was easy to read this values into an integer variable:

FILE *fp = fopen("test.dat","r");
int mem_word = 0;
while (fscanf(fp,"%x", &mem_word) != EOF)
...

Now, I have the following format where my hex number
is splitted by spaces. So I wonder if there is
an easy way to parse such a line and not doing
a lot of space removing etc.

0C 10 00 12 // 00000000
0C 10 00 12 // 00000000

If there''s always " // 00000000" at the end of the line and
never anything else (except trailing white space) it''s rela-
tively simple:

#include <stdio.h>
#include <stdlib.h>

int main( void ) {
FILE *fp = fopen( "sh.dat", "r" );
unsigned int d[ 4 ];
unsigned int x;

if ( fp == NULL )
return EXIT_FAILURE;

while ( fscanf( fp, "%x%x%x%x // 00000000",
d, d + 1, d + 2, d + 3 ) == 4 ) {
x = ( ( ( ( ( d[ 0 ] << 8 ) + d[ 1 ] ) << 8 )
+ d[ 2 ] ) << 8 ) + d[ 3 ];
printf( "x = %x\n", x );
}

return 0;
}

but if that''s not the case then you will need to do a bit more of
work in that while loop:

while ( fscanf( fp, "%x%x%x%x", d, d + 1, d + 2, d + 3 ) == 4 ) {
x = ( ( ( ( ( d[ 0 ] << 8 ) + d[ 1 ] ) << 8 )
+ d[ 2 ] ) << 8 ) + d[ 3 ];
printf( "x = %x\n", x );
while ( ( c = fgetc( fp ) ) != ''\n'' && c != EOF )
/* empty */ ;
}

You also will need to add a definition of ''c'' (as an int, not a
char!).

You could use something simpler if you can be sure that there will
be never more than 12 characters following your quadruplet of hex
numbers (again except trailing white space characters):

#include <stdio.h>
#include <stdlib.h>

int main( void ) {
FILE *fp = fopen( "sh.dat", "r" );
unsigned int d[ 4 ];
unsigned int x;
char buffer[ 13 ];

if ( fp == NULL )
return EXIT_FAILURE;

while ( fscanf( fp, "%x%x%x%x%12[^\n]",
d, d + 1, d + 2, d + 3, buffer ) == 5 ) {
x = ( ( ( ( ( d[ 0 ] << 8 ) + d[ 1 ] ) << 8 )
+ d[ 2 ] ) << 8 ) + d[ 3 ];
printf( "x = %x\n", x );
}

return 0;
}
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de


You could use something simpler if you can be sure that there will
be never more than 12 characters following your quadruplet of hex
numbers (again except trailing white space characters):

#include <stdio.h>
#include <stdlib.h>

int main( void ) {
FILE *fp = fopen( "sh.dat", "r" );
unsigned int d[ 4 ];
unsigned int x;
char buffer[ 13 ];

if ( fp == NULL )
return EXIT_FAILURE;

while ( fscanf( fp, "%x%x%x%x%12[^\n]",
d, d + 1, d + 2, d + 3, buffer ) == 5 ) {
x = ( ( ( ( ( d[ 0 ] << 8 ) + d[ 1 ] ) << 8 )
+ d[ 2 ] ) << 8 ) + d[ 3 ];
printf( "x = %x\n", x );
}

return 0;
}

Great thanks Jens. It works fine for one line so I have to figure out
how I can parse the whole txt file :)

Cheers!


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

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