修改文件内容时使用fgets()和sscanf()的问题 [英] problems using fgets() and sscanf() while modifying file contents

查看:105
本文介绍了修改文件内容时使用fgets()和sscanf()的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,这是reddy,初学者来说,这里我有一些问题

在阅读和修改文件内容时,希望你能帮忙

解决了这个问题。在这里我附上要修改的文件和

程序代码。

在下面的附件中我只想更改

的值第1行PVT 1 15之后的数据(仅浮点值),直到2 G TT,

从正到负,反之亦然,并将日期连接到其他

文件中。有人可以帮我解决这个问题。

谢谢。

*** MEAS(T,L)数据***

/ 07 / 07

T

8

PINION

3

2

2

119.6329

-113.2538

74.8409

2.0000

1 PVT 1 15

20 -6.2

21 -4.0

22 0.7

1 PVL 1 5

23 3.9

24 -5.1

25 5.2

26 -5.7

1 PVT 3 15

27 5.9

28 -5.2

2 G TT

1 1.2488

3 1.2598

END

程序代码:

do {


if(c = fgets(file1,sizeof file1,in1)!=''\ n''

&& sscanf(file1,"%d%f",& num, & value)== 2)

{

value =(value-(2 * value));

printf("%) d%1.1f \ n",num,value);

fprintf(append3,"%d%1.1f \ n",num,value);

}

else

{

for(j = 0 ;; j ++){

printf("%c" ,file1 [j]);

fprintf(file2,"%c",file1 [j]);

if(file1 [j] ==''\\ \\ n'');} $


} while(!feof(in1)); >
返回EXIT_SUCCESS;

解决方案

allpervas ... @ gmail.com写道:


[...]


在下面的附件中我只想更改

数据的值(仅限浮点值)在第1行PVT 1 15之后直到2 G TT,

从正到负,反之亦然,并在其他

文件中连接日期。有人可以帮我解决这个问题。

谢谢。


*** MEAS(T,L)数据***

/ 07/07

T

8

PINION

3

2

2

119.6329

-113.2538

74.8409

2.0000

1 PVT 1 15

20 -6.2

21 -4.0

22 0.7

1 PVL 1 5

23 3.9

24 -5.1

25 5.2

26 -5.7

1 PVT 3 15

27 5.9

28 -5.2

2 G TT

1 1.2488

3 1.2598

END


程序代码:



这不是一个程序。这只是一个代码片段。


do {


if(c = fgets(file1,sizeof file1,in1) !=''\ n''



fgets返回空指针,如果文件结束或发生错误,

否则它返回file1。在这里你要检查它的返回值

对换行字符。这个测试的结果,1或0

为真或者错误被分配给c.EYYM其他东西。


&& sscanf(file1,"%d%f",& num,& value )== 2)

{

value =(value-(2 * value));

printf("%d%1.1 f \ n",num,value);

fprintf(append3,"%d%1.1f \ n",num,value);

}

else

{

for(j = 0 ;; j ++){

printf("%c" ,file1 [j]);

fprintf(file2,"%c",file1 [j]);

if(file1 [j] ==''\ n'')中断;}


}


} while(!feof(in1));



您需要检查fgets调用是否返回空指针。这可能是因为两个文件结束,(如果feof()返回true则为true),或者

其他错误,(如果ferror()返回true,则为true) )。所以在这里你应该检查

两种结果。


< snip>


< blockquote>你也用空指针检查了fgets,但是没有

更改,

实际上我能够从+更改数据(浮点值)反之亦然,反之亦然,但问题在于即使行前的数据也是如此。

" 1 PVT 115"并且在2 G TT行之后如下所示变为
,所以我需要一个更好的方法来解决这个bug。谢谢


原始数据:


*** MEAS(T,L)数据***

/ 07/07

..

..

119.6329

-113.2538

74.8409

2.0000

1 PVT 1 15

20 -6.2

21 -4.0

22 0.7

..

..

2 G TT

1 1.2488

3 1.2598

结束


修改后的数据:


*** MEAS( T,L)DATA ***

/ 07/07

..

..

119 - 0.6

-113 -0.3

74 -0.8

2 0.0

1 PVT 1 15

20 6.2

21 4.0

22 -0.7

..

..

2 G TT

1 -1.2

3 -1.3

END

END


allpervas ... @ gmail.com写道:


ya我使用null检查了fgets指针也,但没有

ch ange ,,

实际上iam能够将数据(浮点值)从+ ve更改为-ve和

反之亦然但问题是即使是之前的数据line

" 1 PVT 1 15"并且在2 G TT行之后如下所示变为
,所以我需要一个更好的方法来解决这个bug。谢谢



< snip>


我会建议一般的算法:


读取每行数据文件

如果行匹配所需的模式,(在这种情况下,1 P VT 1 15)

如果下一行_does not_ match closing pattern,(即2 G

TT)

将线转换为适当的数值

更改值想要改变

写出修改后的值

从上面的第三步重复


基本上你需要输入你的主要修改循环只有在你检测到关闭

模式后,才会检测到开盘模式并退出。在开始模式之前和结束模式之后,你需要b / b
写出未经修改的文件。我建议将

修改代码封装到一个单独的函数中。使用string.h中的标准字符串函数可以检测到模式




hi all, this is reddy, a beginner to c lang,,here i have some problems
in reading and modifying the contents of a file,, hope you can help to
solve this problem. Here i attach the file to be modified and the
program code.
In the attached file below i just want to change the value of
data(only float value) after the line 1 P V T 1 15 till 2 G TT,
from positive to negative and vice versa, and wire the date in other
file. can someone help me to solve this.
thanks.
*** MEAS(T,L) DATA ***
/07/07

T
8
PINION
3
2
2
119.6329
-113.2538
74.8409
2.0000
1 P V T 1 15
20 -6.2
21 -4.0
22 0.7
1 P V L 1 5
23 3.9
24 -5.1
25 5.2
26 -5.7
1 P V T 3 15
27 5.9
28 -5.2
2 G TT
1 1.2488
3 1.2598
END
program code:
do {

if (c=fgets(file1, sizeof file1, in1) !=''\n''
&& sscanf(file1,"%d %f",&num,&value) == 2)
{
value= (value- (2*value));
printf("%d %1.1f\n", num, value);
fprintf(append3,"%d %1.1f\n", num, value);
}
else
{
for(j=0;;j++){
printf("%c",file1[j]);
fprintf(file2,"%c",file1[j]);
if(file1[j]==''\n'')break;}

}

} while (!feof(in1));

return EXIT_SUCCESS;

解决方案

allpervas...@gmail.com wrote:

[ ... ]

In the attached file below i just want to change the value of
data(only float value) after the line 1 P V T 1 15 till 2 G TT,
from positive to negative and vice versa, and wire the date in other
file. can someone help me to solve this.
thanks.

*** MEAS(T,L) DATA ***
/07/07

T
8
PINION
3
2
2
119.6329
-113.2538
74.8409
2.0000
1 P V T 1 15
20 -6.2
21 -4.0
22 0.7
1 P V L 1 5
23 3.9
24 -5.1
25 5.2
26 -5.7
1 P V T 3 15
27 5.9
28 -5.2
2 G TT
1 1.2488
3 1.2598
END
program code:

This is not a program. This is just a code snippet.

do {

if (c=fgets(file1, sizeof file1, in1) !=''\n''

fgets returns a null pointer, if end-of-file or an error occurs,
otherwise it returns file1. Here you''re checking it''s return value
against the newline character. The result of this test, either 1 or 0
for true or false gets assigned to c. ITYM something else.

&& sscanf(file1,"%d %f",&num,&value) == 2)
{
value= (value- (2*value));
printf("%d %1.1f\n", num, value);
fprintf(append3,"%d %1.1f\n", num, value);
}
else
{
for(j=0;;j++){
printf("%c",file1[j]);
fprintf(file2,"%c",file1[j]);
if(file1[j]==''\n'')break;}

}

} while (!feof(in1));

You need to check the fgets call for a null pointer return. That could
be because of both end-of-file, (true if feof() returns true), or
other error, (true if ferror() returns true). So here you should check
for both outcomes.

<snip>


ya i checked the fgets using null pointer also,, but the there is no
change,,
actually iam able to change the data(float value) from +ve to -ve and
vice-versa but the problem is that even the data before the line
"1 P V T 1 15" and after the line "2 G TT" gets changed as shown
below, so i need a better approach to solve that bug. thanks

original data:

*** MEAS(T,L) DATA ***
/07/07
..
..
119.6329
-113.2538
74.8409
2.0000
1 P V T 1 15
20 -6.2
21 -4.0
22 0.7
..
..
2 G TT
1 1.2488
3 1.2598
END


modified data:

*** MEAS(T,L) DATA ***
/07/07
..
..
119 -0.6
-113 -0.3
74 -0.8
2 0.0
1 P V T 1 15
20 6.2
21 4.0
22 -0.7
..
..
2 G TT
1 -1.2
3 -1.3
END
END


allpervas...@gmail.com wrote:

ya i checked the fgets using null pointer also,, but the there is no
change,,
actually iam able to change the data(float value) from +ve to -ve and
vice-versa but the problem is that even the data before the line
"1 P V T 1 15" and after the line "2 G TT" gets changed as shown
below, so i need a better approach to solve that bug. thanks

<snip>

I''ll suggest a general algorithm:

Read each line of data file
If line matches desired pattern, (in this case, "1 P VT 1 15")
If next line _does not_ match closing pattern, (i.e., "2 G
TT")
Convert the line into appropriate numerical values
Change the value you want to change
Write out the modified values
Repeat from step three above

Basically you need to enter your main modification loop only after you
detect the opening pattern and exit it after you detect the closing
pattern. Before the opening pattern and after the closing pattern you
should write out the file unmodified. I suggest encapsulating the
modification code into a separate function. The patterns can be
detected with the standard string functions in string.h.


这篇关于修改文件内容时使用fgets()和sscanf()的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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