Thread: File output is not displaying an acceptable answer

  1. #1
    Registered User
    Join Date
    Feb 2017
    Posts
    4

    File output is not displaying an acceptable answer

    So I have a text file which reads: This line has numbers: 1, 2,3, 4, 55 Some lines include punctuations such as , ; . / & ^

    My output is displaying drastic results:

    Number of vowels: 0
    Number of consonents: 242
    Number of digits: 66
    Number of white spaces: 32
    Number of other characters: 55

    Why is it doing this if the code is just reading til the end of the file?

    Here's my code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    
    void output_summary (int space_count, int ch_count, int digit_count, int vowel_count, int sym_count);
    int main (void)
    {
        char ch;
        int status;
        int space_count = 0, ch_count = 0, digit_count = 0, vowel_count = 0, sym_count = 0;
        
        FILE *infile;
        
        infile=fopen("input.txt", "r");
        if (infile == NULL)
        {
            printf("ERROR COMPUTING FILE\n");
            exit(EXIT_FAILURE);
        }
        
        
        status = fscanf(infile, "%c", &ch);
        while (status != EOF)
            {
                if (isspace(ch))
                {
                    space_count++;
                }
            
                else if (isupper(ch))
                {
                    ch = tolower(ch);
                    ch_count++;
                }
            
                else if (islower(ch))
                {
                    ch_count++;
                }
            
                else if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
                {
                    vowel_count++;
                }
            
                else if (isdigit(ch))
                {
                    digit_count++;
                }
            
                else
                {
                    sym_count++;
                }
            
                status = fscanf(infile, "%c", &ch);
            }
            
        fclose(infile);
        
        output_summary (space_count, ch_count, digit_count, vowel_count, sym_count);
        
        return (0);
    
    
    }
        
    void output_summary (int space_count, int ch_count, int digit_count, int vowel_count, int sym_count)
    {
        FILE *outfile;
        
        outfile = fopen("output.txt", "w");
        fprintf(outfile, "Number of vowels: %d\n", vowel_count);
        fprintf(outfile, "Number of consonents: %d\n", ch_count);
        fprintf(outfile, "Number of digits: %d\n", digit_count);
        fprintf(outfile, "Number of white spaces: %d\n", space_count);
        fprintf(outfile, "Number of other characters: %d\n", sym_count);
        
        fclose(outfile);
    }

  2. #2
    Registered User
    Join Date
    Aug 2006
    Posts
    100
    One example: If it is a space, or an upper case character, or a lower case character, how will the test for vowel be true?

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,304
    You don't need status or two fscanf calls. Instead:

    Code:
    while (fscanf(infile, "%c", &ch) == 1)  // fscanf returns number of fields scanned
    {
        ...
    }
    or the more usual

    Code:
    while ((ch = fgetc(infile)) != EOF)
    {
        ...
    }
    But the fgetc requires that ch be declared as an int.

    Your isupper and islower tests will also catch vowels, so you'll need to test for vowels before them.

    I assume you think you're converting to lowercase for the vowel test, but if the isupper case is true then the vowel test will never happen since only one of the nested if/else if blocks will execute.

    You should probably convert to lowercase as a separate step.

    Code:
    int ch;
    
    while ((ch = fgetc(infile)) != EOF)
    {
        ch = tolower(ch);  // no need for the isupper test
    
        if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
            vowel++;
        else if (isalpha(ch))
            consonant++;
        else if (isdigit(ch))
            digit++;
        else if (isspace(ch))
            space++;
        else
            other++;
    }
    Still, this doesn't actually explain your extraordinary output if your input is just:
    Code:
    This line has numbers: 1, 2,3, 4, 55
    Some lines include punctuations such as , ; . / & ^
    Anyway, make the above changes and try again.

  4. #4
    Registered User
    Join Date
    Feb 2017
    Posts
    4
    Okay, I corrected my errors, but it still seems to output extraordinary data.

    My revised main function looks like this:

    Code:
    int ch;
    	int space_count = 0, con_count = 0, digit_count = 0, vowel_count = 0, other = 0;
    	
    	FILE *infile;
    	
    	infile=fopen("input.txt", "r");
    	if (infile == NULL)
        {
    		printf("ERROR COMPUTING FILE\n");
    		exit(EXIT_FAILURE);
    	}
    	
    	
    	while ((ch = fgetc(infile)) != EOF)
    		{
    			ch = tolower(ch);
    			
    			if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
    			
    				vowel_count++;
    			
    		else if (isalpha(ch))
    			
    				con_count++;
    			
    		else if (isdigit(ch))
    			
    				digit_count++;
    			
    		else if (isspace(ch))
    			
    				space_count++;
    			
    			else
    			
    				other++;
    		}
    	
    	
    	output_summary (space_count, con_count, digit_count, vowel_count, other);
    	
    	fclose(infile);
    	
    	return (0);
    
    
    }

  5. #5
    Registered User
    Join Date
    Jun 2015
    Posts
    1,304
    What OS and compiler are you using?
    Your file probably contains more than you think.
    What is the file size according to the system? You may need to look at the "properties" to see the size in bytes (on windows, at least).
    Are you sure the file you're looking at is the one being read?
    Maybe the program is reading from a different directory than you think.
    Try changing the file name to input2.txt and running the program. It shouldn't be able to open it, but if it still opens it then it's reading a different file.

    If that doesn't solve your problem, what does the following program print. Is it different than the file size (in bytes) reported by the OS?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
        FILE *f = fopen("input.txt", "r");
        if (!f) {perror("fopen"); exit(EXIT_FAILURE);}
        int c, cnt = 0;
        while ((c = fgetc(f)) != EOF)
            cnt++;
        fclose(f);
        printf("%d\n", cnt);
        return 0;
    }

  6. #6
    Registered User
    Join Date
    Feb 2017
    Posts
    4
    I solved it. The output displayed correctly. Thank you very much for the help.

    The default text editor on mac only has the option to save as a rich text format (very stupid), I then converted the .rtf to what I needed, a .txt. The .rtf carries many characters in the file that the program read and thought was apart of the actual .txt itself.

    I had to re-write the message in an online text program and save it as a .txt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wrong answer or just an output problem?
    By Mycarus in forum C Programming
    Replies: 2
    Last Post: 04-26-2015, 07:46 AM
  2. Need help with displaying the output
    By yoda in forum C Programming
    Replies: 4
    Last Post: 02-04-2013, 10:24 PM
  3. What's an acceptable file to store mesh data on?
    By shrink_tubing in forum C++ Programming
    Replies: 9
    Last Post: 07-02-2010, 04:10 PM
  4. File I/O - displaying output properly
    By Tropod in forum C++ Programming
    Replies: 4
    Last Post: 10-16-2009, 02:16 PM
  5. unpredictable answer output of factorial
    By Mathsniper in forum C Programming
    Replies: 5
    Last Post: 12-14-2006, 09:55 AM

Tags for this Thread

Website Security Test