cant find where my error is in the line counter

This is a discussion on cant find where my error is in the line counter within the C Programming forums, part of the General Programming Boards category; Everytime I compile, A value of 1 is returned for the total number of lines. I am still very new ...

  1. #1
    Registered User Led Zeppelin's Avatar
    Join Date
    Mar 2002
    Posts
    17

    cant find where my error is in the line counter

    Everytime I compile, A value of 1 is returned for the total number of lines. I am still very new at C, so I am sure I am overlooking something stupid. Just need a little direction. Thanks in advance for your time.



    Here is the code:

    #include <stdio.h>
    #include <conio.h>




    main()
    {


    char symbol;
    int preSymbol;
    int lineCount = 0;
    int symbolCount = 0;
    FILE *gettysburg;


    if (!(gettysburg = fopen("A:\\gettysburg.txt", "r")))
    {
    printf("Unable to open file for reading");
    return (1);
    }
    while ((symbol = fgetc(gettysburg)) != EOF)
    {
    if (symbol != '\n')
    symbolCount++;
    else
    lineCount++;
    preSymbol = symbol;
    }
    if (preSymbol != '\n')
    lineCount++;

    printf("The number of characters is: %d\n", symbolCount);
    printf("The number of lines is: %d\n", lineCount);

    fclose(gettysburg);
    getch();
    return 0;




    }

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    I compiled your code and it worked.

    Test Data:
    hello
    world

    Results:
    The number of characters is: 10
    The number of lines is: 2

    And check this out for formatting your posts http://www.cprogramming.com/cboard/s...threadid=13473
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  3. #3
    www.entropysink.com
    Join Date
    Feb 2002
    Posts
    605
    If the text in gettysburg.txt is all one line (i.e. no "hard breaks"), then the number of lines will be 1. Post gettysburg.txt if this doesn't make sense.
    Visit entropysink.com - It's what your PC is made for!

  4. #4
    Registered User Led Zeppelin's Avatar
    Join Date
    Mar 2002
    Posts
    17

    Thanks

    Thanks,
    ROB and C-Coder

    Rob thanks for pointing out obout the hard enter, I am taking my
    first class in C and my assignment was to copy gettysburg address and save it as a text file on the a: drive. We were told to compose it in notepad, but most word processing apps that I know of dont require a hard return. Is there an option that I can change to have C recognize hard returns. Well thanks for the help it was much appreciated.



    http://www.angelfire.com/ny2/scutlys...s/colorzep.gif

  5. #5
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    How is your file written?
    Give us more info.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  6. #6
    Registered User Led Zeppelin's Avatar
    Join Date
    Mar 2002
    Posts
    17
    I just typed it in notepad and saved it on a 1.44" floppy. I had word wrap checked. Not sure what else you need. But I attached a copy of the text.

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Never find EOF after improperly formatted input
    By MALDATA in forum C Programming
    Replies: 5
    Last Post: 09-30-2008, 02:24 PM
  2. adding line numbers and concatenating a filename
    By durrty in forum C Programming
    Replies: 25
    Last Post: 06-28-2008, 04:36 AM
  3. Very strange error...
    By Blackroot in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2006, 01:37 AM
  4. Trouble replacing line of file
    By Rpog in forum C Programming
    Replies: 4
    Last Post: 04-19-2004, 11:22 AM
  5. Read only one line using seekg
    By RedZippo in forum C++ Programming
    Replies: 3
    Last Post: 03-31-2004, 11:10 PM

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