Q: Newbee: Why is this little program not working

  • Thread starter Martin Hvidberg
  • Start date
M

Martin Hvidberg

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=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);


fclose(pInFile);
return 0;
}
 
S

SM Ryan

# 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]++;
# }
 
M

Martin Hvidberg

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=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);


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 ...
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
 
M

Martin Hvidberg

The solution have been supplied by another NG

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

:) Martin

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=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);


fclose(pInFile);
return 0;
}
 
J

Jens.Toerring

Martin Hvidberg said:
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.

ASCII is just up to 127...
It seems that it counts all charecters as ascii=1
What is wrong --- please.
while(c=fgetc(pInFile)!=EOF)

Look at this line again and ask yourself what you really assign to 'c'.
Hint: It's _not_ the return value of fgetc()...

Regards, Jens
 
R

Régis Troadec

Martin Hvidberg said:
Please refere to the source below:
Hi,

[snipped]

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

Because of the precedence of the != operator over the = operator, your
while condition is like c = (fgetc(pInFile) != EOF)) and always evaluates to
true (which explains the value of alph[1] ) until you don't reach the end of
file.

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

/* Note also that a char may be unsigned, and that fgetc returns an int, you
would better declare c as an int */
{
i++;
a = c; // c as integer
alph[a]++;
}

[snipped]

Regis
 
N

Nils Petter Vaskinn

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 ???

Hint 1: What will happen first? '=' or '!='? Look up operator precedence
in you C book.
Hint 2: What is the value of truth?

Hint3:
 
M

Martin Dickopp

Martin Hvidberg said:
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]);

If argv[0] is a null pointer, this invokes undefined behavior.

Otherwise, argv[0] can be a pointer to an empty string, in which case
the user probably wouldn't understand the message.

I consider it better style to write error messages to standard error
instead of standard output.
return 1;

The only portable return values from main are 0, EXIT_SUCCESS, and
EXIT_FAILURE.
}
else
{
if ((pInFile = fopen(argv[1],"r"))==NULL)
{
printf("\n\n ERROR - Can't open InFile %s\n\n",argv[1]);

Same comment as above w.r.t. standard error vs. standard output.
return 2;

Same comment as above w.r.t. return values from main.
}
else printf("> InFile %s open ...\n",argv[1]);
}

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

i=0;
while(c=fgetc(pInFile)!=EOF)


Here's your problem: the while-expression behaves like

c = (fgetc (pInFile) != EOF)

i.e. c will become either 1 (if fgetc returns something different from
EOF, i.e. the comparison yields `true') or 0 (if fgetc returns EOF,
i.e. the comparison yields `false'). You probably want

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

Note also that fgetc returns an int, not a char. The above line
will not work unless you change c to type int.

If fgetc returns EOF, you should check if this is due to an error or due
to end-of-file condition.
{
i++;
a = c; // c as integer

Pointless, as c would have been converted to int in the following line
anyway.

Note that // comments are an error unless you have a C99 compliant
compiler.
alph[a]++;

You should make sure that a is indeed less than 256:

if (a < 256)
alph [a]++;

or better yet:

if (a < sizeof alph / sizeof *alph)
alph [a]++;
}

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


fclose(pInFile);
return 0;
}


Martin
 
C

CBFalconer

Martin said:
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.
.... snip ...

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

If you read the documentation for fgetc you will find it returns
an integer. So to start with your loop should be:

while (EOF != (a = fgetc(pInFile))) {
}

because EOF cannot be stored in a char. Note the use of
parentheses above, which lack is at the root of your problems.
There are no prizes for economizing on blanks either.

--
Some useful references:
<http://www.ungerhu.com/jxh/clc.welcome.txt>
<http://www.eskimo.com/~scs/C-faq/top.html>
<http://benpfaff.org/writings/clc/off-topic.html>
<http://anubis.dkuug.dk/jtc1/sc22/wg14/www/docs/n869/> (C99)
 
A

Arthur J. O'Dwyer

Just wondering. Is it only up to 127? Do you mean the others are from
-128 to -1?

The ASCII character encoding only defines characters in the range
0..127. It's a seven-bit encoding. The 8-bit encoding you're probably
thinking of is an ANSI standard used on Windows machines, but there are
also 8-bit encodings like Latin-1 and various national standards.

Short answer: There are no "others" in ASCII.

Topical answer: Depends on whether 'char' is signed.

-Arthur
 
C

CBFalconer

Arthur J. O'Dwyer said:
.... snip ...

The ASCII character encoding only defines characters in the range
0..127. It's a seven-bit encoding. The 8-bit encoding you're
probably thinking of is an ANSI standard used on Windows machines,
but there are also 8-bit encodings like Latin-1 and various
national standards.

Short answer: There are no "others" in ASCII.

Topical answer: Depends on whether 'char' is signed.

There is no normative mention of ASCII, nor of ANSI, in the
standard that I can find. There is a minimal set of chars,
represented by glyphs, required, but the coding is entirely up to
the implementor. Once this is done the functions in ctype.h need
to be implemented, and they fully govern the classification.
Codes for the characters are constrained by the values in
limits.h.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,141
Messages
2,570,817
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top