I'm writing a program to analyse a text file ie. how many characters it has, character occurrence frequency etc.
The user defines where the file is via gets.
However, no matter where I place the .txt or what path I type, the program cannot find the file.
For example, placing it in documents and entering the string: d:\documents\test.txt produces "Can't open file"
Anyone have any idea what I need to change?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
FILE *fpin;
int ch;
int count;
char infile [256];
printf ("\nInput file name:\n");
gets (infile);
fpin=open (infile, "r");
if ((fpin=fopen (infile, "r"))==NULL)
{
printf ("Can't open file\n");
exit (0);
}
count=0;
while ((ch=getc (fpin)) !=EOF)
{
count=count+1;
}
fclose(fpin);
printf ("\nFile %s has %d characters\n", infile, count);
}