问题描述:

I opened a memory stream using fmemopen() in mode 'w+'. I wrote some data tho this file and then I am trying to read the data to count no. of lines written. But I am getting garbage value by accessing this file using fgets(). My code is huge so I am just writing chunks of it here to give you an idea what I am doing.

This is how I opened the stream:

char buf_sn[10000];

size_t len = 10000;

TokenStream = fmemopen(buf_sn, len, "w+");

if (TokenStream == NULL) {

printf("Unable to Open Token Stream for processing\n\r");

return FALSE;

}

This is where I am writing to the stream:

fprintf(TokenStream,"%.*s %d %ld\n",(tok_index_l), token_s, tok_type_l,line_no_l);

printf("\nToken::%.*s\n",(tok_index_l),token_s);

fflush (TokenStream);

Now when I read data directly from the buffer I get correct output:

printf ("\n\n\nbuf=\n%slen=%ld\n", buf_sn, len);

Output:

. 2 1

section 1 1

. 2 1

init 1 1

. 2 2

globl 1 2

_ 2 2

start 1 2

MOVI 1 4

R0 1 4

, 2 4

0x20000000 4 4

MOVI 1 5

R1 1 5

, 2 5

0x00200000 4 5

ORR 1 6

R0 1 6

, 2 6

R0 1 6

, 2 6

R1 1 6

, 2 6

0 1 6

MOVI 1 8

R1 1 8

, 2 8

0x00040000 4 8

STRI 1 9

R1 1 9

, 2 9

R0 1 9

, 2 9

4 3 9

MOVI 1 11

R1 1 11

, 2 11

0x00010000 4 11

loop 1 13

: 2 13

STRI 1 13

R1 1 13

, 2 13

R0 1 13

, 2 13

40 3 13

MOVI 1 15

R2 1 15

, 2 15

0 1 15

wait1 1 16

: 2 16

ADDI 1 16

R2 1 16

, 2 16

R2 1 16

, 2 16

1 3 16

CMPI 1 17

R2 1 17

, 2 17

0x00400000 4 17

BNE 1 18

wait1 1 18

STRI 1 20

R1 1 20

, 2 20

R0 1 20

, 2 20

28 3 20

MOVI 1 22

R2 1 22

, 2 22

0 1 22

wait2 1 23

: 2 23

add 1 23

r2 1 23

, 2 23

r2 1 23

, 2 23

# 2 23

1 3 23

CMPI 1 24

R2 1 24

, 2 24

0x00400000 4 24

BNE 1 25

wait2 1 25

BAL 1 27

loop 1 27

But when I access this same stream using fgets() I get garbage at the end:

rewind(TokenStream); //Reset the TokenStream Pointer to the Start of the Stream.

while(fgets(line_read_sn,sizeof(line_read_sn), TokenStream) != NULL) {

printf("%ld>> %s\n",total_no_of_tokens_l, line_read_sn);

total_no_of_tokens_l++;

}

Output:

0>> . 2 1

1>> section 1 1

2>> . 2 1

3>> init 1 14>> . 2 2

5>> globl 1 2

6>> _ 2 2

7>> start 1 2

8>> MOVI 1 4

9>> R0 1 4

10>> , 2 4

11>> 0x20000000 4 4

12>> MOVI 1 5

13>> R1 1 5

14>> , 2 5

15>> 0x00200000 4 5

16>> ORR 1 6

17>> R0 1 6

18>> , 2 6

19>> R0 1 6

20>> , 2 6

21>> R1 1 6

22>> , 2 6

23>> 0 1 6

24>> MOVI 1 8

25>> R1 1 8

26>> , 2 8

27>> 0x00040000 4 8

28>> STRI 1 9

29>> R1 1 9

30>> , 2 9

31>> R0 1 9

32>> , 2 9

33>> 4 3 9

34>> MOVI 1 11

35>> R1 1 11

36>> , 2 11

37>> 0x00010000 4 11

38>> loop 1 13

39>> : 2 13

40>> STRI 1 13

41>> R1 1 13

42>> , 2 13

43>> R0 1 13

44>> , 2 13

45>> 40 3 13

46>> MOVI 1 15

47>> R2 1 15

48>> , 2 15

49>> 0 1 15

50>> wait1 1 16

51>> : 2 16

52>> ADDI 1 16

53>> R2 1 16

54>> , 2 16

55>> R2 1 16

56>> , 2 16

57>> 1 3 16

58>> CMPI 1 17

59>> R2 1 17

60>> , 2 17

61>> 0x00400000 4 17

62>> BNE 1 18

63>> wait1 1 18

64>> STRI 1 20

65>> R1 1 20

66>> , 2 20

67>> R0 1 20

68>> , 2 20

69>> 28 3 20

70>> MOVI 1 22

71>> R2 1 22

72>> , 2 22

73>> 0 1 22

74>> wait2 1 23

75>> : 2 23

76>> add 1 23

77>> r2 1 23

78>> , 2 23

79>> r2 1 23

80>> , 2 23

81>> # 2 23

82>> 1 3 23

83>> CMPI 1 24

84>> R2 1 24

85>> , 2 24

86>> 0x00400000 4 24

87>> BNE 1 25

88>> wait2 1 25

89>> BAL 1 27

90>> loop 1 27

91>>

92>>

93>>

94>>

95>>

96>>

97>>

98>>

99>>

100>>

101>>

102>>

103>>

104>>

105>>

106>>

107>>

108>>

109>>

110>>

111>>

112>>

113>>

114>>

115>>

116>>

117>> XA

118>>

119>>

120>>

121>> `=

122>>

123>>

124>> *

125>> =

126>> t=

127>>

128>>

129>>

130>>

Please help me out what is being done wrong? As in between I am not doing anything with the stream so why am I getting this output? More importantly what can I do to fix it? Thanks

网友答案:

When you create a stream in memory with fmemopen() you give it a buffer of 10000 bytes. When you write to it fflush() does not insert anything, it will only inform the underlying system that buffers should be written to storage, which in this case is memory.

Then when you rewind and start reading fgets() will return non-NULL values as long as it can read something. Since your buffer is 10000 bytes and you're not writing that much, there is random noise to be read after your data.

You will have to write some kind of an end value to show that the actual data is over. If every line has something written on it, then the end marked can be as simple as a nul character, which makes fgets() to read an empty line and you can simply check that.

相关阅读:
Top