Click here to Skip to main content
15,903,744 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralBurning CDs programatically Pin
mabotta9-Apr-03 6:30
mabotta9-Apr-03 6:30 
GeneralRe: Burning CDs programatically Pin
Daniel Turini9-Apr-03 6:39
Daniel Turini9-Apr-03 6:39 
Generalhiding and displaying radio buttons in a group Pin
si_699-Apr-03 5:39
si_699-Apr-03 5:39 
GeneralRe: hiding and displaying radio buttons in a group Pin
Maximilien9-Apr-03 6:34
Maximilien9-Apr-03 6:34 
GeneralRe: hiding and displaying radio buttons in a group Pin
adamUK9-Apr-03 9:20
adamUK9-Apr-03 9:20 
GeneralRasCustomDial Pin
Mark Otway9-Apr-03 5:30
Mark Otway9-Apr-03 5:30 
GeneralSAPI question: Pin
Joan M9-Apr-03 5:23
professionalJoan M9-Apr-03 5:23 
GeneralCounting characters from a text file Pin
Overfiend9-Apr-03 4:42
Overfiend9-Apr-03 4:42 
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*************/
/**********************************************/
void countchars(char *buffer, int *count)
/*
*Counts the number of characters in the text file */
{
int num;
int arrletters['z'-'a'];
num = 0;
for(int i =0; i<('z'-'a');i++)
{
arrletters[num]=0;
}
if(buffer[num] >='A' && buffer[num]<='Z')
{

arrletters[buffer[num]-'A']++;
}
if(buffer[num] >= 'a' && buffer[num] <='z')
{
arrletters[buffer[num]-'a']++;
}
count++;
}
/*****************************************************/
/*****************************************************/
int Nchars(char *buffer)
{
int i,count; /*initiate variables */
do /*begin do while loop*/
{
i++; /*start incrementation*/
if(buffer[i]) /*access buffer array at position i*/
{
count++; /*Increment count*/
}
}while(buffer[i]!=EOF); /*check to make sure not at end of file*/
return count; /*return count value to main programme*/
}



int Nwords(char *buffer)
{
int i, NWCounter; /*Initiate variables*/
NWCounter=1; /*Set counter*/
do /* begin do while loop*/
{
i++; /*Begin increment*/
if((buffer[i]== ' ')||(buffer[i]=='\n')) /*Check to see if the*/
{ /*character in the buffer is a space or*/
/*the end of a line*/
NWCounter++; /*increment counter if it is*/
}
}while(buffer[i]!=EOF); /*check for end of file*/
return NWCounter; /*return results*/
}
int Nlines(char *buffer)
{
int i, NLCounter; /*set variables*/
NLCounter=1; /*set counter*/
do
{
i++; /*increment variable
if(buffer[i]=='\n') /*check buffer position for end of line*/
{
NLCounter++; /*increment if end of line found*/
}
}while(buffer[i]!=EOF); /*check for end of line*/
return NLCounter; /*return value*/
}
Any suggestions would be great thanks.
GeneralRe: Counting characters from a text file Pin
Big Art9-Apr-03 6:36
Big Art9-Apr-03 6:36 
GeneralRemoving directories Pin
Brian Delahunty9-Apr-03 4:07
Brian Delahunty9-Apr-03 4:07 
GeneralRe: Removing directories Pin
Rickard Andersson209-Apr-03 5:26
Rickard Andersson209-Apr-03 5:26 
GeneralRe: Removing directories Pin
Brian Delahunty9-Apr-03 5:31
Brian Delahunty9-Apr-03 5:31 
GeneralHtml Edit Control Notification Pin
Atlence9-Apr-03 3:50
Atlence9-Apr-03 3:50 
GeneralRe: Html Edit Control Notification Pin
Atlence19-Apr-03 8:25
Atlence19-Apr-03 8:25 
GeneralMFC Pin
frackasse9-Apr-03 2:30
frackasse9-Apr-03 2:30 
GeneralRe: MFC Pin
Cedric Moonen9-Apr-03 2:41
Cedric Moonen9-Apr-03 2:41 
Generalwinpcap handle problem Pin
summo9-Apr-03 2:15
summo9-Apr-03 2:15 
GeneralCreateThread / ExitThread Pin
Paul Farry9-Apr-03 2:03
professionalPaul Farry9-Apr-03 2:03 
GeneralRe: CreateThread / ExitThread Pin
Zdeslav Vojkovic9-Apr-03 2:35
Zdeslav Vojkovic9-Apr-03 2:35 
GeneralTransfer of a SDI application to MDI Pin
Claude Gagnon9-Apr-03 1:38
Claude Gagnon9-Apr-03 1:38 
GeneralDao problem Pin
naradaji9-Apr-03 1:27
naradaji9-Apr-03 1:27 
GeneralRe: Dao problem Pin
G. Steudtel9-Apr-03 23:43
G. Steudtel9-Apr-03 23:43 
GeneralRe: Dao problem Pin
naradaji9-Apr-03 23:58
naradaji9-Apr-03 23:58 
Generaltype cast problem when going from VS6->VS.NET Pin
Mathias S.9-Apr-03 0:43
Mathias S.9-Apr-03 0:43 
GeneralRe: type cast problem when going from VS6->VS.NET Pin
jhwurmbach9-Apr-03 1:13
jhwurmbach9-Apr-03 1:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.