473,473 Members | 1,492 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

fgetc dropping chars

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
2 2769
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jason K | last post by:
Let me preface this by saying this obviously isn't a C++ *language* issue per se; rather probably an issue relating to quality of implementation, unless I'm just misusing iostream... I wrote a...
8
by: Georg Troxler | last post by:
I try to parse a file. When I read a int with fgetc(f) I get the wrong result. When I open the file with a Editor I see clearly the character should be 0d. In the Memory of f (buffer) it is 0a....
13
by: William L. Bahn | last post by:
I'm sure this has been asked before, and I have looked in the FAQ, but I'm looking for an explanation for the following: The functions pairs: gets()/fgets() puts()/fputs() printf()/fprintf()...
6
by: Kobu | last post by:
Do the "larger" input functions like scanf, gets, fgets use fgetc to take input or an operating system call function like read() (I know it could be any "way", but I'm trying to find out how it's...
8
by: M. Åhman | last post by:
I'm reading "C: A Reference Manual" but still can't understand a very basic thing: is there any functional difference between fgetc/fputc and fread/fwrite (when reading/writing one unsigned char)?...
3
by: cinsky | last post by:
Hi, While reading ISO C Standard, I found follow text in 7.19.8.1: size_t fread(void *restrict ptr, size_t SIZE, ...) ... For each object, SIZE calls are made to the fgetc function and the...
37
by: 01 | last post by:
#include <stdio.h> int main ( int argc, char *argv ) { if ( argc != 2 ) /* argc should be 2 for correct execution */ { /* We print argv assuming it is the program name */ printf( "usage: %s...
4
by: Christopher Benson-Manica | last post by:
In a thread from substantially earlier this week, Harald van D?k <truedfx@gmail.comwrote: Being rather pendantic, I decided to try to verify whether this was true. I would appreciate...
9
by: primeSo | last post by:
// FIRST int main(void){ int c, i = 0; char cArray; while( (c = getchar()) != '\n' && c != EOF){ cArray = c; i ++; }
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.