By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,397 Members | 1,393 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,397 IT Pros & Developers. It's quick & easy.

fgetc dropping chars

P: n/a
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
Nov 14 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Mark wrote:
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
--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Nov 14 '05 #2

P: n/a
Mark wrote:

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)
Nov 14 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.