问:Newbee:为什么这个小程序无效 [英] Q: Newbee: Why is this little program not working

查看:93
本文介绍了问:Newbee:为什么这个小程序无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参考下面的来源:


该程序应该读取s ascii文本文件并计算每个的

出现次数256个ascii代码。结果显示在

屏幕上。


似乎它将所有字符统计为ascii = 1


有什么不对 - 请。


--- 8< ---


/ *返回代码

0:成功

1:缺少命令行参数

2:错误打开文件

3:打开文件时出错* /


#include< stdio.h>

#include< stdlib.h>

#include< string.h> ;


int main(int argc,char * argv [])

{

FILE * pInFile;

int i,alph [256],a;

char c;


if(argc!= 2)

{

printf(" \ n用法:%s< Infile> \ n",argv [0]);

返回1;

}

其他

{

if((pInFile = fopen(argv [1]," r") )== NULL)

{

printf(" \ n \ n ERROR - 无法打开InFile%s \ n \ n, argv [1]);

返回2;

}

else printf("> InFile%s open ... \ n",argv [1]);

}


for(i = 0; i< = 255; i ++)

{

alph [i] = 0;

}


i = 0;

while(c = fgetc(pInFile)!= EOF)

{

i ++;

a = c; // c为整数

alph [a] ++;

}


printf(" \\\
Found% d chars。,i);

for(i = 0; i< = 255; i ++)printf(" \ n%d:\ t%d,i,alph [i]);

fclose(pInFile);

返回0;

}


-

使用M2,Opera的革命性电子邮件客户端: http://www.opera.com/m2/

Please refere to the source below:

The program is supposed to read s ascii text file and count the number of
occurences of each of the 256 ascii codes. The result is presented on
screen.

It seems that it counts all charecters as ascii=1

What is wrong --- please.

---8<---

/* Return codes
0: Succes
1: command line parameters missing
2: Error opening In file
3: Error opening Out file */

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

int main(int argc, char *argv[])
{
FILE *pInFile;
int i, alph[256], a;
char c;

if (argc!=2)
{
printf("\n Usage: %s <Infile>\n",argv[0]);
return 1;
}
else
{
if ((pInFile = fopen(argv[1],"r"))==NULL)
{
printf("\n\n ERROR - Can''t open InFile %s\n\n",argv[1]);
return 2;
}
else printf("> InFile %s open ...\n",argv[1]);
}

for (i=0;i<=255;i++)
{
alph[i]=0;
}

i=0;
while(c=fgetc(pInFile)!=EOF)
{
i++;
a = c; // c as integer
alph[a]++;
}

printf("\n Found %d chars.",i);
for (i=0;i<=255;i++) printf("\n %d:\t%d",i,alph[i]);
fclose(pInFile);
return 0;
}

--
Using M2, Opera''s revolutionary e-mail client: http://www.opera.com/m2/

推荐答案

#i = 0;

#while(c = fgetc(pInFile)!= EOF)

#{


(1)你有没有验证上面给c指定了什么值,perhap by

fprintf(stderr,i =%dc =%d \ n,i,d);


(2)如果你正在使用gcc打开所有警告,在while谓词上是否有任何

诊断?

#i ++;

#a = c; // c为整数

#alph [a] ++;

#}


-

SM Ryan http://www.rawbw.com/~wyrmwif/

我今天甚至不应该在这里。
# i=0;
# while(c=fgetc(pInFile)!=EOF)
# {

(1) Have you verified what value is assigned to c above, perhap by
fprintf(stderr,"i=%d c=%d\n",i,d);

(2) If you are using gcc with all warnings turned on, are there any
diagnostics on the while predicate?

# i++;
# a = c; // c as integer
# alph[a]++;
# }

--
SM Ryan http://www.rawbw.com/~wyrmwif/
I''m not even supposed to be here today.


亲爱的SM Ryan


感谢您的回复。


请在下面找到:

1)插入线路后整个程序是什么样子

)编译器输出

3)在包含

ABCDEFGHIJKLMNOPQRSTUVWXYZ的文件上运行时的程序输出


仍然没有像我希望的那样做。它似乎认定enything为

ascii = 1 ???

第39行 - 正如编译器中所提到的那样:

而(c = fgetc(pInFile)!= EOF)

所以我猜它可能是变量c没有得到

第一名的核心价值??? br />

:-) Martin


1)

--- 8< ---


#include< stdio.h>

#include< stdlib.h>

#include< string.h>


int main(int argc,char * argv [])

{

FILE * pInFile;

int i,alph [256];

char c;


if(argc!= 2)

{

printf(" \ n用法:%s< Infile> \ n",argv [0]);

返回1;

}

else

{

if((pInFile = fopen(argv [1]," r"))== NULL)

{

printf(\ n \ n错误 - 无法打开InFile%s \ n \ nn,argv [1]);

返回2;

}

else printf("> InFile%s open ... \ n",argv [1]);

}


for(i = 0; i< = 255; i ++)

{

alph [i] = 0;

}


i = 0;

while(c = fgetc(pInFile)!= EOF)

{

fprintf(stderr," i =%dc =%d \ n",i,c);

i ++;

alph [c] ++;

}


printf(" \ n Found%d chars。",i);

for(i = 0; i< = 255; i ++)printf(" \\ \\ n%d:\ t%d",i,alph [i]);

fclose(pInFile);

返回0;

}

2)

--- 8< ---


Borland C ++ 5.5.1 for Win32 Copyright( c)1993年,2000年Borland

D:\ 2go \ Guru-C\charstat.c:

警告W8060 D:\ 2go \ Guru-C \ charstat.c 39:可能不正确的任务

函数main

Turbo Incremental Link 5.00版权所有(c)1997,2000 Borland


工具成功完成


3)

--- 8&l t; ---
Dear SM Ryan

Thanks for your reply.

Please find below:
1) What the entire program looks like with your line inserted
2) The compiler output
3) the program output when run on a file containing
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"

It''s still not doing like I hoped. It seems to identify enything as
ascii=1 ???
Line 39 - as mentioned in the compiler repsonce is:
while(c=fgetc(pInFile)!=EOF)
so I guess it could be that variable c don''t get the corect value in the
first place ???

:-) Martin

1)
---8<---

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

int main(int argc, char *argv[])
{
FILE *pInFile;
int i, alph[256];
char c;

if (argc!=2)
{
printf("\n Usage: %s <Infile>\n",argv[0]);
return 1;
}
else
{
if ((pInFile = fopen(argv[1],"r"))==NULL)
{
printf("\n\n ERROR - Can''t open InFile %s\n\n",argv[1]);
return 2;
}
else printf("> InFile %s open ...\n",argv[1]);
}

for (i=0;i<=255;i++)
{
alph[i]=0;
}

i=0;
while(c=fgetc(pInFile)!=EOF)
{
fprintf(stderr,"i=%d c=%d\n",i,c);
i++;
alph[c]++;
}

printf("\n Found %d chars.",i);
for (i=0;i<=255;i++) printf("\n %d:\t%d",i,alph[i]);
fclose(pInFile);
return 0;
}
2)
---8<---

Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
D:\2go\Guru-C\charstat.c:
Warning W8060 D:\2go\Guru-C\charstat.c 39: Possibly incorrect assignment
in function main
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

Tool completed successfully

3)
---8<---
InFile AZ.txt open ...
InFile AZ.txt open ...



i = 0 c = 1

i = 1 c = 1

i = 2 c = 1

i = 3 c = 1

i = 4 c = 1

i = 5 c = 1

i = 6 c = 1

i = 7 c = 1

i = 8 c = 1

i = 9 c = 1

i = 10 c = 1

i = 11 c = 1

i = 12 c = 1

i = 13 c = 1

i = 14 c = 1

i = 15 c = 1

i = 16 c = 1

i = 17 c = 1

i = 18 c = 1

i = 19 c = 1

i = 20 c = 1

i = 21 c = 1

i = 22 c = 1

i = 23 c = 1

i = 24 c = 1

i = 25 c = 1

找到26个字符。

0:0

1:26

2:0

3:0

4:0

5:0

6:0

7:0

8:0

9:0

10:0

11:0

12:0

13:0

14:0

15:0

16:0

17:0

18:0

19:0

20:0

21:0

22:0

23:0

24:0

25:0

26:0
27:0

28:0

29:0

30:0

31 :0

32:0

33:0

34:0

35:0

36:0

37:0

38:0

39:0

40: 0

41:0

42:0

43:0

44:0

45:0

46:0

47:0

48:0

49:0

50:0

51:0

52:0

53:0

54:0

55:0

56:0

57:0

58:0

59:0

60:0

61:0

62:0

63:0

64:0

65:0

66:0

67:0

68:0

69:0

70:0

71:0

72:0

73:0

74:0

75:0

76:0
77:0

78:0

79:0

80:0

81:0

82:0

83:0

84:0

85:0

86:0

87:0

88:0

89:0

90:0

91:0

92:0

93:0

94:0

95:0

96:0

97:0

98:0

99:0

100:0

101:0

102:0

103:0

104:0

105:0

106:0

107:0

108:0

109:0

110:0

111:0

112:0
113:0

114:0

115:0

116:0

117 :0

118:0

119:0

120:0

121:0

122:0

123:0

124:0

125:0

126: 0

127:0

128:0

129:0

130:0

131:0

132:0

133:0

134:0

135:0

136:0

137:0

138:0

139:0

140:0

141:0

142:0

143:0

144:0

145:0

146:0

147:0

148:0

149:0

150:0

151:0

152:0

153:0

154:0

155:0
156:0

157:0

158:0

159:0

160 :0

161:0

162:0

163:0

164:0

165:0

166:0

167:0

168:0

169: 0

170:0

171:0

172:0

173:0

174:0

175:0

176:0

177:0

178:0

179:0

180:0

181:0

182:0

183:0

184:0

185:0

186:0

187:0

188:0

189:0

190:0

191:0

192:0

193:0

194:0

195:0

196:0

197:0
198:0

199:0

200:0

201:0

202 :0

203:0

204:0

205:0

206:0

207:0

208:0

209:0

210:0

211: 0

212:0

213:0

214:0

215:0

216:0

217:0

218:0

219:0

220:0

221:0

222:0

223:0

224:0

225:0

226:0

227:0

228:0

229:0

230:0

231:0

232:0

233:0

234:0

235:0

236:0

237:0

238:0

239:0

240:0

241:0

242:0

243:0

244:0

245:0

246:0

247:0
248:0

249:0

250:0

251:0

252:0

253:0

254:0

255:0


i=0 c=1
i=1 c=1
i=2 c=1
i=3 c=1
i=4 c=1
i=5 c=1
i=6 c=1
i=7 c=1
i=8 c=1
i=9 c=1
i=10 c=1
i=11 c=1
i=12 c=1
i=13 c=1
i=14 c=1
i=15 c=1
i=16 c=1
i=17 c=1
i=18 c=1
i=19 c=1
i=20 c=1
i=21 c=1
i=22 c=1
i=23 c=1
i=24 c=1
i=25 c=1

Found 26 chars.
0: 0
1: 26
2: 0
3: 0
4: 0
5: 0
6: 0
7: 0
8: 0
9: 0
10: 0
11: 0
12: 0
13: 0
14: 0
15: 0
16: 0
17: 0
18: 0
19: 0
20: 0
21: 0
22: 0
23: 0
24: 0
25: 0
26: 0
27: 0
28: 0
29: 0
30: 0
31: 0
32: 0
33: 0
34: 0
35: 0
36: 0
37: 0
38: 0
39: 0
40: 0
41: 0
42: 0
43: 0
44: 0
45: 0
46: 0
47: 0
48: 0
49: 0
50: 0
51: 0
52: 0
53: 0
54: 0
55: 0
56: 0
57: 0
58: 0
59: 0
60: 0
61: 0
62: 0
63: 0
64: 0
65: 0
66: 0
67: 0
68: 0
69: 0
70: 0
71: 0
72: 0
73: 0
74: 0
75: 0
76: 0
77: 0
78: 0
79: 0
80: 0
81: 0
82: 0
83: 0
84: 0
85: 0
86: 0
87: 0
88: 0
89: 0
90: 0
91: 0
92: 0
93: 0
94: 0
95: 0
96: 0
97: 0
98: 0
99: 0
100: 0
101: 0
102: 0
103: 0
104: 0
105: 0
106: 0
107: 0
108: 0
109: 0
110: 0
111: 0
112: 0
113: 0
114: 0
115: 0
116: 0
117: 0
118: 0
119: 0
120: 0
121: 0
122: 0
123: 0
124: 0
125: 0
126: 0
127: 0
128: 0
129: 0
130: 0
131: 0
132: 0
133: 0
134: 0
135: 0
136: 0
137: 0
138: 0
139: 0
140: 0
141: 0
142: 0
143: 0
144: 0
145: 0
146: 0
147: 0
148: 0
149: 0
150: 0
151: 0
152: 0
153: 0
154: 0
155: 0
156: 0
157: 0
158: 0
159: 0
160: 0
161: 0
162: 0
163: 0
164: 0
165: 0
166: 0
167: 0
168: 0
169: 0
170: 0
171: 0
172: 0
173: 0
174: 0
175: 0
176: 0
177: 0
178: 0
179: 0
180: 0
181: 0
182: 0
183: 0
184: 0
185: 0
186: 0
187: 0
188: 0
189: 0
190: 0
191: 0
192: 0
193: 0
194: 0
195: 0
196: 0
197: 0
198: 0
199: 0
200: 0
201: 0
202: 0
203: 0
204: 0
205: 0
206: 0
207: 0
208: 0
209: 0
210: 0
211: 0
212: 0
213: 0
214: 0
215: 0
216: 0
217: 0
218: 0
219: 0
220: 0
221: 0
222: 0
223: 0
224: 0
225: 0
226: 0
227: 0
228: 0
229: 0
230: 0
231: 0
232: 0
233: 0
234: 0
235: 0
236: 0
237: 0
238: 0
239: 0
240: 0
241: 0
242: 0
243: 0
244: 0
245: 0
246: 0
247: 0
248: 0
249: 0
250: 0
251: 0
252: 0
253: 0
254: 0
255: 0


该解决方案由另一个NG提供/>

将approtriate行改为:

while((c = fgetc(pInFile))!= EOF)

全部运行为意图


:-) Martin


2004年5月3日星期一14:51:39 +0200,Martin Hvidberg< Ma ** **@Hvidberg.net>

写道:
The solution have been supplied by another NG

chenge the approtriate line to:
while( (c = fgetc(pInFile)) != EOF )
The all runs as intented

:-) Martin

On Mon, 03 May 2004 14:51:39 +0200, Martin Hvidberg <Ma****@Hvidberg.net>
wrote:
请参考以下来源:

该程序应该读取s ascii文本文件并计算256个ascii代码中每个的出现次数。结果显示在
屏幕上。

似乎它将所有字符统计为ascii = 1

出了什么问题---请。

--- 8< ---

/ *返回代码
0:成功
1:缺少命令行参数
2:打开错误文件
3:打开文件时出错* /

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

int main(int argc,char * argv [])
{/> FILE * pInFile;
int i,alph [256],a;
char c;

if(argc!= 2)
{/> printf(" \ n用法:%s< Infile> \ n" ,argv [0]);
返回1;
}
其他
{
if((pInFile = fopen(argv [1]," r") )== NULL)
{/> printf(\ n \ n错误 - 无法打开InFile%s \ n \\ nn,argv [1]);
返回2;
}
其他printf("> InFile%s open ... \ n",argv [1]);
}
for(i = 0; i <= 255; i ++)
{al / [i] = 0;
}

i = 0;
while(c = fgetc(pInFile)!= EOF)
{
i ++;
a = c; // c为整数
alph [a] ++;
}
printf(" \ n Found%d chars。",i);
for(i = 0; i< = 255; i ++)printf(" \ n%d:\ t%d",i,alph [i]);

fclose(pInFile );
返回0;
}
Please refere to the source below:

The program is supposed to read s ascii text file and count the number
of occurences of each of the 256 ascii codes. The result is presented on
screen.

It seems that it counts all charecters as ascii=1

What is wrong --- please.

---8<---

/* Return codes
0: Succes
1: command line parameters missing
2: Error opening In file
3: Error opening Out file */

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

int main(int argc, char *argv[])
{
FILE *pInFile;
int i, alph[256], a;
char c;

if (argc!=2)
{
printf("\n Usage: %s <Infile>\n",argv[0]);
return 1;
}
else
{
if ((pInFile = fopen(argv[1],"r"))==NULL)
{
printf("\n\n ERROR - Can''t open InFile %s\n\n",argv[1]);
return 2;
}
else printf("> InFile %s open ...\n",argv[1]);
}

for (i=0;i<=255;i++)
{
alph[i]=0;
}

i=0;
while(c=fgetc(pInFile)!=EOF)
{
i++;
a = c; // c as integer
alph[a]++;
}

printf("\n Found %d chars.",i);
for (i=0;i<=255;i++) printf("\n %d:\t%d",i,alph[i]);
fclose(pInFile);
return 0;
}




-

使用M2,Opera''革命性的电子邮件客户端: http://www.opera.com/m2/


这篇关于问:Newbee:为什么这个小程序无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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