LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 12-28-2008, 08:45 AM   #1
amolgupta
Member
 
Registered: Feb 2005
Location: agra,india
Distribution: FC2
Posts: 158

Rep: Reputation: 30
my c programming crashes on call of fopen


and debugger shows
Program received signal SIGSEGV, Segmentation fault.
0x0000003e45a6cc50 in _IO_new_file_fopen () from /lib64/libc.so.6

what i think is fopen should either return null pointer or a pointer to the file.its crashing doesn't really makes sense.code compiles well.
Code:
#include <stdio.h>
int main (int argc,char *arg[]){
printf("%d\n",argc);
if(argc!=3){
printf("usage smst  <input terminal points file > <output file >\n this appliaction produces seprable mininmum spanning tree .\nit reads the input from tha input file \nand write the out put to the specified output file\n ");
exit(1);
}
FILE *f;
int *x,*y,n,i;
printf("opening file %s",arg[1]);
if((f=(FILE *)fopen(arg[1],'r'))==NULL) { // open a file
printf("could not open input file\n"); // print an error
exit(1);
}
fscanf("%d\n",&n);
x=(int*)malloc(sizeof(int)*n);
y=(int*)malloc(sizeof(int)*n);
for (i=0;i<n;i++){
fscanf(f,"%d\t%d\n",x+n,y+n);
printf("%d/t%d\n",x[n],y[n]);
}
fclose(f);
return 0;
}
can it be that the libraries are corrupt
if you try to run it,the program requires two parameter
one input file and one out put file.but that is not relevant because the program crashes at fopen. i have checked it via debugger.first input must be file to be opened and second input can be any file name or any thing
 
Old 12-28-2008, 09:45 AM   #2
clvic
Member
 
Registered: Feb 2008
Location: Rome, Italy
Distribution: OpenSuSE 11.x, vectorlinux, slax, Sabayon
Posts: 206
Blog Entries: 2

Rep: Reputation: 45
The prototype of the function fopen is:
FILE *fopen(const char *path, const char *mode);

so that the second argument must be a string. That is, try changing from:

fopen(arg[1],'r')

to

fopen(arg[1],"r")

note that I only changed ' to ". This should explain the crash
 
Old 12-28-2008, 11:35 AM   #3
amolgupta
Member
 
Registered: Feb 2005
Location: agra,india
Distribution: FC2
Posts: 158

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by clvic View Post
The prototype of the function fopen is:
FILE *fopen(const char *path, const char *mode);

so that the second argument must be a string. That is, try changing from:

fopen(arg[1],'r')

to

fopen(arg[1],"r")

note that I only changed ' to ". This should explain the crash
thanx it works
 
  


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
Function call overhead (C programming) exvor Programming 4 10-09-2006 05:51 PM
How linux generates call trace when the machine crashes. asurya Linux - General 1 02-09-2006 05:52 PM
What programming call is like 'df' ? netstv Programming 3 08-23-2004 06:05 PM
a CGI written in C, problem with an fopen() system call Sm0k3 Programming 2 10-03-2003 02:18 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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