write a C program using data files. the input data Numbers.txt contains several values for the base and power on alternating lines
- برمجة
- برمجة سي c
- 2021-04-04
- daafoor
الأجوبة
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(int argc, char *argv[])
{
//read any text file from currect directory
char const* const fileName = "Numbers.txt";
FILE* file = fopen(fileName, "r");
FILE* fileToWrite = fopen(fileName, "w");
if(!file){
printf("\n Unable to open : %s ", fileName);
return -1;
}
char line[100];
char num1[100];
char num2[100];
int counter=0;
while (fgets(line, sizeof(line), file)) {
if(counter%2==0)
num1=line;
else {
//here we make the power operator then write in Results.txt
num2=line;
fprintf (fileToWrite, "%d^%d=%d\n",num1,num2,pow(atoi(num1),atoi(num2)));
}
counter++;
}
fclose(fileToWrite);
fclose(file);
return 0;
}