Like Tree1Likes
  • 1 Post By oogabooga

Characters, words and lines counting

This is a discussion on Characters, words and lines counting within the C Programming forums, part of the General Programming Boards category; Hi all, newbie here. I was trying to write a program that counts characters, words and lines from a text ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    21

    Characters, words and lines counting

    Hi all, newbie here. I was trying to write a program that counts characters, words and lines from a text file. I wrote a function for every task, and because i'm pretty sure the problem is there i will post only that... if you need the whole program please tell me.

    Characters:

    Code:
    int contaCaratteri(FILE* file){
    	char buffer;
    	int caratteri = 0;
    	
    	while (!feof(file)){
    		fscanf(file, "%c",&buffer);
    		if(buffer != ' ' || buffer != '\b' || buffer != '\n'){
    			caratteri++;
    		}
    	}
    	return caratteri;
    }
    I don't want to include spaces, newline and EOF in the count...

    lines:
    Code:
    int contaRighe(FILE* file){
    	int righe = 1;
    	char buffer;
    	
    	while (!feof(file)){
    		fscanf(file, "%c",&buffer);
    		if(buffer == '\b' || buffer == '\n'){
    			righe++;
    		}
    	}
    	return righe;
    }
    I thought that every newline means a line more, and the first is sure

    words:

    Code:
    int contaParole(FILE* file){
    	int parole = 1;
    	char buffer;
    	
    	while (!feof(file)){
    		fscanf(file, "%c",&buffer);
    		if(buffer == ' ' || buffer == '\b' || buffer == '\n'){
    			parole++;
    		}
    	}
    	return parole;
    }
    same here: every space or newline, means a word, and the first is sure...

    To speak the truth, i'm really not sure of the if conditions, i think the problem is there...

    now, that is the answer of the program.. eg.

    bla
    bla
    bla

    13 characters (instead of 9)
    1 line (instead of 3)
    1 word (instead of 3)

    thank you so much for your help!

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    63
    Checkout ctype.h. It might make a few things easier for you.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Generally, you wouldn't make separate functions for these since they can all be done at the same time with one pass through the file.

    You are not properly testing for end-of-file. You need to check the return value of fscanf.
    Code:
    while (fscanf(file, "%c", &buffer) > 0) {
        if (...)
            ...
    }
    But, now that I think about it, why are you using fscanf when fgetc would do?
    Code:
    int c;
    while ((c = fgetc(file)) != EOF) {
        if (...)
            ...
    }
    Try implementing these ideas and then post your entire program.
    Salem likes this.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    In reality you should not be reading one byte at a time from disk. Yes your computer is most likely configured to batch up multiple reads to the same disk sector into one buffered read (thank goodness, if it didn't do that then your code would be thousands of times slower). However that still occurs a lot further down into what the runtimes, OS, disk drivers or even the drive itself does, with regards to preventing multiple reads, and so it's still much slower.

    Just keep in mind that a real program would typically read in at least 4K, (4096 bytes) at once and then just iterate over all those bytes in memory, assuming the file size is more than a few Kb that is.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    21
    Quote Originally Posted by failure67 View Post
    Checkout ctype.h. It might make a few things easier for you.
    Yeah you are probably right... thing is, the excercise asked me to do that only stdio.h\stdlib.h\string.h (that function is not the whole excercise...)

    Quote Originally Posted by oogabooga View Post
    Generally, you wouldn't make separate functions for these since they can all be done at the same time with one pass through the file.

    You are not properly testing for end-of-file. You need to check the return value of fscanf.
    Code:
    while (fscanf(file, "%c", &buffer) > 0) {
        if (...)
            ...
    }
    But, now that I think about it, why are you using fscanf when fgetc would do?
    Code:
    int c;
    while ((c = fgetc(file)) != EOF) {
        if (...)
            ...
    }
    Try implementing these ideas and then post your entire program.
    My tutor wants me to read and write on file only with fscanf and fprintf... or fread and fwrite for binary files... for now.
    So I used the fscanf to test the end-of-file, but I don't understand what is wrong in the feof...

    Code:
    void counter(FILE* file, int* contatore){
    	char buffer;
    	int caratteri,righe,parole;
    	caratteri = righe = parole = 0;
    	
    	while(fscanf(file,"%c",&buffer)>0){
    		if(buffer != ' ' && buffer != '\b' && buffer != '\n'){
    			caratteri++;
    		}else if(buffer == '\b' || buffer == '\n'){
    			righe++;
    			parole++;
    		}else if(buffer == ' '){
    			parole++;
    		}
    	}
    	
    	*contatore = caratteri;
    	contatore++;
    	*contatore = righe;
    	contatore++;
    	*contatore = parole;
    }
    Additionally, there was an error in the first if condition: I used && instead of ||...

    Now the program works perfectly. Thank you so much.

    Quote Originally Posted by iMalc View Post
    In reality you should not be reading one byte at a time from disk. Yes your computer is most likely configured to batch up multiple reads to the same disk sector into one buffered read (thank goodness, if it didn't do that then your code would be thousands of times slower). However that still occurs a lot further down into what the runtimes, OS, disk drivers or even the drive itself does, with regards to preventing multiple reads, and so it's still much slower.

    Just keep in mind that a real program would typically read in at least 4K, (4096 bytes) at once and then just iterate over all those bytes in memory, assuming the file size is more than a few Kb that is.
    Thank you for the advice, it will be very useful in the future.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting rows, words and characters in C
    By Makara in forum C Programming
    Replies: 8
    Last Post: 01-06-2012, 12:42 PM
  2. Counting groups of lines
    By Ayreon in forum C Programming
    Replies: 26
    Last Post: 03-04-2009, 09:51 AM
  3. Counting Characters And Words
    By SNaRe in forum C Programming
    Replies: 6
    Last Post: 05-04-2005, 10:48 AM
  4. Counting Lines
    By darfader in forum C Programming
    Replies: 6
    Last Post: 09-12-2003, 06:19 AM
  5. Counting Lines
    By drdroid in forum C++ Programming
    Replies: 7
    Last Post: 11-18-2002, 04:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21