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??
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!)