Hey guys, quick question from a programming noob.
I need to read in the contents of a .txt file into an array. The .txt file looks something like:
0 2 8 4
1 0 4 5
1 2 0 6
9 6 5 0
All i need to do is read these contents into a single 1D integer array, but i cant do it!At the moment i'm using the code:
void ReadFile() {
FILE *file;
int input;
int i=0;
file = fopen("InputMatrix.txt", "r" );
input = (int)fgetc(file);
while (input!=EOF){
if (isspace(input)){
input = (int)fgetc(file);
}
else {
edges = input;
i++;
input = (int)fgetc(file);
}
}
fclose(file);
However this just stores the ascii value of the input item in the array. I'm trying to use fscanf now but still having no success. Any advice? (it has to be in C i'm afraid). Ta
I need to read in the contents of a .txt file into an array. The .txt file looks something like:
0 2 8 4
1 0 4 5
1 2 0 6
9 6 5 0
All i need to do is read these contents into a single 1D integer array, but i cant do it!At the moment i'm using the code:
void ReadFile() {
FILE *file;
int input;
int i=0;
file = fopen("InputMatrix.txt", "r" );
input = (int)fgetc(file);
while (input!=EOF){
if (isspace(input)){
input = (int)fgetc(file);
}
else {
edges = input;
i++;
input = (int)fgetc(file);
}
}
fclose(file);
However this just stores the ascii value of the input item in the array. I'm trying to use fscanf now but still having no success. Any advice? (it has to be in C i'm afraid). Ta