Thread: Hit a brick wall here...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    21

    Hit a brick wall here...

    I think i have some mixed up stuff in my program, maybe some c++ in there as well. Used alot of internet sites and maybe misunderstood something. Im trying to get this thing to count the number of lines words and characters in a file name. any help would be appreciated. Here is what i have
    Code:
    #include<stdio.h>
    void chars(struct file );     /* function prototype*/
    void words(struct file );     /*function prototype*/
    void lines(struct file );     /*function prototype*/
    struct file; 
    struct in;
    int main ()
    {
        file *in;
        char fNAME[15];                 /*limits chars to 15 for input*/
        printf("\nEnter the file name: ");
        scanf ("%s",fNAME);
        in=fopen(fNAME, "r");       /*opens file*/
        if(in == NULL)                  /*checks for file opening*/
        {
        printf("\n Cannot find file %s.\n",fNAME);
        exit(1)
        }
        chars(in);
        words(in);
        lines(in);
        fclose(in);                    /*fclose closes a file*/
        system("pause");
        return 0;
    }
    void chars(file * fNAME)       /*a pointer to a file*/
         {
         long ftell();
         fseek(fNAME, OL, SEEK_END);               /*for file positioning to the end of the file*/
         printf("The number of characters in the file is: %d\n",ftell(fNAME));
         return;
         }
    
    void words(filw * fNAME)                /*a pointer to a file*/
    
         {
         long ftell();
         fseek(fNAME, OL, SEEK_END);/*for file positioning to the end of the file*/
         printf("The number of words in the file is: %d\n",ftell(fNAME));
         return;
         }
    
    void lines(file * fNAME)                    /*a pointer to a file*/
         {
         long ftell();
         fseek(fNAME, OL, SEEK_END);                            /*for file positioning to the end of the file*/
         printf("The number of lines in the file is: %c\n",fNAME);
         return;
         }

  2. #2
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    There's somehow a good amout of syntax/semantic error in your code. It would take some time to explain them all, especially since english isn't my first language hah. But i'm sure people will help you (or, at least, they'll redirect you to good source of information).

    I'll just talk to you about your 3 functions, chars(..), words(..) and lines(..).

    I don't know if you realize, but they are all the same (except the last one who has a bigger number of errors heh..). And, well, you should understand that counting the number of caracter isn't the same thing as counting the number of words nor the number of lines.

    Your function chars(FILE *file) is the closer one to be correct. Without the errors, it should looks like this :
    Code:
    void chars(FILE *file)       /*a pointer to a file*/
    {
    	fseek(file, 0, SEEK_END);               /*for file positioning to the end of the file*/
    	printf("The number of characters in the file is: %dl\n", ftell(file));
    
    	return;
    }
    It would give the correct answer depending on what is your definition of a character. (are non-printable characters are characters for you ?)


    Next, second easiest function, lines(FILE *file). Well, for this, you only need to count the number of newline character ('\n') from the beginning of the file. Should looks like this:
    Code:
    void lines(FILE *file)
    {
    	int linesNb = 1;
    
    	if (file != NULL)
    	{
    		rewind(file);		// making sure we are at the beginning of the file
    
    		while (!feof(file))
    		{
    			if (fgetc(file) == '\n')
    			{
    				// Here's a new line character
    				linesNb = linesNb + 1;
    			}
    		}
    
    		printf("The number of lines in the file is: %d\n", linesNb);
    	}	
    
    	return;
    }

    And for the words counting function... well... it should look for an alphabetic character, then increment the number of words, and then moving to the next whitespace and doing this for all the file. It's going to be appropriate in most of the case. Here's an example

    Code:
    void words(FILE *file)                   
    {
    	int wordsNb = 0;
    	char car;
    
    	if (file != NULL)
    	{
    		rewind(file);
    
    		while (!feof(file))
    		{
    			do
    			{
    				car = fgetc(file);
    			} while ((!feof(file)) && ((car < 'a') || (car > 'z')) && ((car < 'A') || (car > 'Z')));
    
    			if (!feof(file))
    			{
    				/* A: car is an alphabetic caracter */
    
    				wordsNb = wordsNb + 1;
    
    				do
    				{
    					car = fgetc(file);
    				}
    				while ((!feof(file)) && (car != ' ') && (car != '\n') && (car != '.'));
    			}
    		}
    
    		printf("The number of words in the file is: %d\n", wordsNb);
    	}
    
    	return;
    }
    That's it for today.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    fgetc returns int
    so generaly using of fgetc is like
    Code:
    int c;
    while((c = fgetc(file)) != EOF)
    {
       /* do something with c */
    }
    instead of manual checking of the diapasons like 'a':'z' better to use function isupper and islower - they are portable.

    for the space checking isspace can help
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Stack problem - I've hit a wall!
    By miniwhip in forum C Programming
    Replies: 7
    Last Post: 11-14-2007, 03:05 AM
  2. The wall behind is showing over the wall in front. (Glut)
    By Queatrix in forum C++ Programming
    Replies: 2
    Last Post: 10-22-2005, 04:50 PM
  3. Help with Printing BMPs
    By simtron in forum Windows Programming
    Replies: 5
    Last Post: 09-22-2005, 09:21 AM
  4. My log file player; Hit the brick wall
    By Twig in forum C Programming
    Replies: 6
    Last Post: 07-27-2002, 05:35 PM
  5. Snake or nib or tron
    By Faceoff49 in forum Game Programming
    Replies: 10
    Last Post: 05-31-2002, 02:55 PM