Help with my source code

This is a discussion on Help with my source code within the C Programming forums, part of the General Programming Boards category; Can anyone tell me why this code won't run. I'm trying to write a code that reads the number of ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    18

    Help with my source code

    Can anyone tell me why this code won't run.
    I'm trying to write a code that reads the number of lines in a text file.

    \code
    #include <stdio.h>


    int main (void)
    {

    FILE *inp;
    int count = 0;

    inp = fopen("Text.txt", "r");
    while (inp != EOF);
    {
    fscanf (inp, "%[^\n]", &count);
    count++;

    if(inp == '\n')
    count++;

    }

    printf("The counter is %d", count);
    fclose(inp);
    return (0);
    }

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Oi, was this not just recently discussed?

    First of all, as i recall the expression in while loop is no-good. Secondly, using fscanf is gross. Thirdly:

    count++;

    if(inp == '\n')
    count++;


    What???? I suggest you read some man pages. Check out feof and fgets. And lean wha ta FILE* is and why if(inp == '\n') is so radiclous.

    If my condasending attitude doesn't help, I'll try to be nicer next time.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Stop spamming the board with this topic! Stick to one thread!

    First off, this is wrong:

    while (inp != EOF);

    Don't test this value. You've already been told this too. You have a bad habbit of ignoring what people tell you. Stop. You should be testing the return value of the read function, not just seeing if the pointer itself is EOF.

    I'm done with this topic. If you haven't learned by now, you never will. Stop spamming the board. If I was a moderator, I'd be deleting these.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    18

    need help

    How about this code

    #include <stdio.h>


    int main (void)
    {

    FILE *filePtr;
    int lineCounter = 0;
    char ch;

    filePtr = fopen("Text.txt", "r");

    while( (ch = getc(filePtr)) != EOF)
    {
    if(ch == '\n') lineCounter++;
    }

    printf("The number of lines in the file is %d.\n", lineCounter);



    fclose(filePtr);
    return (0);
    }

  5. #5
    CS Author and Instructor
    Join Date
    Sep 2002
    Posts
    511

    Wink

    Hey you are not using code tags!!


    The code looks ok. Did you compile and run the code yourself to see if it works correctly.
    Mr. C: Author and Instructor

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    18

    re how about this code

    Yeah it compiles but it an error message pops up?

  7. #7
    Registered User
    Join Date
    Nov 2002
    Posts
    491

    Re: re how about this code

    Originally posted by duty11694
    Yeah it compiles but it an error message pops up?
    Why do people insist on saying "i got an error" but always fail to say what the error is????

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    18

    Re need help

    I decided to try it with the fgets but it still gives an error window when it runs:

    [\code]
    #include <stdio.h>
    #define MAXLINE 100


    int main (void)
    {

    FILE *inputFile = NULL;
    int count = 0;
    char line[MAXLINE];


    inputFile = fopen("Text.txt", "r");

    while(fgets(line, MAXLINE, inputFile) !=NULL)
    {
    count++;
    }

    printf("The number of lines in the file is %d.\n", count);



    fclose(inputFile);
    return (0);
    }

    The error is:
    Debug assertion failed
    file: fgetc
    line: 60
    expression: str !=NULL

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: Re: re how about this code

    Originally posted by orbitz
    Why do people insist on saying "i got an error" but always fail to say what the error is????
    I've given up on this poster. They obviously have no desire to learn. They won't listen to a single thing they're told, and they still don't use code tags correctly. AND directly following your post, they post more code, and don't give the error again!

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Registered User
    Join Date
    Nov 2002
    Posts
    18
    Theres the error
    my bad

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    if ( inputFile == NULL ) {
          perror("Text.txt");
          return 1;
    }
    ... and use code tags please. See my signature for a link and an example on this.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seven Kingdoms I: Ancient Adversaries for Linux
    By MIH1406 in forum Projects and Job Recruitment
    Replies: 13
    Last Post: 01-17-2010, 04:03 PM
  2. How do you call another source code file?
    By nifear4 in forum C Programming
    Replies: 2
    Last Post: 10-28-2008, 12:16 PM
  3. DxEngine source code
    By Sang-drax in forum Game Programming
    Replies: 5
    Last Post: 06-26-2003, 05:50 PM
  4. Lines from Unix's source code have been copied into the heart of Linux????
    By zahid in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 05-19-2003, 03:50 PM
  5. Source Code Beautifier
    By Hammer in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 05-05-2002, 09:21 PM

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