getc() only returns 1500 chars on a 5 Meg file

S

spike

I use this code to read number of characters
from a file.

The problem is: It only returns about 1500 chars.
If i open the file in a text editor it contains
almoust 3 million rows. Sure it must contain more than
1500 characters? Is it because some characters are not readable?
The file is not a textfile but a half-life demo file. (binary)

Whats the problem?

What I want to do eventually is to search for the string "/name/"
and read the string that comes after that.

Any idea?

Source code:
------------------------------------------------------
#include <stdio.h>

int main()
{
FILE *fp;
fp = fopen("demo.dem","r");
if(fp == NULL)
{
perror("Error");
}
else
{
char tkn;
int i=0;
while(!(feof(fp)))
{
tkn = getc(fp);
printf("%c",tkn);
i++;
}
printf("Found %d characters", i); // Says 1500 characters on a 5 meg file
fclose(fp);
}
return 0;
}
------------------------------------------------------
 
L

Leor Zolman

int main()
{
FILE *fp;
fp = fopen("demo.dem","r");

If the file is binary, open it in binary mode:

fp = fopen("demo.dem", "rb");

It is probably finding a ^Z byte and thinking that is EOF.
-leor

if(fp == NULL)
{
perror("Error");
}
else
{
char tkn;
int i=0;
while(!(feof(fp)))
{
tkn = getc(fp);
printf("%c",tkn);
i++;
}
printf("Found %d characters", i); // Says 1500 characters on a 5 meg file
fclose(fp);
}
return 0;
}

Leor Zolman
BD Software
(e-mail address removed)
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
 
A

Artie Gold

spike said:
I use this code to read number of characters
from a file.

The problem is: It only returns about 1500 chars.
If i open the file in a text editor it contains
almoust 3 million rows. Sure it must contain more than
1500 characters? Is it because some characters are not readable?
The file is not a textfile but a half-life demo file. (binary)

Whats the problem?

What I want to do eventually is to search for the string "/name/"
and read the string that comes after that.

Any idea?

Source code:
------------------------------------------------------
#include <stdio.h>

int main()
{
FILE *fp;
fp = fopen("demo.dem","r");
if(fp == NULL)
{
perror("Error");
}
else
{
char tkn;
int i=0;
while(!(feof(fp)))
{
tkn = getc(fp);
printf("%c",tkn);
i++;
}
printf("Found %d characters", i); // Says 1500 characters on a 5 meg file
fclose(fp);
}
return 0;
}
------------------------------------------------------

It's a FAQ (well, actually at least two -- for a start):

http://www.eskimo.com/~scs/C-faq/q12.1.html

http://www.eskimo.com/~scs/C-faq/q12.2.html

As long as you're there, why not read the whole section (at the very least).

If you're *still* having problems, by all means post back here.

HTH,
--ag
 
C

CBFalconer

spike said:
I use this code to read number of characters
from a file.

The problem is: It only returns about 1500 chars.
If i open the file in a text editor it contains
almoust 3 million rows. Sure it must contain more than
1500 characters? Is it because some characters are not readable?
The file is not a textfile but a half-life demo file. (binary)

Whats the problem?

What I want to do eventually is to search for the string "/name/"
and read the string that comes after that.

Source code:

int main(void) is better.
{
FILE *fp;
int tkn;
size_t i=0;

add the above 2 lines. I don't think you have a C99 compiler.
fp = fopen("demo.dem","r");
if(fp == NULL)
{
perror("Error");
}
else
{
char tkn;
int i=0;

Delete the above two lines.
while(!(feof(fp)))
{
tkn = getc(fp);

while (EOF != (tkn = getc(fp))) {

replaces the above 3 lines.
printf("%c",tkn);
i++;
}
printf("Found %d characters", i); // Says 1500 characters on a 5 meg file

printf("Found %lu chars", (unsigned long)i);

Don't use // comments unless you have a C99 compiler, and don't
use them in newsgroup postings anyhow.
fclose(fp);
}
return 0;
}
------------------------------------------------------

Your fundamental problems are misuse of feof, getc returns an int,
and possible overflow. Your warning level is set far too low.

You could add a test for (i < ULONG_MAX) within the read loop and
break out with overflow if that occurs. Alternatively you could
increment an overflow counter and carry on, since unsigned
arithmetic is always modulo arithmetic.
 
A

Artie Gold

CBFalconer said:
int main(void) is better.



int tkn;
size_t i=0;

add the above 2 lines. I don't think you have a C99 compiler.
Why?


Delete the above two lines.

Why? Declaring/defining variables at the beginning of a block was just
as valid in C89 as it is in C99.
while (EOF != (tkn = getc(fp))) {

replaces the above 3 lines.




printf("Found %lu chars", (unsigned long)i);

Don't use // comments unless you have a C99 compiler, and don't
use them in newsgroup postings anyhow.

Now *that's* (somewhat) reasonable.
Your fundamental problems are misuse of feof, getc returns an int,
and possible overflow. Your warning level is set far too low.

You could add a test for (i < ULONG_MAX) within the read loop and
break out with overflow if that occurs. Alternatively you could
increment an overflow counter and carry on, since unsigned
arithmetic is always modulo arithmetic.
--ag
 
J

Jack Klein

I use this code to read number of characters
from a file.

The problem is: It only returns about 1500 chars.
If i open the file in a text editor it contains
almoust 3 million rows. Sure it must contain more than
1500 characters? Is it because some characters are not readable?
The file is not a textfile but a half-life demo file. (binary)

Rule Number 1: Open binary files in binary mode.

Rule Number 2: Don't ever forget rule number 1.

Rule Number 3: See rule number 2.
 
A

August Derleth

Jack said:
Rule Number 1: Open binary files in binary mode.

Rule Number 2: Don't ever forget rule number 1.

Rule Number 3: See rule number 2.

[OT]
Rule Number 4: Get a real OS.
[/OT]
 
P

Peter Shaggy Haywood

Groovy hepcat spike was jivin' on 22 Feb 2004 13:30:50 -0800 in
comp.lang.c.
getc() only returns 1500 chars on a 5 Meg file's a cool scene! Dig it!
I use this code to read number of characters
from a file.

The problem is: It only returns about 1500 chars.
If i open the file in a text editor it contains
almoust 3 million rows. Sure it must contain more than
1500 characters? Is it because some characters are not readable?
The file is not a textfile but a half-life demo file. (binary)

Whats the problem?

READ THE FAQ (http://www.eskimo.com/~scs/C-faq/top.html) and then
open your file in binary mode!

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
M

Martien Verbruggen

I use this code to read number of characters
from a file.

The problem is: It only returns about 1500 chars.
The file is not a textfile but a half-life demo file. (binary)
^^^^^^
Whats the problem?
FILE *fp;
fp = fopen("demo.dem","r");
^^^

Maybe you should open the file in binary mode?
while(!(feof(fp)))

And you shouldn't do this.

See the C FAQ at http://www.eskimo.com/~scs/C-faq/top.html.

question 12.2, and while you're there, check 12.38 as well, and maybe
all the other questions as well. It's good information.

Martien
 

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

Staff online

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top