Thread: Why My Code Does not Count The Number Of Lines in the txt file

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    10

    Why My Code Does not Count The Number Of Lines in the txt file

    Here is my CODE:
    Code:
    int main(){
    int numberoflines=0;
    char findinglines;
    FILE *tabloline;
    tabloline=fopen("sporttable.txt","a");
    for(findinglines=getc(tabloline);findinglines!=EOF;findinglines=getc(tabloline))
    {
        if(findinglines=='\n')
        numberoflines=numberoflines+1;
    }
    printf("%d",numberoflines);
    return 0;
    }
    Text in the file:



    Player G A RC PS
    Mehmet Ekinci 9 11 5 10
    Volkan Sen 12 8 1 15
    Caner Erkin 3 8 2 8
    Selcuk Inan 4 8 0 13
    Felipo Melo 1 1 0 17

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,683
    You're opening the file for appending("a") instead of reading("r").
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    80
    [...]

    Quote Originally Posted by kayra09 View Post
    Code:
        numberoflines=numberoflines+1;
    [...]

    Yet better is :

    Code:
    numberoflines++;

  4. #4
    Registered User
    Join Date
    Apr 2017
    Location
    Iran
    Posts
    80
    Quote Originally Posted by GReaper View Post
    You're opening the file for appending("a") instead of reading("r").
    IMHO it is better to choose between text or binary file each time. use "rt" to read a text file, "rb" to read a binary file.

    More info

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,683
    Quote Originally Posted by ordak View Post
    IMHO it is better to choose between text or binary file each time. use "rt" to read a text file, "rb" to read a binary file.
    I've never seen use of "t", and I don't think it's part of the standard. A file stream defaults to text mode anyway.
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Mar 2019
    Posts
    10
    Yea I see my mistake now.When I used "r" instead of "a" it kinda worked I guess.I'm getting 5 lines instead of 6.

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,683
    Quote Originally Posted by kayra09 View Post
    I'm getting 5 lines instead of 6.
    Yes, that probably happens because the last line has no '\n' character. To fix that bug, you could count the amount of characters in the current line, and if you reach EOF while the amount is greater than zero, count that as a line too.
    Devoted my life to programming...

  8. #8
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    615
    Quote Originally Posted by ordak View Post
    IMHO it is better to choose between text or binary file each time. use "rt" to read a text file, "rb" to read a binary file.

    More info
    "rt" is needed by Windows compilers to translate the "\r\n" line termination of DOS/Windows text files to a single "\n" char in memory. "bt" make all files binary and no translation occurs.

    Linux, UNIX, OS/X, all do not need this translation, as the line termination in text files is simply a newline char, "\n". No distinction is made between "text" or "binary" files in these three O/S's.

  9. #9
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    625
    Quote Originally Posted by rstanley View Post
    "rt" is needed by Windows compilers to translate the "\r\n" line termination of DOS/Windows text files to a single "\n" char in memory. "bt" make all files binary and no translation occurs.
    Are you sure Windows needs that? Since fopen is supposed to open a file in "text" mode by default, that would mean Microsoft is not following the standards (again), which really wouldn't surprise me very much.

    Quote Originally Posted by kayra09 View Post
    I'm getting 5 lines instead of 6.
    That would be a problem with your input file. Each line must end with the newline character(s)*, otherwise it's an incomplete line**.

    * Windows uses "\r\n", and the civilized world (Linux, Mac OS X, etc) uses "\n" as the newline character(s) in text files.

    ** The newline character(s) should be used as a line terminator, not as a line separator. If it were used as a line separator, a text file with zero lines would look identical to a file with one empty line (i.e., the file would be zero bytes long). Unfortunately, many text editors confusingly display an empty line at the end of the file when the last line is properly terminated, and many people remove the line terminator from the last line to remove that mythical "empty line".

  10. #10
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    615
    Quote Originally Posted by christop View Post
    Are you sure Windows needs that? Since fopen is supposed to open a file in "text" mode by default, that would mean Microsoft is not following the standards (again), which really wouldn't surprise me very much.
    I haven't used a Windows compiler for many years, so I don't know, or remember what the default is set to. I was simply explaining that "rt" and "rb" are used in Windows programming, not in other O/S's.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    26,837
    It is true that the use of "t" in a mode is non-standard, so the advice to 'use "rt" to read a text file' makes the code non-standard. For Microsoft compilers, text mode is still the default, so it is not true that '"rt" is needed by Windows compilers to translate the "\r\n" line termination of DOS/Windows text files to a single "\n" char in memory'. However, it is possible to change the default mode to binary on Microsoft compilers, so that's why Windows programmers probably use "rt" to make certain that their code works as expected even if the default is changed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 09-12-2016, 03:21 AM
  2. Count lines in file
    By Xpload in forum C Programming
    Replies: 12
    Last Post: 12-21-2009, 06:55 AM
  3. Count number of lines in a text file
    By eXeCuTeR in forum C# Programming
    Replies: 3
    Last Post: 04-30-2008, 01:46 AM
  4. count only lines of code...
    By flightsimdude in forum C Programming
    Replies: 13
    Last Post: 09-23-2003, 07:08 PM
  5. how do I count the lines in a text file?
    By slow brain in forum C Programming
    Replies: 4
    Last Post: 03-10-2003, 02:56 PM

Tags for this Thread