Thread: Counting characters

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    8

    Counting characters

    Hi all,

    Am trying to write some code that will read a simple text file and return what is placed in the text file along with how many characters there are. Reading the file works but am having a problem with the correct number of characters, keeps on saying minus 1. If anyone could help i would be very grateful. Thanks in advance.


    #include<stdio.h>

    int main(void)

    {
    FILE * input_file;
    char input_filename[20];
    char file[20];
    int rval, ch;

    printf("Please input the file name you wish to open: ");
    scanf("%s", input_filename);

    input_file=fopen(input_filename,"r");

    rval=fscanf(input_file,"%s",file);
    printf("%s\n",file);

    while((fgetc(input_file))!=EOF);
    {
    ch=fgetc(input_file);
    printf("%d characters in file.\n", EOF);
    }

    fclose(input_file);

    return 0;
    }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>printf("%d characters in file.\n", EOF);
    One rather obvious problem..... what variable are using to count the number of characters..?! (hint: you aren't actually counting ANY!)

    Open the file.
    Read one character at a time, incrementing a counter as you go
    When you reach EOF, close the file, and print the value of the counter.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Aug 2002
    Posts
    55
    I read something a while back about getting the size of a file. Being as 1 character = 1 byte, I'm *assuming* you can use the same method to count the characters. The method was basically to use fseek(fp, 0L, SEEK_END) and ftell(fp).

    Would that work here, or am I getting muddled with something else?

    John.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Originally posted by John.H
    I read something a while back about getting the size of a file. Being as 1 character = 1 byte, I'm *assuming* you can use the same method to count the characters. The method was basically to use fseek(fp, 0L, SEEK_END) and ftell(fp).

    Would that work here, or am I getting muddled with something else?
    Maybe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Counting the characters from each word from a text file
    By flipguy_ph in forum C Programming
    Replies: 6
    Last Post: 04-27-2009, 05:56 PM
  3. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  4. Help with hw problem: counting characters in a string
    By pinkfloyd4ever in forum C++ Programming
    Replies: 11
    Last Post: 11-04-2007, 11:18 PM
  5. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM