Hey guys,
just signed up as I'm pretty sure you can help me with a few things!
I'm pretty new to C, so my code will probably be ridden with noob errors... But... Here it goes!
I'm trying to write a program which reads a set of numbers from a txt file and puts them into an array; trouble is I keep having trouble with EOF errors, which loop type to use etc.
The numbers are stored in the txt file like this:
99
1890
777
6611
So, a new number each line, and only integer numbers.
My code so far is:
Code:
#include <stdio.h>
#include <stdlib.h>
#define TXTFILE "numbers.txt"
FILE *FPTR; //File Pointer
int numint; //Integer variable to store File contents
int numbarr[100]; //Array to store file contents
int i=0; //Iteration counter
int n; //Integer variable to contain the number of values in the file
main(void)
{
printf("Number Analysis Tool\n");
FPTR = fopen(TXTFILE,"r");
if(!FPTR)
{
printf("File is missing, empty or corrupted.\n");
}
else
{
printf("File opened successfully.\n");
}
while((numint = fgetc(FPTR)) != EOF)
{
i++;
}
n=i;
printf("Number of digits within the txt file: %d\n", n);
getchar();
int i=0;
while((numint = fgetc(FPTR)) != EOF && i>=0 && i<=n)
{
numbarr[i]=numint;
putchar(numbarr[i]);
i++;
}
fclose(FPTR);
getchar();
}
It seems to find the number of digits in the txt file ok, which is the variable n, but further than that it doesn't do much. It also compiles completely fine.
Any help is much appreciated!
Cheers,
Will