Thread: Characters in a txt file.

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

    Characters in a txt file.

    Sorry for posting again.

    I have updated the previous code but still no luck. I am trying to count the number of characters in the code, the number printed is always the number to which i initialised "ch" to, so am guessing the counter is not working. If anyone has any ideas it would be greatly appreciated. Thanks in advance.

    #include <stdio.h>
    #include <stdlib.h>

    void content(void);
    void characters(void);

    FILE * input_file;
    char input_filename[50];
    char file[50];
    int rval;
    char temp;
    int ch=0;

    main(void)

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

    content();
    characters();

    fclose(input_file);
    }

    void content( )

    {
    input_file=fopen(input_filename, "r");
    rval=fscanf(input_file, "%s", file);
    printf("The file contains the following text:- %s", file);

    if(input_file==NULL)
    {
    printf("Error opening file");
    }


    }

    void characters( )

    {
    while(!feof(input_file))
    {
    temp=fgetc(input_file);
    ch++;

    if(ferror(input_file))
    {
    printf("Error counting characters");
    }

    }
    printf("\nThe file is %d characters long.\n", ch);

    }

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    in content():
    Code:
    input_file=fopen(input_filename, "r");
    rval=fscanf(input_file, "%s", file);
    First check if fopen failed, then read from file.

    After you have called this function (content()) the file pointer (input_file) is pointing to the second string (and not the beginning of the file). You need to reposition the filepointer before calling characters().
    Code:
    content();
    fseek(input_file, 0, SEEK_SET); /* reposition filepointer to beginning of file */
    characters();
    In characters():
    Code:
    while((temp=fgetc(input_file)) != EOF)
    {
       ch++;
    }
    Another option is to combine the 2 functions into 1:
    Code:
    void characters()
    {
       char ch;
       int count = 0;
    
       printf("The file contains the following text:\n");
    
       while((ch = fgetc(fp)) != EOF)
       {
          printf("%c", ch);
          count++;
       }
    
       printf("Number of characters = %d\n", count);
    }
    Last edited by Monster; 01-31-2003 at 08:03 AM.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    8
    Thanks Monster that was great help. Have changed my code as you suggested.

    Only one more little problem. I am reading the text file as a string, is there anyway i can stop the program from thinking a "space" in the text file is the End Of File. Sorry to be a pain again. Thanks for your help once more.

    #include <stdio.h>
    #include <stdlib.h>

    void content(void);
    void characters(void);

    FILE * input_file;
    char input_filename[50];
    char file[50];
    int rval;
    char temp;
    int ch=0;

    main(void)

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

    content();
    fseek(input_file, 0, SEEK_SET);
    characters();

    fclose(input_file);
    }

    void content( )

    {
    input_file=fopen(input_filename, "r");
    rval=fscanf(input_file, "%s", file);
    printf("The file contains the following text:- %s", file);

    if(input_file==NULL)
    {
    printf("Error opening file");
    }


    }

    void characters( )

    {
    while((temp=fgetc(input_file)) != EOF)

    {
    ch++;

    if(ferror(input_file))
    {
    printf("Error counting characters");
    }

    }
    printf("\nThe file is %d characters long.\n", ch);

    }

  4. #4
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>is there anyway i can stop the program from thinking a "space" in the text file is the End Of File.
    scanf() is space delimited by default, either use fgets() with the file size, or save the characters as you read them, or print them as you read them :-)
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      int ch, count = 0;
      FILE *in = fopen("input.txt", "r");
    
      if (in == 0)
      {
        return 1;
      }
    
      printf("The file contains:\n");
      while ((ch = getc(in)) != EOF)
      {
        putchar(ch);
        count++;
      }
    
      printf("\nThe count is -- %d\n", count);
    
      return 0;
    }
    Or if an exact count isn't as important as 'close enough' then you can ease your pain by just doing this :-)
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      long int size;
      FILE *in = fopen("input.txt", "rb");
    
      if (in == 0)
      {
        return 1;
      }
    
      fseek(in, 0, SEEK_END);
      if ((size = ftell(in)) != -1L)
      {
        printf("Character count -- %ld\n", size);
      }
    
      return 0;
    }
    *Cela*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM