使用fpos_t直接访问不起作用 [英] direct access with fpos_t doesn′t work

查看:52
本文介绍了使用fpos_t直接访问不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想在一个结构中保存一个ini文件的关键字,以及一个

fpos_t。我认为我的概念是正确的,但通过fsetpos()

的访问不起作用。位置总是错误的(在这个例子中除了

第一行)。


我的结构看起来像这样:

***

typedef struct {

char pname [32];

fpos_t pos;

} iniParam ;


这个例程读取数据并将其放入iniParam param [100]:

***

while(!feof(pFile)){

fgetpos(pFile,& pos);

c = fgetc(pFile);

ungetc (c,pFile);

if(c ==''#'')

fgets(linebuf,255,pFile);

否则{

param [i] .pos = pos;

fgets(linebuf,255,pFile);

sscanf(linebuf," %s",param [i] .pname);

i ++;

}

}


这个例程读取关键字并执行相应的fscanf:

***

for(i = 0; i< n; i ++){

if(strcmp(param [i] .pname," keyword1")== 0){

pos = param [i] .pos;

fsetpos (PFILE, & pos);

fscanf(pFile,"%* s%d"& param1);

}

if( strcmp(param [i] .pname," keyword2")== 0){

...


不知道为何通过一个fpos_t位置不起作用。

感谢您的帮助,Chris

解决方案

2003年10月8日星期三03 :26:26 +0200,C。 Sengstock" < sp ** @ sucks.com>

写道:


我想保存一个ini文件的关键字结构,以及
fpos_t。我认为我对这个概念是正确的,但通过fsetpos()
访问不起作用。位置总是错误的(除了在这个例子中,第一行)。


这对我来说只有两个问题。阅读循环没有正确检测文件结尾,如常见问题所述:

http://www.eskimo.com/~scs/C-faq/q12.2.html


ini文件中的空白行和垃圾行导致param []中的无效

条目。


我的建议是:

1.检查你调用的每个库函数的返回码。

2.发布一个完整但极简主义的程序,包括ini

文件内容证明了这个问题。

我的结构看起来像这样:
***
typedef struct {
char pname [32];
fpos_t pos;
} iniParam;

此例程读取数据并将其放入iniParam param [100]:
***
while(!feof(pFile)){
fgetpos(pFile,& pos);
c = fgetc(pFile);
ungetc(c,pFile);
if( c ==''#'')
fgets(linebuf,255,pFile);
else {
param [i] .pos = pos;
fgets(linebuf,255,pFile);
sscanf(linebuf,"%s",param [i] .pname); <这个例程读取关键字并执行相应的fscanf:
***
for(i = 0)这个例程读取关键字并执行相应的fscanf:
***
; I< N; i ++){
if(strcmp(param [i] .pname," keyword1")== 0){
pos = param [i] .pos;
fsetpos(pFile,& ; pos);
fscanf(pFile,"%* s%d",& param1);
}
if(strcmp(param [i] .pname," keyword2" )== 0){
...

不知道为什么通过fpos_t位置访问不起作用。
感谢您的帮助,Chris



尼克。


关于
http://www.eskimo.com/~scs/C-faq/q12.2.html

u可以

而(fgets(buf,MAXLINE,infp)!= NULL&&!feof(infp))

$

" Nick Austin" < NI ********** @ nildram.co.uk>在消息中写道

新闻:r3 ******************************** @ 4ax.com ...

2003年10月8日星期三03:26:26 +0200,C。 Sengstock" < sp ** @ sucks.com>
写道:

我想在一个结构中保存一个ini文件的关键字,以及一个
fpos_t。我认为我的概念是正确的,但通过
fsetpos()的访问不起作用。位置总是错误的(除了,在这个例子中,
第一行)。



它只对我有两个问题。读取循环没有正确检测文件结尾,如常见问题所述:

http://www.eskimo.com/~scs/C-faq/q12.2.html
<同样,ini文件中的空白行和垃圾行导致param []中的条目无效。

我的建议是:
1.检查每个的返回码你调用的库函数。
2.发布一个完整但极简主义的程序,包括演示问题的ini
文件内容。

我的结构如下所示:
***
typedef struct {
char pname [32];
fpos_t pos;
} iniParam;

此例程读取数据并将其放入iniParam param [100]:
***
while(!feof(pFile)){
fgetpos(pFile,& pos);
c = fgetc(pFile);
ungetc(c,pFile);
if(c ==''#'')
fgets(linebuf,255,pFile);
否则{
param [i] .pos = pos;
fgets(linebuf,255,pFile);
sscanf(linebuf,"%s",param [i] .pname);
i ++;
}

这个例程读取关键字并执行相应的fscanf:
***
for(i = 0; I< N; i ++){
if(strcmp(param [i] .pname," keyword1")== 0){
pos = param [i] .pos;
fsetpos(pFile,& ; pos);
fscanf(pFile,"%* s%d",& param1);
}
if(strcmp(param [i] .pname," keyword2" )== 0){
...

不知道为什么通过fpos_t位置访问不起作用。
感谢您的帮助,Chris


尼克。





" Nick Austin"写道:

我的建议是:
1.检查你调用的每个库函数的返回代码。
2.发布一个完整但极简主义的程序,包括ini 文件内容,证明问题。




这是更直接的。代码应保存

行的位置,并通过该位置执行输出。但代码不起作用,

的输出是:

---------------------- -------

#dontreadthis<<<这是myfile.txt。数据

波特= 4

#nodata


port = 50

parity = 6

<<<到这里

***输出

#dontreadthis<<<<这是通过职位产生的产出

这个

ta

<<< <那是结束

------------------------------------

代码如下:

----------------------------- --------

fpos_t position [50];

FILE * fp;

char line [200];

int i,n;


fp = fopen(" myfile.txt"," r");

if( !fp)perror(错误打开文件);

else {

i = 0;

while(!feof(fp)) {

if(fgetpos(fp,& position [i])!= 0){

printf(错误获取位置\ n);

退出(1);

}

if(fgets(line,200,fp)== NULL)

打破;

printf("%s",line);

i ++;

}

}

n = i;

printf(" \ n *** output\\\
");

for(i = 0; i< ; n; i ++){

if(fsetpos(fp,& position [i])!= 0){

printf(" error setting position\\\
;);

退出(1);

}

fgets(line,200,fp);

printf("%s",line);

}

fclose(fp);

-----------------------------------------


谢谢,Chris


Hi,
i want to save the keywords of an ini file in a struct, together with a
fpos_t. I think i′m right with the concept, but the access through fsetpos()
doesn′t work. The position is always wrong (except, in this example, the
first line).

My struct looks like this:
***
typedef struct {
char pname[32];
fpos_t pos;
} iniParam;

This routine reads the data and put it to "iniParam param[100]":
***
while (!feof (pFile)) {
fgetpos(pFile, &pos);
c=fgetc(pFile);
ungetc(c, pFile);
if(c == ''#'')
fgets (linebuf, 255, pFile);
else {
param[i].pos = pos;
fgets (linebuf, 255, pFile);
sscanf(linebuf, "%s", param[i].pname);
i++;
}
}

This routine reads the keywords and do a corresponding fscanf:
***
for(i=0; i<n; i++) {
if(strcmp(param[i].pname, "keyword1") == 0) {
pos = param[i].pos;
fsetpos(pFile, &pos);
fscanf(pFile, "%*s %d", &param1);
}
if(strcmp(param[i].pname, "keyword2") == 0) {
...

Don′t know why the access through a fpos_t position doesn′t work.
Thanks for any help, Chris

解决方案

On Wed, 8 Oct 2003 03:26:26 +0200, "C. Sengstock" <sp**@sucks.com>
wrote:

Hi,
i want to save the keywords of an ini file in a struct, together with a
fpos_t. I think i′m right with the concept, but the access through fsetpos()
doesn′t work. The position is always wrong (except, in this example, the
first line).
It works for me with just two problems. The reading loop doesn''t
detect end of file correctly as explained in the faq:

http://www.eskimo.com/~scs/C-faq/q12.2.html

Also blank lines and junk lines in the ini file result in invalid
entries in param[].

My advice is:
1. Check the return code of each library functions you call.
2. Post a complete but minimalist program, including the ini
file contents that demonstrate the problem.
My struct looks like this:
***
typedef struct {
char pname[32];
fpos_t pos;
} iniParam;

This routine reads the data and put it to "iniParam param[100]":
***
while (!feof (pFile)) {
fgetpos(pFile, &pos);
c=fgetc(pFile);
ungetc(c, pFile);
if(c == ''#'')
fgets (linebuf, 255, pFile);
else {
param[i].pos = pos;
fgets (linebuf, 255, pFile);
sscanf(linebuf, "%s", param[i].pname);
i++;
}
}

This routine reads the keywords and do a corresponding fscanf:
***
for(i=0; i<n; i++) {
if(strcmp(param[i].pname, "keyword1") == 0) {
pos = param[i].pos;
fsetpos(pFile, &pos);
fscanf(pFile, "%*s %d", &param1);
}
if(strcmp(param[i].pname, "keyword2") == 0) {
...

Don′t know why the access through a fpos_t position doesn′t work.
Thanks for any help, Chris



Nick.


about
http://www.eskimo.com/~scs/C-faq/q12.2.html
u can
while(fgets(buf, MAXLINE, infp) != NULL && !feof(infp))
{

}

"Nick Austin" <ni**********@nildram.co.uk> wrote in message
news:r3********************************@4ax.com...

On Wed, 8 Oct 2003 03:26:26 +0200, "C. Sengstock" <sp**@sucks.com>
wrote:

Hi,
i want to save the keywords of an ini file in a struct, together with a
fpos_t. I think i′m right with the concept, but the access through fsetpos()doesn′t work. The position is always wrong (except, in this example, the
first line).



It works for me with just two problems. The reading loop doesn''t
detect end of file correctly as explained in the faq:

http://www.eskimo.com/~scs/C-faq/q12.2.html

Also blank lines and junk lines in the ini file result in invalid
entries in param[].

My advice is:
1. Check the return code of each library functions you call.
2. Post a complete but minimalist program, including the ini
file contents that demonstrate the problem.

My struct looks like this:
***
typedef struct {
char pname[32];
fpos_t pos;
} iniParam;

This routine reads the data and put it to "iniParam param[100]":
***
while (!feof (pFile)) {
fgetpos(pFile, &pos);
c=fgetc(pFile);
ungetc(c, pFile);
if(c == ''#'')
fgets (linebuf, 255, pFile);
else {
param[i].pos = pos;
fgets (linebuf, 255, pFile);
sscanf(linebuf, "%s", param[i].pname);
i++;
}
}

This routine reads the keywords and do a corresponding fscanf:
***
for(i=0; i<n; i++) {
if(strcmp(param[i].pname, "keyword1") == 0) {
pos = param[i].pos;
fsetpos(pFile, &pos);
fscanf(pFile, "%*s %d", &param1);
}
if(strcmp(param[i].pname, "keyword2") == 0) {
...

Don′t know why the access through a fpos_t position doesn′t work.
Thanks for any help, Chris



Nick.




"Nick Austin" wrote:

My advice is:
1. Check the return code of each library functions you call.
2. Post a complete but minimalist program, including the ini
file contents that demonstrate the problem.



This is straighter to the point. The code should save the position of a
line, and do the output through that positions. But the code doesn′t work,
the output is:
-----------------------------
#dontreadthis <<< this is the "myfile.txt" data
baud = 4
#nodata

port = 50
parity = 6
<<< till here
*** output
#dontreadthis <<<< that′s the generated output through positions
this
= 4
ta
<<<< that′s the end
------------------------------------
the code looks like this:
-------------------------------------
fpos_t position[50];
FILE* fp;
char line[200];
int i, n;

fp = fopen("myfile.txt", "r");
if(!fp) perror("error opening file");
else {
i=0;
while(!feof(fp)) {
if(fgetpos(fp, &position[i]) != 0) {
printf("error getting position\n");
exit(1);
}
if(fgets(line, 200, fp)==NULL)
break;
printf("%s", line);
i++;
}
}
n=i;
printf("\n*** output\n");
for(i=0; i<n; i++) {
if(fsetpos(fp, &position[i]) != 0) {
printf("error setting position\n");
exit(1);
}
fgets(line, 200, fp);
printf("%s", line);
}
fclose(fp);
-----------------------------------------

Thanks, Chris


这篇关于使用fpos_t直接访问不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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