Question:
prompt user for file and then count words, lines and character.?
2012-06-16 09:12:08 UTC
I can get the prompt to ask for a file, but thats it. Can anyone please help?


#include
#include

int main()
{
FILE *inFile;
char filename[64];

printf("\nEnter a file name: ");
gets(filename);
fflush(stdin); /* clear input area so you can pause */
getchar(); /* force the computer to pause until you press a key on the keyboard */

inFile = fopen(filename, "r");
if(inFile == NULL)
{
printf("\nThe File %s was not successfully opened.", filename);
printf("\nPlease check that the file currently exists. \n");
return(1);
fflush(stdin); /* clear input area so you can pause */
getchar(); /* force the computer to pause until you press a key on the keyboard */
}

int countword = 0;
int countchar = 0;
int linecount = 0;
int count = 0;
char c;

c = getc(inFile);
while (c != EOF );
{
countchar++;
if (c == '\n') linecount++;
if (c == ' ') countword++;
c = getc(inFile);
}
fclose (inFile);
if (countchar != 0);
{
printf("Number of characters = %d, number of lines = %d, Number of words%d\n", countchar, linecount, countword);
}

return 0;
Three answers:
??????
2012-06-16 09:20:32 UTC
You have to keep a variable, named "whitespace" that is 1 if you are in white space, cause like your program is written now, it counts the number of spaces instead of the number of words. This is because you can have several spaces one after the other. Work like this :



if (c==' ') if (!whitespace) { whitespace=1; countword++; } else whitespace=0;



I also have doubts about c=='\n', because \n normally consists of 2 characters : carriage return (13) and line feed (character number 10). Anyway, this is the case when you read a file in a binary way, but you use text file (opening with "r" and "w" instead of "rb" or "wb").

I prefer working with binary files ("rb" and/or "wb") instead of working with text files.
2012-06-16 09:39:11 UTC
> gets(filename);

Unsafe. See: http://c-faq.com/stdio/getsvsfgets.html

Also note that it was deprecated in the latest revision of the C standard. It's no longer part of the standard C libary. Good riddance.



> fflush(stdin); /* clear input area so you can pause */

It doesn't do that at all. fflush works with *output* streams, not input streams. Passing in an input stream has undefined behaviour.



Reading in a path name then opening it can be fairly difficult to do correctly without knowing how input/output works, it's also unnecessary for an example program since you can just redirect the contents of a file into stdin. Anyway, the problem with your program is that it counts spaces instead of words. Here's how you can count words:



#include

#include



enum { CHARS, WORDS, NEWLINES, NTYPES };



int main(void)

{

        int count[NTYPES] = { 0 };

        int inspace = 1;

        int c;



        while ((c = getchar()) != EOF) {

                if (c == '\n')

                        count[NEWLINES]++;

                if (isspace(c)) {

                        inspace = 1;

                } else if (inspace) {

                        inspace = 0;

                        count[WORDS]++;

                }

                count[CHARS]++;

        }

        printf("%d %d %d\n", count[CHARS], count[WORDS], count[NEWLINES]);

        return 0;

}



edit: http://c-faq.com/stdio/stdinflush.html
?
2016-11-29 00:12:29 UTC
Awesooooome!!! it is the main inefficient implementation ever! Given your implementation, you do no longer might desire to study something to count selection the characters, only use fseek() to bypass to the tip of the record, and then get the area using ftell(). BTW, conio.h is a non-everyday header, you may desire to circumvent it, as that is in basic terms obtainable on some DOS and abode windows SDKs, use getchar() somewhat of getch(). the different difficulty on your application is which you at the instant are not examining something, you're passing the pointer infile to countchar, so that is examining from a random memory section... try another compiler+IDE, that wisely comments the blunders on your code, i % to propose Code::Blocks + MinGW, or bypass at present to GNU/Linux the place are the terrific progression equipment. For counting the words/lines, you may desire to optimize it with the aid of examining with the aid of 4/8 bytes (relying on in case you're on x86 or x86_64) until the tip of the string, the place you may desire to earlier write zeros until it gets aligned (after the study). on account that x86 and x86_64 are little-endian architectures, you in basic terms evaluate the low 8bits, shift with the aid of 8bits, evaluate lower back... that's all, that is lots swifter :). additionally you need to use XOR to discover the areas on the entire sign in, that would improve the overall performance slightly extra, or maybe use truly professional SIMD training (yet that's going too some distance XD). i can't comprehend why truthfully everybody talks approximately "a million byte = a million char", especially whilst no person makes use of 7bit ASCII or any 8bit encodings anymore. This implementation isn't properly suited with UTF-8, to make it properly suited you may desire to verify the lenght of each and every char.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...