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

what's wrong with this

rsk
Hi Friends,

Can you plzz tell me whats wrong with this code.

#include <stdio.h>
#include <math.h>

main () {

FILE *fp;

fp = fopen("file.txt", "r");
if(fp==NULL){
printf("\n Cannot open file.txt file.\n");
exit(1);
}
else {
printf("\n file.txt File is Opened.\n");

}
}

The file.txt is not having any value but surprisingly iam getting the
following message when i run the above code

file.txt File is Opened.

Can you please tell me why iam getting this?

Thanks in advance.
ss..
--
Message posted using http://www.talkaboutprogramming.com/group/comp.lang.c/
More information at http://www.talkaboutprogramming.com/faq.html

Sep 28 '07 #1
11 1624

"rsk" <kr******@yahoo.co.inschrieb im Newsbeitrag
news:ee******************************@localhost.ta lkaboutprogramming.com...
Hi Friends,

Can you plzz tell me whats wrong with this code.

#include <stdio.h>
#include <math.h>

main () {
int main(void)
>
FILE *fp;

fp = fopen("file.txt", "r");
if(fp==NULL){
printf("\n Cannot open file.txt file.\n");
exit(1);
}
else {
printf("\n file.txt File is Opened.\n");

}
return 0;
}

The file.txt is not having any value but surprisingly iam getting the
? You mean that file is empty? Or it doesn't exist?
following message when i run the above code

file.txt File is Opened.

Can you please tell me why iam getting this?
Because it got opened successfully. Which means that the file existed before
(or your implementation has a bug)
Bye, Jojo
Sep 28 '07 #2

"Joachim Schmitz" <no*********@schmitz-digital.deschrieb im Newsbeitrag
news:fd**********@online.de...
>
"rsk" <kr******@yahoo.co.inschrieb im Newsbeitrag
news:ee******************************@localhost.ta lkaboutprogramming.com...
>Hi Friends,

Can you plzz tell me whats wrong with this code.

#include <stdio.h>
#include <math.h>

main () {
int main(void)
>>
FILE *fp;

fp = fopen("file.txt", "r");
if(fp==NULL){
printf("\n Cannot open file.txt file.\n");
exit(1);
}
else {
printf("\n file.txt File is Opened.\n");

}
return 0;
>}

The file.txt is not having any value but surprisingly iam getting the
? You mean that file is empty? Or it doesn't exist?
>following message when i run the above code

file.txt File is Opened.

Can you please tell me why iam getting this?
Because it got opened successfully. Which means that the file existed
before (or your implementation has a bug)
7.19.5.3-4
Opening a file with read mode ('r' as the first character in the mode
argument) fails if the file does not exist or cannot be read.
Sep 28 '07 #3
rsk wrote:
Hi Friends,

Can you plzz tell me whats wrong with this code.

#include <stdio.h>
#include <math.h>
You don't use anything in math.h
main () {
Better:
int main(void) { /* ... */ }
FILE *fp;

fp = fopen("file.txt", "r");
if(fp==NULL){
printf("\n Cannot open file.txt file.\n");
exit(1);
One is not a portable status value. Use one of 0, EXIT_SUCCESS or
EXIT_FAILURE. The latter two are defined in stdlib.h. The first two are
more or less interchangeable.
}
else {
printf("\n file.txt File is Opened.\n");

}
And return a value from main.
}

The file.txt is not having any value but surprisingly iam getting the
following message when i run the above code

file.txt File is Opened.

Can you please tell me why iam getting this?
Are you sure the file did not exist. Or does it exist, but is empty. If the
former case, then your implementation is broken, but it's much probably the
latter case.
Sep 28 '07 #4
rsk
hi Friends,

Actually the file.txt exists but is not having any value.

Actually my requirement is to check whether the files file.txt exists or
not and then to see whether it has any data with it.

For that how should i modify my code?

Thanks in advance.
ss...

--
Message posted using http://www.talkaboutprogramming.com/group/comp.lang.c/
More information at http://www.talkaboutprogramming.com/faq.html

Sep 28 '07 #5
"rsk" <kr******@yahoo.co.inschrieb im Newsbeitrag
news:dd******************************@localhost.ta lkaboutprogramming.com...
hi Friends,

Actually the file.txt exists but is not having any value.

Actually my requirement is to check whether the files file.txt exists or
not and then to see whether it has any data with it.
if fopen("file.txt", "r") succeeds, the file exists and you'r allowed to
read it
if you can fread() more than 0 bytes, it contains some data

in POSIX there's stat(), which would check existence and at the same time
tell you everything about the file you ever wanted to know, including it's
lenght

Bye, Jojo
Sep 28 '07 #6
"rsk" <kr******@yahoo.co.inwrites:
Can you plzz tell me whats wrong with this code.
"please", not "plzz", please.
#include <stdio.h>
#include <math.h>

main () {

FILE *fp;

fp = fopen("file.txt", "r");
if(fp==NULL){
printf("\n Cannot open file.txt file.\n");
exit(1);
}
else {
printf("\n file.txt File is Opened.\n");

}
}

The file.txt is not having any value but surprisingly iam getting the
following message when i run the above code

file.txt File is Opened.

Can you please tell me why iam getting this?
Others have pointed out the various errors in your code; you should
fix them, but they're probably not causing the problem you're asking
about.

When I read this, I had no idea what you meant by "The file.txt is not
having any value". From the followups, I think you really meant that
file.txt is empty. An empty file is perfectly valid, and is a very
different thing from a file that doesn't exist.

If you want to know whether the file is empty, try to read some data
from it; if you immediately hit the end of the file, it's empty.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Sep 28 '07 #7
In <dd******************************@localhost.talkab outprogramming.com"rsk" <kr******@yahoo.co.inwrites:
Actually the file.txt exists but is not having any value.
Actually my requirement is to check whether the files file.txt exists or
not and then to see whether it has any data with it.
For that how should i modify my code?
If fopen("file.txt", "r") succeeds, then the file exists. Otherwise, it
does not.

If a single fgetc() succeeds, then the file has some data in it. If not,
then it doesn't.

--
John Gordon A is for Amy, who fell down the stairs
go****@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

Sep 28 '07 #8
In article <fd**********@reader1.panix.com>,
John Gordon <go****@panix.comwrote:
>Actually my requirement is to check whether the files file.txt exists or
not and then to see whether it has any data with it.
>If fopen("file.txt", "r") succeeds, then the file exists. Otherwise, it
does not.
.... or maybe it exists but you don't have the necessary permission to
read it, or any of a zillion other reasons. But quite likely it's the
right thing to do anyway, and it's the requirement that's imprecise.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Sep 28 '07 #9
On Fri, 28 Sep 2007 20:31:06 +0000, John Gordon wrote:
In <dd******************************@localhost.talkab outprogramming.com"rsk" <kr******@yahoo.co.inwrites:
>Actually the file.txt exists but is not having any value.
>Actually my requirement is to check whether the files file.txt exists or
not and then to see whether it has any data with it.
>For that how should i modify my code?

If fopen("file.txt", "r") succeeds, then the file exists. Otherwise, it
does not.

If a single fgetc() succeeds, then the file has some data in it. If not,
then it doesn't.
Or better: otherwise, if feof() is true, the file is empty;
if ferror() is true, there was an I/O error.

--
Army1987 (Replace "NOSPAM" with "email")
A hamburger is better than nothing.
Nothing is better than eternal happiness.
Therefore, a hamburger is better than eternal happiness.

Sep 29 '07 #10
Army1987 wrote:
John Gordon wrote:
.... snip ...
>
>If a single fgetc() succeeds, then the file has some data in it.
If not, then it doesn't.

Or better: otherwise, if feof() is true, the file is empty;
if ferror() is true, there was an I/O error.
No, because feof only shows the result after getc() (or equivalent)
has returned EOF.

--
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 29 '07 #11
CBFalconer wrote:
Army1987 wrote:
>John Gordon wrote:
... snip ...
>>
>>If a single fgetc() succeeds, then the file has some data in it.
If not, then it doesn't.

Or better: otherwise, if feof() is true, the file is empty;
if ferror() is true, there was an I/O error.

No, because feof only shows the result after getc() (or equivalent)
has returned EOF.
I think that's what Army1987 meant, i.e., if feof returns true after fgetc
fails, then the file can be considered empty, otherwise if ferror is true,
then an I/O error occurred.

Sep 30 '07 #12

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

Similar topics

3
by: Mike Henley | last post by:
I first came across rebol a while ago; it seemed interesting but then i was put off by its proprietary nature, although the core of the language is a free download. Recently however, i can't...
72
by: E. Robert Tisdale | last post by:
What makes a good C/C++ programmer? Would you be surprised if I told you that it has almost nothing to do with your knowledge of C or C++? There isn't much difference in productivity, for...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
28
by: Madhur | last post by:
Hello what about this nice way to open a file in single line rather than using if and else. #include<stdio.h> void main() { FILE *nd; clrscr();...
9
by: Pyenos | last post by:
import cPickle, shelve could someone tell me what things are wrong with my code? class progress: PROGRESS_TABLE_ACTIONS= DEFAULT_PROGRESS_DATA_FILE="progress_data" PROGRESS_OUTCOMES=
3
by: Siong.Ong | last post by:
Dear all, my PHP aims to update a MySQL database by selecting record one by one and modify then save. Here are my PHP, but I found that it doesnt work as it supposed to be, for example, when...
89
by: Tubular Technician | last post by:
Hello, World! Reading this group for some time I came to the conclusion that people here are split into several fractions regarding size_t, including, but not limited to, * size_t is the...
20
by: Daniel.C | last post by:
Hello. I just copied this code from my book with no modification : #include <stdio.h> /* count characters in input; 1st version */ main() { long nc; nc = 0;
24
by: MU | last post by:
Hello I have some code that sets a dropdownlist control with a parameter from the querystring. However, when the querystring is empty, I get an error. Here is my code: Protected Sub...
2
by: mingke | last post by:
Hi... So I have problem with my if condition..I don't know what's wrong but it keeps resulting the wrong answer.... So here's the part of my code I have problem with: for (i=0; i<size2;...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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...

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.