I apologise for posting so late after receiving many useful replies. Thank you for your help.
I am working on a remote server so was not able to copy and paste code when i previosly posted, hopefully this will give you a better idea of what i was trying to acomplish.
The code appears to work for what i can throw at it, however I'm not sure i fully understood the use of pointers in the function sscanf. I am also concerned about declaring an array with it's size as a variable without using alloc, even though this seems to work in this case.
The general idea was to get a matrix into memory from a file, without using any non standard libraries.
Feel free to rip this to shreds with constructive criticism:
Code:
#include<stdio.h>
#include<math.h>
int main(int argc, char *argv[])
{
int a=0, b=0, checkb1=0, checkb2=0, x=0, n=0, MatrixHeight=0,MatrixWidth=0 ;
char temp[2048] , word[100];
char *ptr;
FILE *FilePointer;
if (NULL==(FilePointer=fopen(argv[1],"r"))) //open and check file
{
printf("Inccorect Usage\n\n");
return -1;
}
while(fgets(temp,2047,FilePointer) != NULL) //while not the end of the file
{
if(sscanf(temp,"%s",word) < 1) //make sure there's something in the line, before adding a
goto end;
a++;
b=0; //reset b for a new line
ptr = temp;
while(sscanf(ptr,"%lf%n",word,&n) == 1)
{
b++;
ptr += n;
}
if((checkb1!=0)&&(checkb2!=b)) //make sure the width is always the same
{
printf("Matrix is missing elements!\n\n");
return -1;
}
checkb1++;
checkb2=b;
end:;
}
printf("Matrix is a %d by %d matrix\n", a, b );
double MatrixSize[a][b]; //get the matrix into memory
rewind(FilePointer);
MatrixHeight = a;
MatrixWidth = b;
a = 0;
b = 0;
for ( ; a < MatrixHeight ; a++)
{
b=0;
for ( ; b < MatrixWidth ; b++)
fscanf(FilePointer,"%lf",&MatrixSize[a][b]);
}
a=0;
for ( ; a < MatrixHeight ; a++)
{
b=0;
printf("\n");
for ( ; b < MatrixWidth ; b++)
printf("%3lf\t",MatrixSize[a][b]);
}
printf("\n");
return 0;
}