NO

Author Topic: Console program unable to find file  (Read 2720 times)

QwertyTSecond

  • Guest
Console program unable to find file
« on: April 09, 2010, 11:28:26 AM »
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?

Code: [Select]
#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);

}

Offline TimoVJL

  • Global Moderator
  • Member
  • *****
  • Posts: 2115
Re: Console program unable to find file
« Reply #1 on: April 09, 2010, 02:38:46 PM »
remove that unnecessary line:
Code: [Select]
//fpin=open (infile, "r"); <--

if ((fpin=fopen (infile, "r"))==NULL)
{
printf ("Can't open file\n");
exit (0);
}
« Last Edit: April 09, 2010, 02:43:33 PM by timovjl »
May the source be with you

QwertyTSecond

  • Guest
Re: Console program unable to find file
« Reply #2 on: April 10, 2010, 12:04:20 PM »
Well, I have no idea how that snuck in. Removed it, but it still doesn't want to work.