LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 11-24-2016, 02:52 PM   #1
YehudaSinger
Member
 
Registered: Jun 2013
Posts: 30

Rep: Reputation: Disabled
fopen


Dear sir/Mrs.

I use the Red-Hat 6.2 on an 4 cores of Intel. The IDE is Eclipse.
The code is:
std::string filename_str;
FILE * fp ;

fp = fopen ( file_name_str.c_str() , "a+" ) ;
if (fp==NULL)
{

cout << " Error in openingfile" ;
}

in run-time the file is created. the value of fp is not null. However, the if statement is executed and the cout statement works.

Best regards,
Yehuda
 
Old 11-24-2016, 03:47 PM   #2
dogpatch
Member
 
Registered: Nov 2005
Location: Central America
Distribution: Mepis, Android
Posts: 490
Blog Entries: 4

Rep: Reputation: 238Reputation: 238Reputation: 238
filename_str != file_name_str
 
Old 11-25-2016, 01:01 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
This is impossible:
Quote:
the value of fp is not null. However, the if statement is executed
if that cout statement was executed than fp must be null.
Checking compiler warnings you could see/recognize the different variable names.
 
Old 11-25-2016, 07:27 AM   #4
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Of course it is going to be NULL you only just created it. Nothing was put into it, therefore it is NULL. You'd be better off it check to see if it has been created, if not, then throw an error.

Last edited by BW-userx; 11-25-2016 at 07:36 AM.
 
1 members found this post helpful.
Old 11-25-2016, 01:44 PM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,649
Blog Entries: 4

Rep: Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934
You've been given some unintended mis-information here. By far the easiest thing to do is to add a printf statement that will print the value of the pointer, cast as an integer.

Also, check the value of the errno variable.

The fact that the message appears, indicates definitively that the value returned was NULL.
 
Old 11-27-2016, 01:33 AM   #6
YehudaSinger
Member
 
Registered: Jun 2013
Posts: 30

Original Poster
Rep: Reputation: Disabled
fopen

Dear sir/Mrs.

I use the Red-Hat 6.2 on an 4 cores of Intel. The IDE is Eclipse.
The code is:
std::string file_name_str;
FILE * fp ;

fp = fopen ( file_name_str.c_str() , "a+" ) ;
if (fp==NULL)
{

cout << " Error in openingfile" ;
}

in run-time the file is created. The value of fp is not null(examined in the debugger). However, the if statement is executed and the cout statement works.

Best regards,
Yehuda
 
Old 11-27-2016, 09:09 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
as it was already told that is impossible. You may have mixed something. Try to use perror instead of that cout to check what's happened.
 
Old 11-27-2016, 09:38 AM   #8
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,649
Blog Entries: 4

Rep: Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934
Instead of relying on a debugger, add print-statements throughout the code to display the values and to trace execution through the various program paths.

I (almost ...) never use a debugger.

For one thing, debuggers are fairly contrary to the actual behavior of any decent optimizing compiler, which will pass values directly in registers whenever possible and which will not (for instance) actually store a value into a variable if it sees that it doesn't have to. (Remember: the generated object-code will be functionally equivalent to, but not necessarily identical to, what your source-code said.)
 
Old 11-27-2016, 02:03 PM   #9
YehudaSinger
Member
 
Registered: Jun 2013
Posts: 30

Original Poster
Rep: Reputation: Disabled
fopen & Debug

Thanks for your answer. When I use the debugger, I set the optimization to NULL. Otherwise, you are right.
Yehuda
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
fopen 2 gb file schneidz Programming 5 10-26-2007 10:24 AM
glibc-2.4 and fopen rajesh_b Programming 0 06-20-2006 12:56 AM
fopen in C simasimon Programming 3 06-07-2006 03:46 PM
fopen+netwrok warney_out Programming 5 05-31-2006 04:14 AM
fopen() bcf2 Programming 3 12-23-2004 04:20 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:25 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration