Sunday 15 January 2012

c - Const number of words in a text -


I tried to write a program to count the number of words in a text taken from a file I had a problem The compiler can not find my file, but I put this file in the project folder. What can i do

  #include & lt; Stdio.h & gt; # Include & lt; Conio.h & gt; #include & lt; String.h & gt; Int word (Const four sentences []); Int main (zero) {letter sentence [100]; FILE * cfPtr; If ((cfPtr = fopen ("C programming.dat", "r")) == NULL {printf ("The file could not be opened \ n"); } Other {fscanf (cfPtr, "% s", sentence); } Word (sentence); Printf ("% d", word (sentence)); Getch (); Return 0; } Int word (const char sentence []) {int i, length = 0, count = 0, last = 0; Length = Stellen (sentence); For (i = 0; i & lt; length; i ++) if (sentence [i] == '' || sentence [i] == '\ t' || sentence [i] == '\ n ') Count ++; Calculation of return; }    

I will try to improve the usefulness of your program, filename is optional Accepting the parameter as

  int main (ant argc, char * argv []) {character sentence [100]; Const char * filename = "programming dat"; FILE * cfPtr; If (argc == 2) filename = argv [1]; If ((cfPtr = fopen (filename, "r")) == faucet {printf ("The file '% s' could not be opened \ n", file name); } Else {int total = 0; While (fgets (sentence, sentence sentence, cfPtr)) Total + = words (sentences); Printf ("% d", total); Fclose (cfPtr); } Getch (); Return 0; } ...   

Note: untested

No comments:

Post a Comment