473,386 Members | 1,699 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,386 software developers and data experts.

Do you see anything wrong with programme ?

I got the following programme from an old post:
http://tinyurl.com/53oa6o

It was given at a job interview and the question was
"The following program works, but what is a
potential problem with it? "

#include <stdio.h>
#include <stdlib.h>

int main() {
char buf[128];
FILE *fp = fopen(__FILE__, "r");

if (!fp) return EXIT_FAILURE;
while(!feof(fp)) {
if (fgets(buf, sizeof buf, fp))
puts(buf);
}
fclose(fp);
return 0;
}

My opinion follows below but you might want
to think about it before scrolling down.







The only problem I see with it is that it
might insert some extra newlines relative
to the original file. If the output of the
programme is meant to be recompiled
this might create problems. Otherwise
it seems fine to me.

Am I missing something ?
Jun 27 '08 #1
4 1136
Spiros Bousbouras wrote:
I got the following programme from an old post:
http://tinyurl.com/53oa6o

It was given at a job interview and the question was
"The following program works, but what is a
potential problem with it? "

#include <stdio.h>
#include <stdlib.h>

int main() {
char buf[128];
FILE *fp = fopen(__FILE__, "r");

if (!fp) return EXIT_FAILURE;
while(!feof(fp)) {
if (fgets(buf, sizeof buf, fp))
puts(buf);
}
fclose(fp);
return 0;
}

My opinion follows below but you might want
to think about it before scrolling down.

[...]

The only problem I see with it is that it
might insert some extra newlines relative
to the original file. If the output of the
programme is meant to be recompiled
this might create problems. Otherwise
it seems fine to me.

Am I missing something ?
A statement of what the program is supposed to do?
We can see what it actually does, and we're told that it
"works," but without knowing what it is supposed to do
how can we tell if there's a "potential problem?" For
example, maybe it's *intended* to split long lines.

The only other things I notice is that the program
tries to keep going after an input error and that it
doesn't check for output errors -- but if these behaviors
are part of the specification, they're not "problems."

--
Er*********@sun.com
Jun 27 '08 #2
In article <d0**********************************@a23g2000hsc. googlegroups.com>,
Spiros Bousbouras <sp****@gmail.comwrote:
>It was given at a job interview and the question was
"The following program works, but what is a
potential problem with it? "

#include <stdio.h>
#include <stdlib.h>

int main() {
char buf[128];
FILE *fp = fopen(__FILE__, "r");

if (!fp) return EXIT_FAILURE;
while(!feof(fp)) {
if (fgets(buf, sizeof buf, fp))
puts(buf);
}
fclose(fp);
return 0;
}
>The only problem I see with it is that it
might insert some extra newlines relative
to the original file.
It certainly inserts a blank line between every line, and others if
any line exceeds the buffer size (which they don't if you're reading
this file, but see below). fgets() goes more naturally with fputs(),
and if you're not interpreting the text as lines it makes more sense
to use fread() and fwrite().
A more drastic problem is that __FILE__ is not guaranteed to refer
to the right file at run-time. If the source has been deleted, or
you're in a different directory, it's not going to work. It might
even refer to a completely different file.

-- Richard
--
:wq
Jun 27 '08 #3
On May 15, 10:46 am, Spiros Bousbouras <spi...@gmail.comwrote:
I got the following programme from an old post:http://tinyurl.com/53oa6o

It was given at a job interview and the question was
"The following program works, but what is a
potential problem with it? "

#include <stdio.h>
#include <stdlib.h>

int main() {
char buf[128];
FILE *fp = fopen(__FILE__, "r");

if (!fp) return EXIT_FAILURE;
while(!feof(fp)) {
if (fgets(buf, sizeof buf, fp))
puts(buf);
}
fclose(fp);
return 0;

}

My opinion follows below but you might want
to think about it before scrolling down.

The only problem I see with it is that it
might insert some extra newlines relative
to the original file. If the output of the
programme is meant to be recompiled
this might create problems. Otherwise
it seems fine to me.

Am I missing something ?
I don't do much file IO stuff, but fgets returns NULL on error. Is it
possible for it to return NULL but not set end of file? If so,
infinite loop...

And yes, puts will double the number of newlines. And lines longer
than
buf (not in that source, but could be) will be broken into pieces.
Seems
like printf is called for here...

-David
Jun 27 '08 #4
In article <fb**********************************@k37g2000hsf. googlegroups.com>,
David Resnick <ln********@gmail.comwrote:
>I don't do much file IO stuff, but fgets returns NULL on error. Is it
possible for it to return NULL but not set end of file?
Yes, if it gets an i/o error instead.
>If so, infinite loop...
That depends on the nature of the error. I don't think the standard
says anything about whether future reads may succeed after a error,
or what they should read (e.g. should they retry to read the same
block of a file?).

-- Richard
--
:wq
Jun 27 '08 #5

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

Similar topics

0
by: cm012b5105 | last post by:
Hello i want to put an interactive text programme in to a graphical box this is a small section of my text programme s = raw_input ("Hello whats your name? ") if s=='melvyn': print "your my...
0
by: melledge | last post by:
Full Programme for XTech 2005 Announced Premier European XML Industry Event Expands Focus to "XML, the Web and Beyond"; Co-hosted by the Mozilla Foundation,W3C, and OASIS, Presenters Include...
2
by: NDAKI MBOULET | last post by:
J'ai un problème pour écrire un programme. Voici mon sujet: Ecrire en c++ un programme qui reçoit en entrée une suite d'instruction encadrées par les mots clés BIBODLE et LISUK dans un langage...
15
by: ben | last post by:
this programme from sedgewick's 'algorithms in c parts 1-4' "is a sample client program that ... uses a symbol table to find the distinct values in a sequence of keys (randomly generated or read...
5
by: katekukku | last post by:
HI, I need the source code for a programme in C. It should have the basic features of a paint programme like circle, line etc, etc,. I lost a programme which wa driven by keyboard, if somebody...
15
by: robert maas, see http://tinyurl.com/uh3t | last post by:
Here's the source: #include <stdio.h> #include <errno.h> main () { char* str = "9999999999"; long long int llin; char* endptr; /* Set by strtoll */ int nch; errno = 0; llin = strtoll(str,...
18
by: arnuld | last post by:
i compiled the "hello world" programme from K&R2: #include<stdio.h> int main() { printf("hello world\n"); }
4
by: arnuld | last post by:
this is my final code. Can you folks advise some improvements for making this programme better? BTW, i aways compile my programme with this cmmand on Linux: g++ -ansi -pedantic -Wall -Wextra...
6
by: =?ISO-8859-1?Q?FERHAT_A=C7ICI?= | last post by:
hi everyone...I want to run any programme or file with my programme.example any file or programme, like this "xxx.ncn".I want If users double click this xxx.ncn this file can run by my...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.