Hi all, I've recently started programming at university and am having a bit of trouble with it. I've begun a piece of work that requires the user to enter a filename, read it into an array, do some maths/statistics and display a variety of results in tables. Annoyingly, I can't get the first part working - my attempt thus far is as follows;
Code:
#include<stdio.h>
#include<math.h>
#include<string.h>
int main(void)
{
char filename[25];
int numbers;
FILE * fptr;
printf("Please enter a filename: ");
scanf(" %24s",filename[25]);
fptr=fopen(filename[25],"r");
if (fptr==NULL) printf("Open failed.");
while(fscanf(fptr," %d",&numbers)!=EOF)
printf(" %3d",numbers);
fclose(fptr);
getchar();
getchar();
return 0;
}
The program seems to freeze as soon as I type in the filename and hit enter; I was under the impression that the scanf would read in whatever I typed to filename[25], so if I typed for example "statsdata.txt", this would then open the file as if I'd simply typed fptr=fopen("statsdata.txt","r"); normally, so I'm not sure what to do!
Also, in my file I have 8 numbers seperated by whitespace; if I type out the fptr=fopen("statsdata.txt","r"), these numbers are displayed onscreen. I'm unsure though if these are now in an array or not - is filename[25] here considered to be a string and unrelated to the numbers within?
The program required is pretty complex for my current knowledge, kind of stressing about it which doesn't help either. Thanks a lot for any help you can give.