473,407 Members | 2,312 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,407 software developers and data experts.

Anything wrong with this code?

I am writing a code to read a file. The file say "foo.txt" is not
generated at the first place. So the code will sit in a tight loop to
wait this file being generated from the 3rd party. I don't concern the
3rd party generation as long as the file is generated eventually.

The code is:

#include <stdio.h>
#include <stdio.h>
int main(){

FILE *fp;

while(1){
fp = fopen("foo.txt", "r"); ;
if (fp != NULL)
break;
else{
printf("Waiting for the file\n");
}
fclose(fp);
}
printf("I am out.\n");
fclose(fp);
return 0;
}

The execution result is:

Waiting for the file
Segmentation fault (core dumped)
What is wrong?

Aug 16 '07 #1
3 1188
In article <11**********************@b79g2000hse.googlegroups .com>,
we*********@gmail.com <we*********@gmail.comwrote:
>#include <stdio.h>
>int main(){
FILE *fp;
while(1){
fp = fopen("foo.txt", "r"); ;
if (fp != NULL)
break;
else{
printf("Waiting for the file\n");
}
fclose(fp);
}
printf("I am out.\n");
fclose(fp);
return 0;
}
If fp *is* NULL, then you do not break, so you fclose(fp)
which is fclose(NULL). That's not defined.
--
If you lie to the compiler, it will get its revenge. -- Henry Spencer
Aug 16 '07 #2
we*********@gmail.com <we*********@gmail.comwrote:
I am writing a code to read a file. The file say "foo.txt" is not
generated at the first place. So the code will sit in a tight loop to
wait this file being generated from the 3rd party. I don't concern the
3rd party generation as long as the file is generated eventually.
The code is:
#include <stdio.h>
#include <stdio.h>
int main(){
FILE *fp;
while(1){
fp = fopen("foo.txt", "r"); ;
if (fp != NULL)
break;
else{
printf("Waiting for the file\n");
}
fclose(fp);
This line is definitely not correct: you're not allowed to
call fclose() with a NULL pointer (and you can only get her
if 'fp' is NULL). Doing so invokes undefined behaviour and
getting a segmentation fault is one possible outcome.
}
printf("I am out.\n");
fclose(fp);
return 0;
}
The execution result is:
Waiting for the file
Segmentation fault (core dumped)
Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Aug 16 '07 #3
"we*********@gmail.com" wrote:
>
.... snip ...
>
#include <stdio.h>
#include <stdio.h>

int main() {
FILE *fp;

while (1) {
fp = fopen("foo.txt", "r"); ;
if (fp != NULL) break;
else printf("Waiting for the file\n");
fclose(fp);
}

printf("I am out.\n");
fclose(fp);
return 0;
}

The execution result is:
Waiting for the file
Segmentation fault (core dumped)
What is wrong?
(I removed some extraneous braces.) It is not legal to pass NULL
to fclose. The NULL signifies that fopen failed. The scheme will
probably fail anyhow.

You might try for a considerable reduction in paper/terminal
display space with a simpler overall code in a function:

putc('\n');
while (! (fp = fopen("foo.txt", "r")) {
printf("\rWaiting for the file"); fflush(stdout);
/* optional delay - allow other things to run */
}
printf("\nGot it\n");
return fp;

and call that with:

fp = awaitfile(...);
/* play with file */
fclose(fp);

but worry about how to abort.

--
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

Aug 17 '07 #4

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

Similar topics

4
by: Chris | last post by:
Could anyone please help me. IS there something wrong with it? char * function getString() { char myString; sprintf(myString, "hello world");
6
by: greenflame | last post by:
I have been working for some time on a script that will show a matrix with the elements aligned on the right for sometime and finally got it to work. Then I did some patching up and ran the script...
2
by: atv | last post by:
Hi, is there something wrong with this code? It seems that it crashes at the else { clause, when allocating a second record. Excuse for the indentation, the g_warnings are from gtk. Also, i'm...
3
by: Roubles | last post by:
Hi All, Here's my problem, I have a bunch of code that passes an allocated object (say obj) to a function, and then dereferences that allocated object when the function returns: foo(obj);...
4
by: QQ | last post by:
Hi Here is part of my code typedef struct{ int len; char code; }Code; typedef struct{ .... Code *a;
83
by: Anonymous | last post by:
Came across some code summarized as follows: char const* MyClass::errToText(int err) const { switch (err) { case 0: return "No error"; case 1: return "Not enough"; case 2: return "Too...
28
by: hijkl | last post by:
hey guys anything wrong with this code?? if it is then what? int *array(int n){ return new int(n); } int main(){ int *p = array(10); for( int i = 0; i < 10; i++ ) {
10
by: Jim Langston | last post by:
I use a game engine using MSVC++ .net 2003 and have no problems. Some users of DevC++ who use the same engine crash at times when a copy of this structure is the return variable. I don't have...
4
by: beebob | last post by:
Hi all, I have just changed my JDK, from 1.5.0 to 1.6.0, and also change my Netbeans, from NB 4.0 to NB 5.5. I've got a problem with my code, which I wrote on NB 4.0 + JDK 1.5.0.. I fail to put...
19
by: =?Utf-8?B?anZjb2FjaDIz?= | last post by:
I've got a global.aspx file that works in my dev environment (vs 2005). When i publish the site to a windows 2000 sp4 box running IIS, the global does not seem to fire. Since it's a test server,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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
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
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
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...
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,...

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.