Like Tree1Likes
  • 1 Post By whiteflags

HELP IN THIS TOO..sorry i have an exam tommorow and thats why im posting alot :/

This is a discussion on HELP IN THIS TOO..sorry i have an exam tommorow and thats why im posting alot :/ within the C Programming forums, part of the General Programming Boards category; The questions asks to count number of characters and lines and print them from any number of files processed.. i ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    9

    HELP IN THIS TOO..sorry i have an exam tommorow and thats why im posting alot :/

    The questions asks to count number of characters and lines and print them from any number of files processed.. i dont seem to get anything useful from my program and i need ur help to fix it..its simialar to the cat command in linux but it should tell the number of characters and lines

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[]){
    int i;
    FILE *fp;
    char ch;
    int count;
    int lines;
    count=0;
    lines=0;
    char S[100];
    
           if(argc == 1){
                    fgets(S,100,stdin);
                    fputs(S,stdout);
            }
            for(i=1;i<argc;i++){
                    if((fp=fopen(argv[i],"r"))==NULL){
                            fprintf(stderr,"ERROR OPENNING FILE");
                    }
            }
                    while(( ch =fgetc(fp)) != EOF){
                            count++;
                            if(ch == '\n')
                                    lines++;
                    }
                    }
                    printf("Number of characters is %d\n",count);
                    printf("Number of lines is %d\n",lines);
                    fclose(fp);
                    }
    }

  2. #2
    Registered User
    Join Date
    Apr 2012
    Posts
    9
    CAN someone please reply ASAP!

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    All of the 'interesting' lines of code are correct, but your braces are all over the place. For a start you have more closing braces than opening ones, never going to make a compiler happy....

    If you have an if, or for, or while, followed by an opening brace, the program will loop over all the statements inside the braces. From your indentation it looks like you know your while loop should be nested inside the for loop, but you've closed the for loop on line 23. So your program will open all the files it was given, then it'd count the stuff in the last file it opened.

    If you make your indentation match your braces you should be able to sort this out pretty easily.

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    9
    Thank you for your reply, i fixed the braces and its working but regarding the number of characters its printing a wrong number, as in when i run my program with a file that has this simple content
    2
    3
    6

    The program Run would print
    Number of character is 6
    Number of lines 3

    WHich i dont get why character count not working?

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    Quote Originally Posted by smokeyangel View Post
    If you have an if[...] followed by an opening brace, the program will loop over all the statements inside the braces.
    If statements do not and will never loop any code under them.
    smokeyangel likes this.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    Quote Originally Posted by Yomna View Post
    Thank you for your reply, i fixed the braces and its working but regarding the number of characters its printing a wrong number, as in when i run my program with a file that has this simple content
    2
    3
    6

    The program Run would print
    Number of character is 6
    Number of lines 3

    WHich i dont get why character count not working?
    It is working: your file contains

    Code:
    2\n
    3\n
    6\n
    So three digits and 3 newline characters. I guess you don't want to count newlines as characters


    Code:
                            if(ch == '\n')
                                    lines++;
                            else
                                    count++;
    Would be 1 way to fix it

    Quote Originally Posted by whiteflags View Post
    If statements do not and will never loop any code under them.
    Whoops. Of course they don't! Brain

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    9
    Thank you..just one more think..if i type in a file name that doesnot exist, it prints the error message as it should but also it throws segmentation fault.. how can i have that fixed..cuz i reallly donnot know where else should i put my if statement?

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    It depends -- what do you want your program to do?

    If a file that doesn't exist is a fatal error and you want to quit, you can just return from main in your if statement.

    Code:
    if((fp=fopen(argv[i],"r"))==NULL){
        fprintf(stderr,"ERROR OPENNING FILE");
        return 1;
    }
    That'll just exit the program after printing the error.

    If you want to move on to the next file:
    Code:
    if((fp=fopen(argv[i],"r"))==NULL){
        fprintf(stderr,"ERROR OPENNING FILE");
        continue;
    }
    "continue" will go back to the top of the enclosing loop, in this case the for loop, and resume execution there.

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    2,517
    Note: On some systems the type of ch should NOT be char; int is the normal type returned by fgetc function.
    Tim S.
    Code:
    char ch;
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-22-2011, 08:09 PM
  2. Replies: 2
    Last Post: 01-23-2011, 12:40 PM
  3. alot?
    By Yarin in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 07-08-2008, 07:36 PM
  4. storing alot of variables...
    By MikeyIckey in forum C Programming
    Replies: 11
    Last Post: 05-30-2008, 12:31 PM
  5. Replies: 6
    Last Post: 08-18-2002, 08:01 AM

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