473,769 Members | 4,010 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with sizeof in while loop reading a file

I wrote a small test program to read a file of data and print each
line, but it's only printing the 2nd line out of 3 total lines.

The test file, "foo.txt", has 3 lines:

7388: Zn->Z0 Run coward!
8473: Q1->P2 HAHAHAHAHA!
8381: G3->EVERYONE ok ok ok!

But when I run my progam it only prints the second line:

about to open file for reading
testing if fp stream was opened
entering while loop
about to call fgets
about to call printf
8473: Q1->P2 HAHAHAHAHA!

about to call fgets
about to call printf
closing file
about to exit

Also what does this warning mean and how can I eliminate it:
foo.c: In function 'main':
foo.c:33: warning: incompatible implicit declaration of built-in
function 'exit'

Here is my code:

#include <stdio.h>

int main(void)
{

FILE *fp;
char s[80];

printf("about to open file for reading\n");
fp = fopen("foo.txt" ,"r");
printf("testing if fp stream was opened\n");
if (fp != NULL)
{

printf("enterin g while loop\n");

while ( (fgets(s, sizeof(s), fp)) != NULL)
{
printf("about to call fgets\n");
fgets(s,sizeof( s),fp);
printf("about to call printf\n");
printf("%s\n",s );
}
}

printf("closing file\n");

fclose(fp);

printf("about to exit\n");

exit(0);

}

Lisp 9000

Sep 14 '07 #1
23 2091
In article <11************ **********@r29g 2000hsg.googleg roups.com>,
li******@gmail. com <li******@gmail .comwrote:
while ( (fgets(s, sizeof(s), fp)) != NULL)
Here you call fgets, and don't print the result.
{
printf("about to call fgets\n");
fgets(s,sizeof( s),fp);
Here you call it again.

-- Richard
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Sep 15 '07 #2
On Sep 14, 7:41 pm, rich...@cogsci. ed.ac.uk (Richard Tobin) wrote:
In article <1189813623.280 906.180...@r29g 2000hsg.googleg roups.com>,

lisp9...@gmail. com <lisp9...@gmail .comwrote:
while ( (fgets(s, sizeof(s), fp)) != NULL)

Here you call fgets, and don't print the result.
{
printf("about to call fgets\n");
fgets(s,sizeof( s),fp);

Here you call it again.
Oops. Thanks Richard. And what does this warning mean and how can I
eliminate it:
foo.c: In function 'main':
foo.c:33: warning: incompatible implicit declaration of built-in
function 'exit'

Lisp 9000

Sep 15 '07 #3
"li******@gmail .com" wrote:
>
I wrote a small test program to read a file of data and print each
line, but it's only printing the 2nd line out of 3 total lines.

The test file, "foo.txt", has 3 lines:

7388: Zn->Z0 Run coward!
8473: Q1->P2 HAHAHAHAHA!
8381: G3->EVERYONE ok ok ok!

But when I run my progam it only prints the second line:
.... snip ...
>
Here is my code:

#include <stdio.h>
Where is #include <stdlib.h(for exit function)
>
int main(void) {
FILE *fp;
char s[80];

printf("about to open file for reading\n");
fp = fopen("foo.txt" ,"r");
printf("testing if fp stream was opened\n");
if (fp != NULL) {
printf("enterin g while loop\n");
while ( (fgets(s, sizeof(s), fp)) != NULL) {
printf("about to call fgets\n");
You just called fgets. eliminate this line.
fgets(s,sizeof( s),fp);
And this line. It just discards the line just read.
printf("about to call printf\n");
printf("%s\n",s );
}
}
printf("closing file\n");
fclose(fp);
printf("about to exit\n");
exit(0);
You could use "return 0" here in place of exit.
}
--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Sep 15 '07 #4
On Sat, 15 Sep 2007 00:32:54 -0000, "li******@gmail .com"
<li******@gmail .comwrote in comp.lang.c:
On Sep 14, 7:41 pm, rich...@cogsci. ed.ac.uk (Richard Tobin) wrote:
In article <1189813623.280 906.180...@r29g 2000hsg.googleg roups.com>,

lisp9...@gmail. com <lisp9...@gmail .comwrote:
while ( (fgets(s, sizeof(s), fp)) != NULL)
Here you call fgets, and don't print the result.
{
printf("about to call fgets\n");
fgets(s,sizeof( s),fp);
Here you call it again.

Oops. Thanks Richard. And what does this warning mean and how can I
eliminate it:
foo.c: In function 'main':
foo.c:33: warning: incompatible implicit declaration of built-in
function 'exit'
It means that you should include the standard header that prototypes
the exit() function, namely <stdlib.h>.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Sep 15 '07 #5
On Sep 14, 9:19 pm, CBFalconer <cbfalco...@yah oo.comwrote:
>
Where is #include <stdlib.h(for exit function)
Oops, thought that was provided by <stdio.h:)
You just called fgets. eliminate this line.
....
And this line. It just discards the line just read.
Ah I see that now.
You could use "return 0" here in place of exit.
I was curious about that. How do you know when to use "return 0;" vs
"exit(0);" and does it make a difference?

Lisp 9000

Sep 15 '07 #6
On Sep 14, 11:37 pm, Jack Klein <jackkl...@spam cop.netwrote:
>
It means that you should include the standard header that prototypes
the exit() function, namely <stdlib.h>.
I'll keep that in mind next time. Thanks Jack.
comp.lang.chttp ://c-faq.com/
Did you write the C FAQ? The ASCII version on the website hasn't been
updated in 3 years. Is the FAQ static or will further updates be made?
Just curious.

Lisp 9000

Sep 15 '07 #7
li******@gmail. com wrote, On 15/09/07 07:52:
On Sep 14, 11:37 pm, Jack Klein <jackkl...@spam cop.netwrote:
>It means that you should include the standard header that prototypes
the exit() function, namely <stdlib.h>.

I'll keep that in mind next time. Thanks Jack.
>comp.lang.c http://c-faq.com/

Did you write the C FAQ? The ASCII version on the website hasn't been
updated in 3 years. Is the FAQ static or will further updates be made?
Just curious.
The authors details are on the site. The web version has been updated
fairly recently, the other versions have not.
--
Flash Gordon
Sep 15 '07 #8
<li******@gmail .comschrieb im Newsbeitrag
news:11******** *************@n 39g2000hsh.goog legroups.com...
On Sep 14, 9:19 pm, CBFalconer <cbfalco...@yah oo.comwrote:
<snip>
>You could use "return 0" here in place of exit.

I was curious about that. How do you know when to use "return 0;" vs
"exit(0);" and does it make a difference?
Inside "main" (and inside main only) "return" is equvalent to "exit". Slight
difference: "exit" is a function, so a propor prototy needs to be in scope
and it needs the (), "return" is a keyword and the () are optional.

Bye, Jojo
Sep 15 '07 #9
Joachim Schmitz wrote, On 15/09/07 10:05:
<li******@gmail .comschrieb im Newsbeitrag
news:11******** *************@n 39g2000hsh.goog legroups.com...
>On Sep 14, 9:19 pm, CBFalconer <cbfalco...@yah oo.comwrote:
<snip>
>>You could use "return 0" here in place of exit.
I was curious about that. How do you know when to use "return 0;" vs
"exit(0);" and does it make a difference?
Inside "main" (and inside main only) "return" is equvalent to "exit".
Unless main has been called recursively.
Slight
difference: "exit" is a function, so a propor prototy needs to be in scope
and it needs the (), "return" is a keyword and the () are optional.
Personally I use return from main.
--
Flash Gordon
Sep 15 '07 #10

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

Similar topics

13
2211
by: JasBascom | last post by:
int main() { char record_typec = 'c'; record_typec = toupper(record_typec); * rec->Newcrecord.record_type = record_typec; etc etc \*********************************************\
20
3073
by: ishmael4 | last post by:
hello everyone! i have a problem with reading from binary file. i was googling and searching, but i just cant understand, why isnt this code working. i could use any help. here's the source code: --cut here-- typedef struct pkg_ { short int info; char* data;
8
5229
by: dbuser | last post by:
Hi, I need help on a problem, as described below. I am reading a file "input.txt"which has data like this: abc def gh izk lmnopq rst uvwxyz I am using fstream object to read the file and writing into a dynamic array. My problem is that the array shows extra z and probably because of this further processing gives run time error in borland compiler. Can you please tell me, if the problem is related to handling end-of line , how do i do...
11
6639
by: Abhishek | last post by:
I have a problem transfering files using sockets from pocket pc(.net compact c#) to desktop(not using .net just mfc and sockets 2 API). The socket communication is not a issue and I am able to transfer data across.On the serve I am using Socket 2 API (recv function to read bytes)and not using ..NET. I use FileStream to open the file on the pocket pc, then associate a BinaryReader object with the stream and call ReadBytes to read all the...
2
4453
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c */ #include <time.h>
1
5315
by: xoinki | last post by:
hi experts, I need a little help in debugging this code.. would u pleeze kindly help me? here this program sends a datagram every 10 seconds and on reception it cheks whether the source IP is already written in file or not.. if it is not already entered into the file then a file is opened and that IP is entered. the problem is with this part.. if the filewrite() function is tested independently it is working but in this program...
23
2416
by: Babak | last post by:
Hi Everyone, I've written a standard C code for a simple finite element analysis in MSVC++ . When I save the file as a cpp file, it compiles and runs perfectly, but when I save it as a c file, there are lots of errors and warnings in compiling stage. I am really confused about this problem because I'm not even familiar with C++ and my code only includes simple C functions. Can anybody please tell me what's my mistake? Is there any other...
6
5273
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
9
1934
by: xiao | last post by:
It always dumped when I tried to run it... But it compiles OK. What I want to do is to do a test: Read information from a .dat file and then write it to another file. The original DAT file is like this : (very simple..........) 010001010110001101010101010101010101010101 #include<stdio.h>
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10212
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10047
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9995
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6674
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3563
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.