FILE I/O question in C Hello everyone.
I have a question regarding FILE I/O in c.
I have a text file that contains numbers
like this:
(---in the text file----)
1 22 4
what i want to do is to read the numbers in stored in that text file as "integer"
not as string and then use arithmetic operators (+, -, *, /) on those numbers. usually when I read numbers from a text file, it doesn't recognizes it as integer (it still treat the numbers from the text file as char).
here is the code that i've written
FILE *fp;
int a, b, c;
int i = 0;
int d;
int nums[100];
int num[100];
int temps[100];
if ((fp = fopen("new.txt", "r")) == NULL)
fprintf(stderr, "CANNOT OPEN FILE", "new.txt");
else
{
while ((fscanf(fp, "%d", nums)) != EOF);
{
fclose(fp);
fp = fopen("new.txt", "r");
while((nums[i] = fgetc(fp)) != EOF) {
if ((i %2) == 0)
{
temps[i] = nums[i];
}
i++;
}
}
}
what i want to know is how can i assign the value of temp[i] to an integer number. when i try to pass the value of temp[i] to an integer variable, the passed value from temp[i] to int is still read as character.
I did this program just to know how can i compute numbers that was read from a file.
any help is highly appreciated. |