LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-21-2007, 07:41 AM   #1
yezu
LQ Newbie
 
Registered: Apr 2007
Location: Gdynia, Poland
Distribution: Fedora 10/11
Posts: 22

Rep: Reputation: 15
C/C++ and opening files in UNIX systems (or maybe just Linux ?)


Hello!
I have problem with the fopen() function. In windows if I try to open an unexisting file, the function just returns NULL. Linux seems to treat it as an error, and the program crashes (or just stops with a message). I'm just starting to program in Linux, and I'm not sure how to solve this problem.

I would like to my program to handle the situation if the file does not exist. Should I use another function, catch an exception with 'try' and 'catch' or just to do something with gcc?

thanks

Last edited by yezu; 04-21-2007 at 09:47 AM.
 
Old 04-21-2007, 08:16 AM   #2
maroonbaboon
Senior Member
 
Registered: Aug 2003
Location: Sydney
Distribution: debian
Posts: 1,495

Rep: Reputation: 48
Don't see the problem here...

% cat fopen.c
#include <stdio.h>
main(char **argv, int *argc)
{
FILE *f;
f = fopen("nonexistent.txt", "r");
printf("f = %d\n", (int)f);
}
% cc fopen.c
% ./a.out
f = 0
 
Old 04-21-2007, 09:45 AM   #3
yezu
LQ Newbie
 
Registered: Apr 2007
Location: Gdynia, Poland
Distribution: Fedora 10/11
Posts: 22

Original Poster
Rep: Reputation: 15
Unfortunately the problem exists. Using the same code as You did, I get:

/bin/sh: line 1: 14714 memory security violation ./appname
(I had to translate the message, so I'm not sure if this is fully correct)

Last edited by yezu; 04-21-2007 at 09:48 AM.
 
Old 04-21-2007, 11:06 AM   #4
reddazz
Guru
 
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 73
Moved: This thread is more suitable in the Programming forum and has been moved accordingly to help your thread/question get the exposure it deserves.
 
Old 04-21-2007, 11:27 AM   #5
Matir
Moderator
 
Registered: Nov 2004
Location: San Jose, CA
Distribution: Ubuntu
Posts: 8,507

Rep: Reputation: 117Reputation: 117
I suspect it says "segmentation violation" or something similar. This leads me to suspect that you are trying to do something with the returned null. Can you show us your code?
 
Old 04-22-2007, 07:22 AM   #6
yezu
LQ Newbie
 
Registered: Apr 2007
Location: Gdynia, Poland
Distribution: Fedora 10/11
Posts: 22

Original Poster
Rep: Reputation: 15
Sure, here's the code:

Quote:
FILE *file = fopen("config", "rb");

if(file==NULL)
{
printf("Wrong Configuration File. Using Default.");
}
else
{
fread(current,1,sizeof(Config),file);
}
 
Old 04-22-2007, 07:33 AM   #7
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,481

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
I think the issue is happening after the posted lines, can you post the whole code ?

Alternatively, you can run your program under a debugger and see where it crashes.
 
Old 04-22-2007, 06:58 PM   #8
yezu
LQ Newbie
 
Registered: Apr 2007
Location: Gdynia, Poland
Distribution: Fedora 10/11
Posts: 22

Original Poster
Rep: Reputation: 15
I've got it!

It crashed at fclose(); when there was no file...
Didn't have this problem in win though, a lesson for the future :P


Thanks!
 
  


Reply

Tags
c++, files, gcc, path


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Opening .mpp files in linux jayakrishnan Linux - General 6 07-10-2009 06:55 PM
LXer: Linux replaces Unix on CitiStreet systems LXer Syndicated Linux News 0 11-09-2006 09:54 PM
LXer: Kde flaws put Linux, Unix systems at risk LXer Syndicated Linux News 0 01-20-2006 06:16 PM
Opening Input files with C++ in Linux MDBlueIce Programming 6 08-30-2004 05:38 PM
security flaws threaten unix and linux systems..... Ich_hoffe Linux - Security 1 07-16-2002 06:03 AM


All times are GMT -5. The time now is 09:30 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration