It works for one or the other, but not both. If I put in both sections of code (character count and word count) it will have the word count return 0 to the counter. If I take one away, they work fine! I will put it in separate functions when im done..any ideas of what is wrong??

here is the code

Code:

FILE *fp;
while (1){ char filename[100];

puts("Enter the name of the file to open");

scanf("%s", &filename);

fp=fopen(filename, "r");

if (fp == 0)
{
printf("\n\n%s file name not found!\n\n", filename);
}

else
{
puts("File successfully opened");
break;
}

}//End while loop
//////////////////////////////////////

//charcount function

while (1) {
ch = fgetc(fp);
++charCount;

if (ch == -1)
break;
}
ch = 0;

printf("\nNumber of characters: %i\n", charCount);

//////////////////////////////////////////////////////

//WORDS COUNTER FUNCTION

while((i = getc(fp)) != EOF)
{

if ((i = getc(fp)) == ' ')
++wCount;

}
printf("Number of words: %i\n", wCount);

04-08-2012

memcpy

When you run both functions, the second one is starting at the end of your file. Rewind the file pointer between functions.

Also, your indentation is ........-poor.

04-08-2012

jthunder

Quote:

Originally Posted by memcpy

When you run both functions, the second one is starting at the end of your file. Rewind the file pointer between functions.

Also, your indentation is ........-poor.

I had it really neat before but I was trouble shooting and moving stuff around. Anyway its working now, I appreciate the tip. Thanks.