473,395 Members | 2,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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;
}
------------------------------------------------------
Nov 14 '05 #1
9 1493
On 22 Feb 2004 13:30:50 -0800, jo**@ljungh.se (spike) wrote:
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
le**@bdsoft.com
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
Nov 14 '05 #2
spike wrote:
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

--
Artie Gold -- Austin, Texas

"Yeah. It's an urban legend. But it's a *great* urban legend!"
Nov 14 '05 #3
spike wrote:

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:
------------------------------------------------------
#include <stdio.h>

int main()
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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #4
CBFalconer wrote:
spike wrote:
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:
------------------------------------------------------
#include <stdio.h>

int main()

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.


Why?
fp = fopen("demo.dem","r");
if(fp == NULL)
{
perror("Error");
}
else
{
char tkn;
int i=0;

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(!(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.


Now *that's* (somewhat) reasonable.
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.

--ag
--
Artie Gold -- Austin, Texas

"Yeah. It's an urban legend. But it's a *great* urban legend!"
Nov 14 '05 #5
Artie Gold wrote:
CBFalconer wrote:
spike wrote:
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:
------------------------------------------------------
#include <stdio.h>

int main()
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.

Why?

fp = fopen("demo.dem","r");
if(fp == NULL)
{
perror("Error");
}
else
{
char tkn;
int i=0;


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.

(though `tkn' needs to be an `int') <sheepishly crawling away>

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.

Now *that's* (somewhat) reasonable.

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.

--ag

--
Artie Gold -- Austin, Texas

"Yeah. It's an urban legend. But it's a *great* urban legend!"
Nov 14 '05 #6
On 22 Feb 2004 13:30:50 -0800, jo**@ljungh.se (spike) wrote in
comp.lang.c:
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #7
Jack Klein wrote:
On 22 Feb 2004 13:30:50 -0800, jo**@ljungh.se (spike) wrote in
comp.lang.c:

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.


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

--
My address is yvoregnevna gjragl-guerr gjb-gubhfnaq guerr ng lnubb qbg pbz
Note: Rot13 and convert spelled-out numbers to numerical equivalents.
Nov 14 '05 #8
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"?
Nov 14 '05 #9
On 22 Feb 2004 13:30:50 -0800,
spike <jo**@ljungh.se> wrote:
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
--
|
Martien Verbruggen | I took an IQ test and the results were
Trading Post Australia | negative.
|
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Michael Loomington | last post by:
Couldn't find anything here http://www.python.org/doc/2.3.2/lib/string-methods.html I need to cycle through lines in a file that contain words and compare it to a string thats given, call it SSS...
5
by: wolfgang haefelinger | last post by:
Greetings, I'm trying to read (japanese) chars from a file. While doing so I encounter that a char with length 2 is returned. Is this to be expected or is there something wrong? Basically...
4
by: Andrew Kibler | last post by:
Two sections of code, in the first one fwrite works, in the second one it doesn't (ms VC++) both are identical except in the working one fseek is used twice to set the file pointer, once just...
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()...
8
by: Bill Cunningham | last post by:
Would getc and ungetc be the best and most simple what to parse expressions for a parser? Bill
11
by: TTroy | last post by:
Hello C programmers, Can someone tell me why ungetc can't sent back EOF, but it's sister function getc has no trouble sending it to us? For a file, this might not make a difference, but for an...
19
by: mailursubbu | last post by:
HI, Below is my program. I compiled it through g++. Now strange thing is, getc is not reading the data instead its printing the previously read data ! Please some one let me know whats wrong. ...
7
by: hzmonte | last post by:
My C program has the following: static int skip_space(FILE *fp, int *line, int c) { int i = 0; if(feof(fp)) { printf("in skip feof ...\n"); } printf("in skip start fp=%p line=%d c=%d\n", fp,...
32
by: vippstar | last post by:
Assuming all the values of int are in the range of unsigned char, what happends if getc returns EOF? Is it possible that EOF was the value of the byte read? Does that mean that code aiming for...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.