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 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
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) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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....
|
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()...
|
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...
|
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)?...
| |
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...
|
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...
|
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...
|
by: primeSo |
last post by:
// FIRST
int main(void){
int c, i = 0;
char cArray;
while( (c = getchar()) != '\n' && c != EOF){
cArray = c;
i ++;
}
|
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,...
|
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,...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |