Console program unable to find file

Started by QwertyTSecond, April 09, 2010, 11:28:26 AM

Previous topic - Next topic

QwertyTSecond

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);

}

TimoVJL

#1
remove that unnecessary line:
//fpin=open (infile, "r"); <--

if ((fpin=fopen (infile, "r"))==NULL)
{
printf ("Can't open file\n");
exit (0);
}
May the source be with you

QwertyTSecond

Well, I have no idea how that snuck in. Removed it, but it still doesn't want to work.