fgetc dropping chars

M

Mark

i'm trying to read a file one char at a time into a char[10] array thusly...

char buffer[10];

while (readChars(InputFile, buffer, BUFFER_SIZE) != 0) {
//not doing anything atm
}

int readChars(FILE *InputFile, char buffer[], int readCount)
{
int charsRead = 0;

char ch;
while (((ch=fgetc(InputFile)) != EOF) && (charsRead<readCount)){
//buffer[charsRead] = ch;

//buffer++;
charsRead++;

printf("%d:%c\n", charsRead, ch);
}

return charsRead;
}

readCount is always 10. so say the file contains one line like so...

fdjksfljdsklfdsnm,fndm,

it prints...

1:f
2:d
3:j
4:k
5:s
6:f
7:l
8:j
9:d
10:s
1:l
....

so it's skipping the 11th char, which is k, but i can't figure out what
i'm doing wrong. changing the size of readCount and buffer makes no
difference nor does chaning it to a for loop.

any ideas?

TIA
 
A

Artie Gold

Mark said:
i'm trying to read a file one char at a time into a char[10] array
thusly...

char buffer[10];

while (readChars(InputFile, buffer, BUFFER_SIZE) != 0) {
//not doing anything atm
}

int readChars(FILE *InputFile, char buffer[], int readCount)
{
int charsRead = 0;

char ch;

No! fgetc() returns an int -- otherwise the `EOF' test can never work.
But that's not your problem (yet).
while (((ch=fgetc(InputFile)) != EOF) && (charsRead<readCount)){

OK. Think of what happens when you read the 11th character: You read the
character, but because `charsRead' is no longer less than `readCount',
you exit the loop; hence this character is not printed.
//buffer[charsRead] = ch;

//buffer++;
charsRead++;

printf("%d:%c\n", charsRead, ch);
}

return charsRead;
}

readCount is always 10. so say the file contains one line like so...

fdjksfljdsklfdsnm,fndm,

it prints...

1:f
2:d
3:j
4:k
5:s
6:f
7:l
8:j
9:d
10:s
1:l
...

so it's skipping the 11th char, which is k, but i can't figure out what
i'm doing wrong. changing the size of readCount and buffer makes no
difference nor does chaning it to a for loop.

Right. See above.
A simple `desk check' would have revealed this to you.

HTH,
--ag
 
C

CBFalconer

Mark said:
i'm trying to read a file one char at a time into a char[10] array
thusly...

char buffer[10];

while (readChars(InputFile, buffer, BUFFER_SIZE) != 0) {
//not doing anything atm
}

int readChars(FILE *InputFile, char buffer[], int readCount)
{
int charsRead = 0;
char ch;

while (((ch=fgetc(InputFile)) != EOF) && (charsRead<readCount)) {

replace above line with:

while ((charsRead < readCount) &&
(EOF != (ch = fgetc(InputFile))) {

notice the alteration of the order of tests. You might want to add
a clause of the form "&& ('\n' !- ch)" after the fgetc clause.
charsRead++;
printf("%d:%c\n", charsRead, ch);
}
return charsRead;
}

readCount is always 10. so say the file contains one line like so...

fdjksfljdsklfdsnm,fndm,
.... snip ...

so it's skipping the 11th char, which is k, but i can't figure
out what i'm doing wrong. changing the size of readCount and
buffer makes no difference nor does chaning it to a for loop.

See alteration above.

--
Some useful references about C:
<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)
<http://www.dinkumware.com/refxc.html> (C-library}
<http://gcc.gnu.org/onlinedocs/> (GNU docs)
 

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

Similar Threads

Fibonacci 0
My Status, Ciphertext 2
C language. work with text 3
Adding adressing of IPv6 to program 1
Blue J Ciphertext Program 2
error 7
Command Line Arguments 0
fgetc 37

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,150
Latest member
MakersCBDReviews
Top