c program read from file help?

so i’m supposed to write a c program that can read from a file i created with another c program. now, that file contains:
12.000000S
13.000000+
14.000000*

here’s my program that i have so far, i’m not really sure how to make it run, i keep on getting segmentation fault. i’m not sure if theres some sort of statement that will just keep on reading whats in the file until there’s nothing there. anyway… here’s the program (i apologize for the format of it, the tabs don’t come out on here)

# include
int main ()
{
float num, result;
int i;
char op;
FILE *ptr1;
fscanf(ptr1, “%f”, &num);
fscanf(ptr1, “%c”, &op);
switch(op)
{
case ‘S’:
result = num;
fprintf(ptr1, “\n%f\n”, result);
break;
case ‘*’:
result = result*num;
fprintf(ptr1, “\n%f\n”, result);
break;
case ‘+’:
result = result+num;
fprintf(ptr1, “\n%f\n”, result);
break;
case ‘-’:
result = result-num;
fprintf(ptr1, “\n%f\n”, result);
break;
case ‘/’:
result = result/num;
fprintf(ptr1, “\n%f\n”, result);
break;
default:
printf(“\n operator is not valid\n”);
}
fclose(ptr1);
return 0;
}

This entry was posted in Uncategorized and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>