I'm working on a small program that reads text file off command line and counts number of characters in it. I've created the text file placed it in the same directory as the executable. For some reason it gives me strange number in output.
If I open the file directly through fopen (fopen("filename.txt", "r") program works corectly. But if I do something like(fopen(argv[0], "r") my program gives me strange output like 159806
Here is the code:
//Peter Yelagin
//carcount.c
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
long n=0;
FILE* ifp;
ifp=fopen(*argv, "r");
if(ifp==NULL) {perror("File not found"); exit(1);}
fseek(ifp, n, SEEK_END);
n=ftell(ifp);
printf("%ld\n", n);
fclose(ifp);
return 0;
}
If you guys know what's wrong with it, please let me know.
(I'm sure I have text file I'm trying to read in the same derectory, b/c I can open it on command prompt from that derectory).
Thanks for your time,
Peter