0

I'm trying to run a simple program that reads a file and returns the context and the number of alphabetical characters. Im getting a wrong output.

#include <stdio.h>
#include <ctype.h>
int main()
{
    FILE *f;
    char path[100],c;
    int sum;

    printf("\nGive file's path: \n");
    scanf("%s",&path);

    f=fopen(path,"r");

    if(f==NULL){
        printf("\nFile not found.\n");
        return -1;}

    while(!feof(f)){
    c=fgetc(f);
    putchar(c);
    if (isalpha(c)!=0){
        sum++;}
    }
    printf("\n\n %d Alphabetical characters found.\n\n",sum);
    fclose(f);
    return 0;
}

File's context is: 12345 abz 12345 ABZ I should get the context and the number 6. instead i get this:

12345 abz 12345 ABZ
�

 4772675 Alphabetical characters found.
anastaciu
  • 23,467
  • 7
  • 28
  • 53
smksx
  • 47
  • 5
  • 6
    On an unrelated note, please read [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Some programmer dude Feb 26 '20 at 15:29
  • 3
    You might also be interested to learn about the [`isalpha`](https://en.cppreference.com/w/c/string/byte/isalpha) function. – Some programmer dude Feb 26 '20 at 15:31
  • How do you input the file path? What is the file path you input? – RobertS supports Monica Cellio Feb 26 '20 at 15:31
  • Lastly for your problem: How do you run your program? What is the programs current directory when you run the program? What is the input you give the program? – Some programmer dude Feb 26 '20 at 15:32
  • The wrong output is because you should change `while(!feof(f))` to `while((c=fgetc(f))!=EOF)` because `feof` tells you the _previous_ read failed, not that the _next_ read will succeed. – Paul Ogilvie Feb 26 '20 at 15:44
  • 1
    @dasblinkenlight That extra `&` is innocuous because a pointer to an array has the same value as a pointer to its first element. I.e. `path`, `&path`, `&path[0]` are all the same when passed into `scanf`/`...`. – Maxim Egorushkin Feb 26 '20 at 15:47
  • `sum` is never initialized. – 1201ProgramAlarm Feb 26 '20 at 15:50
  • i change int sum; to int sum=0; then run the program and got on a loop of ��� – smksx Feb 26 '20 at 15:53
  • 2
    Also, `fgetc()` returns an `int`, not a `char`. The value of `EOF` can not be properly represented as a `char` value, so if you assign the return value from `fgetc()` to a `char`, you can not reliably check for `EOF`. – Andrew Henle Feb 26 '20 at 16:09
  • As @Andrew says, use an `int`, also because truncating to (possibly signed) `char` will break `isalpha()` when you least want it to. – Toby Speight Feb 26 '20 at 16:54

1 Answers1

3

The variable sum is not initialized to zero. So it is initialized with a random value (which depends on whatever value happens to be in its location.

To fix, change the line to

int sum = 0;

The other errors pointed out in the comments should also be heeded, but until the initialization of the sum is fixed, you can expect to get crazy nonsense totals.

wallyk
  • 56,922
  • 16
  • 83
  • 148
  • 1
    An indeterminate value might be a fixed arbitrary valid value in practice, on some implementations, at the right phase of the moon. It might also result in more interesting times. Do you want to live in them? – Deduplicator Feb 26 '20 at 17:05