Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Counting characters from a text file

Counting characters from a text file

Scheduled Pinned Locked Moved C / C++ / MFC
c++help
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    Overfiend
    wrote on last edited by
    #1

    Hello everyone, Ok im having alot of trouble with pointers here im trying to get my head around them but it just aint working! Im trying to create a program that will load a txt file into a buffer and then count how many characters are in the text file as well as lines and words the problem is it doesnt count the characters i can get it to count the lines and words but not individual characters. The code looks like this: #include #include #define BUFSIZE 1024 #define NCHARS 256 int getfile(char *fname, char *buffer, int buflen); void countchars(char *buffer, int *count); int Nlines(char *buffer); int Nchars(char *buffer); int Nwords(char *buffer); void main(void) { char fname[100]; char buffer[BUFSIZE]; int cdata[NCHARS]; int i,N; char c; printf("EL1113 assignment 2, 2002/3."); printf("\nText file analysis program."); printf("\n\nEnter the file name: "); scanf("%s",fname); if (getfile(fname,buffer,BUFSIZE)) { printf("\n%s contains ",fname); printf("%d characters and ",Nchars(buffer)); printf("%d words in ",Nwords(buffer)); printf("%d lines.\n",Nlines(buffer)); countchars(buffer,cdata); printf("\nDistribution of letters in %s is:",fname); for (c='A';c<='Z';c++) { N=cdata[c]+cdata[tolower(c)]; printf("\n'%c' %4d: ",c,N); for (i=0;i { putchar(c); } } printf("\n"); } } int getfile(char *fname, char *buffer, int buflen) { int i; int ch; int nchars = 0; FILE *ipfile; /*clear buffer*/ for (i=0;i { buffer[i]='\0'; } /*read from file into buffer*/ ipfile=fopen(fname,"r"); /*This code basically opens a*/ /*specified text file in read mode and*/ /*assigns it to the pointer ipfile*/ /*giving it the variable name fname*/ if (ipfile==NULL) { fprintf(stderr,"\nFile %s not found.",fname); return 0; /*The fprintf statement acts just like*/ /*the printf statement*/ } else { do { ch=fgetc(ipfile); /*collecting a character from the*/ /*ipfile pointer that holds the*/ /*destination for the imported file*/ /*EOF=End Of File*/ if (ch!=EOF) { buffer[nchars]=ch; nchars++; if (nchars==buflen) { fprintf(stderr,"\nFile %s ",fname); fprintf(stderr,"too large for buffer."); fprintf(stderr,"\nRead first %d ",buflen); fprintf(stderr,"characters from %s.",fname); } } } while ((ch!=EOF)&&(nchars fclose(ipfile); /*The fclose statement simply closes the file*/ /*that you are working on when it is no longer needed*/ return 1; } } /**********************************************/ /*THIS IS TH PART THAT DOESNT WORK************

    B 1 Reply Last reply
    0
    • O Overfiend

      Hello everyone, Ok im having alot of trouble with pointers here im trying to get my head around them but it just aint working! Im trying to create a program that will load a txt file into a buffer and then count how many characters are in the text file as well as lines and words the problem is it doesnt count the characters i can get it to count the lines and words but not individual characters. The code looks like this: #include #include #define BUFSIZE 1024 #define NCHARS 256 int getfile(char *fname, char *buffer, int buflen); void countchars(char *buffer, int *count); int Nlines(char *buffer); int Nchars(char *buffer); int Nwords(char *buffer); void main(void) { char fname[100]; char buffer[BUFSIZE]; int cdata[NCHARS]; int i,N; char c; printf("EL1113 assignment 2, 2002/3."); printf("\nText file analysis program."); printf("\n\nEnter the file name: "); scanf("%s",fname); if (getfile(fname,buffer,BUFSIZE)) { printf("\n%s contains ",fname); printf("%d characters and ",Nchars(buffer)); printf("%d words in ",Nwords(buffer)); printf("%d lines.\n",Nlines(buffer)); countchars(buffer,cdata); printf("\nDistribution of letters in %s is:",fname); for (c='A';c<='Z';c++) { N=cdata[c]+cdata[tolower(c)]; printf("\n'%c' %4d: ",c,N); for (i=0;i { putchar(c); } } printf("\n"); } } int getfile(char *fname, char *buffer, int buflen) { int i; int ch; int nchars = 0; FILE *ipfile; /*clear buffer*/ for (i=0;i { buffer[i]='\0'; } /*read from file into buffer*/ ipfile=fopen(fname,"r"); /*This code basically opens a*/ /*specified text file in read mode and*/ /*assigns it to the pointer ipfile*/ /*giving it the variable name fname*/ if (ipfile==NULL) { fprintf(stderr,"\nFile %s not found.",fname); return 0; /*The fprintf statement acts just like*/ /*the printf statement*/ } else { do { ch=fgetc(ipfile); /*collecting a character from the*/ /*ipfile pointer that holds the*/ /*destination for the imported file*/ /*EOF=End Of File*/ if (ch!=EOF) { buffer[nchars]=ch; nchars++; if (nchars==buflen) { fprintf(stderr,"\nFile %s ",fname); fprintf(stderr,"too large for buffer."); fprintf(stderr,"\nRead first %d ",buflen); fprintf(stderr,"characters from %s.",fname); } } } while ((ch!=EOF)&&(nchars fclose(ipfile); /*The fclose statement simply closes the file*/ /*that you are working on when it is no longer needed*/ return 1; } } /**********************************************/ /*THIS IS TH PART THAT DOESNT WORK************

      B Offline
      B Offline
      Big Art
      wrote on last edited by
      #2

      I really don't have the time to go through your code right now. But if it were my project I would simply put the contents of your text file in a CString variable and use the CString::GetLength() fn to get the character count and use the CString::Find(..) fn in a loop counting the spaces to get the word count. Art

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups