Reputation: 215
char* output= (char*) argv[2];
92 fp = fopen(output, "w");
93 if( fp = NULL )
94 {
95 printf("writing output failed");
96 return 0;
97 }
98 fprintf(fp,"hello");
This is causing seg fault exc bad memory at line 98. What am I missing??
Upvotes: 0
Views: 3163
Reputation: 42215
Line 93
if( fp = NULL )
is assigning fp to NULL rather than comparing it to NULL. Use
if( fp == NULL )
instead.
As netcoder pointed out, your compiler should warn you about this. You could also write your test in the form if (NULL == fp)
to generate a compiler error if you accidentally swap comparison for assignment. (Note that some people may find this style of coding distasteful so it may generate some complaints in code reviews!)
Upvotes: 5
Reputation: 27589
Your line
if( fp = NULL)
is assigning the value of NULL
to fp
, instead of comparing.
You should be using
if( fp == NULL)
Upvotes: 8