Does the wc command strip the trailing line feed from txt files?Copying files from command line to...

How to make a pipeline wait for end-of-file or stop after an error?

Please, smoke with good manners

Shrinkwrap tetris shapes without scaling or diagonal shapes

Why is it that the natural deduction method can't test for invalidity?

With a Canadian student visa, can I spend a night at Vancouver before continuing to Toronto?

French for 'It must be my imagination'?

Contradiction proof for inequality of P and NP?

Will a top journal at least read my introduction?

Apply MapThread to all but one variable

What route did the Hindenburg take when traveling from Germany to the U.S.?

Packing rectangles: Does rotation ever help?

How can I practically buy stocks?

How much cash can I safely carry into the USA and avoid civil forfeiture?

How can I place the product on a social media post better?

What is the relationship between spectral sequences and obstruction theory?

What does the "ep" capability mean?

What does KSP mean?

Controversial area of mathematics

Why other Westeros houses don't use wildfire?

Do I have to worry about players making “bad” choices on level up?

Any examples of headwear for races with animal ears?

What makes accurate emulation of old systems a difficult task?

Will tsunami waves travel forever if there was no land?

What happened to Captain America in Endgame?



Does the wc command strip the trailing line feed from txt files?


Copying files from command line to clipboardHow to strip directory structure with command line (or script)Booting from command lineInitiating programs from command lineCompressing files using p7zip from the command-lineEdit HTML files from the command lineHow does this `cat` command to txt script file work?What does the `..` command do?Open Link (type 2) desktop files from the command linecommand line tool to download complete podcast from rss feed






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







0















I'm currently building a replica of the wc command line call (in C). I have a file [tst.txt]tst.txt
and the C code to read that file. The wc tst.txt command responds with the output: 2 6 20 tst.txt, meaning 2 line feeds ('n'). My code, however, counts 3 line feeds. I am assuming this is due to the systematic trailing new line at the end of the file (following Line 3).



Am I correct in thinking that the wc command strips the trailing line feed (by trailing I mean at the EOF), or is a piece of my code incorrect?



Could be that I am incrementing an extra unit?



Here is my code:



#include <stdio.h>
#include <string.h>

int checkForNewLine(char* line, int lineSize);

int main(int argc, char **argv) {
// declare variables
FILE *inputFile; // pointer to inputted file
inputFile = fopen(argv[1], "r"); // set input file to 2nd cmd-line arg.
int newLineCount = 0;
int newLineIncr = 0;

// if file is not found
if (inputFile == NULL){
printf("%s", "File not foundn");
return (-1); // end program
}

char line[201]; // set line to 200 char MAX.


while (fgets(line, 201, inputFile) != NULL){

// new line count
newLineCount = newLineCount + checkForNewLine(line, 201);
}
if (feof(inputFile)) {
}
else {
printf("%s", "Some Other Error...");
}

printf("New Line Count [%d]n", (newLineCount));

fclose(inputFile);

}

int checkForNewLine(char *line, int lineSize){
int count = 0;
for (int i = 0; i < lineSize; i++) {
if (line[i] == ''){
count++;
printf("count amount: %dn", count);
break;
}
}
return count;
}








share







New contributor




slick is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



























    0















    I'm currently building a replica of the wc command line call (in C). I have a file [tst.txt]tst.txt
    and the C code to read that file. The wc tst.txt command responds with the output: 2 6 20 tst.txt, meaning 2 line feeds ('n'). My code, however, counts 3 line feeds. I am assuming this is due to the systematic trailing new line at the end of the file (following Line 3).



    Am I correct in thinking that the wc command strips the trailing line feed (by trailing I mean at the EOF), or is a piece of my code incorrect?



    Could be that I am incrementing an extra unit?



    Here is my code:



    #include <stdio.h>
    #include <string.h>

    int checkForNewLine(char* line, int lineSize);

    int main(int argc, char **argv) {
    // declare variables
    FILE *inputFile; // pointer to inputted file
    inputFile = fopen(argv[1], "r"); // set input file to 2nd cmd-line arg.
    int newLineCount = 0;
    int newLineIncr = 0;

    // if file is not found
    if (inputFile == NULL){
    printf("%s", "File not foundn");
    return (-1); // end program
    }

    char line[201]; // set line to 200 char MAX.


    while (fgets(line, 201, inputFile) != NULL){

    // new line count
    newLineCount = newLineCount + checkForNewLine(line, 201);
    }
    if (feof(inputFile)) {
    }
    else {
    printf("%s", "Some Other Error...");
    }

    printf("New Line Count [%d]n", (newLineCount));

    fclose(inputFile);

    }

    int checkForNewLine(char *line, int lineSize){
    int count = 0;
    for (int i = 0; i < lineSize; i++) {
    if (line[i] == ''){
    count++;
    printf("count amount: %dn", count);
    break;
    }
    }
    return count;
    }








    share







    New contributor




    slick is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      0












      0








      0








      I'm currently building a replica of the wc command line call (in C). I have a file [tst.txt]tst.txt
      and the C code to read that file. The wc tst.txt command responds with the output: 2 6 20 tst.txt, meaning 2 line feeds ('n'). My code, however, counts 3 line feeds. I am assuming this is due to the systematic trailing new line at the end of the file (following Line 3).



      Am I correct in thinking that the wc command strips the trailing line feed (by trailing I mean at the EOF), or is a piece of my code incorrect?



      Could be that I am incrementing an extra unit?



      Here is my code:



      #include <stdio.h>
      #include <string.h>

      int checkForNewLine(char* line, int lineSize);

      int main(int argc, char **argv) {
      // declare variables
      FILE *inputFile; // pointer to inputted file
      inputFile = fopen(argv[1], "r"); // set input file to 2nd cmd-line arg.
      int newLineCount = 0;
      int newLineIncr = 0;

      // if file is not found
      if (inputFile == NULL){
      printf("%s", "File not foundn");
      return (-1); // end program
      }

      char line[201]; // set line to 200 char MAX.


      while (fgets(line, 201, inputFile) != NULL){

      // new line count
      newLineCount = newLineCount + checkForNewLine(line, 201);
      }
      if (feof(inputFile)) {
      }
      else {
      printf("%s", "Some Other Error...");
      }

      printf("New Line Count [%d]n", (newLineCount));

      fclose(inputFile);

      }

      int checkForNewLine(char *line, int lineSize){
      int count = 0;
      for (int i = 0; i < lineSize; i++) {
      if (line[i] == ''){
      count++;
      printf("count amount: %dn", count);
      break;
      }
      }
      return count;
      }








      share







      New contributor




      slick is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      I'm currently building a replica of the wc command line call (in C). I have a file [tst.txt]tst.txt
      and the C code to read that file. The wc tst.txt command responds with the output: 2 6 20 tst.txt, meaning 2 line feeds ('n'). My code, however, counts 3 line feeds. I am assuming this is due to the systematic trailing new line at the end of the file (following Line 3).



      Am I correct in thinking that the wc command strips the trailing line feed (by trailing I mean at the EOF), or is a piece of my code incorrect?



      Could be that I am incrementing an extra unit?



      Here is my code:



      #include <stdio.h>
      #include <string.h>

      int checkForNewLine(char* line, int lineSize);

      int main(int argc, char **argv) {
      // declare variables
      FILE *inputFile; // pointer to inputted file
      inputFile = fopen(argv[1], "r"); // set input file to 2nd cmd-line arg.
      int newLineCount = 0;
      int newLineIncr = 0;

      // if file is not found
      if (inputFile == NULL){
      printf("%s", "File not foundn");
      return (-1); // end program
      }

      char line[201]; // set line to 200 char MAX.


      while (fgets(line, 201, inputFile) != NULL){

      // new line count
      newLineCount = newLineCount + checkForNewLine(line, 201);
      }
      if (feof(inputFile)) {
      }
      else {
      printf("%s", "Some Other Error...");
      }

      printf("New Line Count [%d]n", (newLineCount));

      fclose(inputFile);

      }

      int checkForNewLine(char *line, int lineSize){
      int count = 0;
      for (int i = 0; i < lineSize; i++) {
      if (line[i] == ''){
      count++;
      printf("count amount: %dn", count);
      break;
      }
      }
      return count;
      }






      command-line terminal wc





      share







      New contributor




      slick is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share







      New contributor




      slick is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share



      share






      New contributor




      slick is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 36 mins ago









      slickslick

      1




      1




      New contributor




      slick is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      slick is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      slick is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          1 Answer
          1






          active

          oldest

          votes


















          0














          From man 3 fgets:



          The fgets() function shall read bytes from stream into the array
          pointed to by s, until n−1 bytes are read, or a <newline> is read and
          transferred to s, or an end-of-file condition is encountered.


          So your code counts the last line, irrespective of whether it had a newline at the end of it (which it doesn't), because EOF was encountered. After all, the checkForNewLine() function is checking for, well, null characters, not newlines. Use od, hexdump, etc. to verify what the last character of your input file is.





          share
























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "106"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });






            slick is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f515945%2fdoes-the-wc-command-strip-the-trailing-line-feed-from-txt-files%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            From man 3 fgets:



            The fgets() function shall read bytes from stream into the array
            pointed to by s, until n−1 bytes are read, or a <newline> is read and
            transferred to s, or an end-of-file condition is encountered.


            So your code counts the last line, irrespective of whether it had a newline at the end of it (which it doesn't), because EOF was encountered. After all, the checkForNewLine() function is checking for, well, null characters, not newlines. Use od, hexdump, etc. to verify what the last character of your input file is.





            share




























              0














              From man 3 fgets:



              The fgets() function shall read bytes from stream into the array
              pointed to by s, until n−1 bytes are read, or a <newline> is read and
              transferred to s, or an end-of-file condition is encountered.


              So your code counts the last line, irrespective of whether it had a newline at the end of it (which it doesn't), because EOF was encountered. After all, the checkForNewLine() function is checking for, well, null characters, not newlines. Use od, hexdump, etc. to verify what the last character of your input file is.





              share


























                0












                0








                0







                From man 3 fgets:



                The fgets() function shall read bytes from stream into the array
                pointed to by s, until n−1 bytes are read, or a <newline> is read and
                transferred to s, or an end-of-file condition is encountered.


                So your code counts the last line, irrespective of whether it had a newline at the end of it (which it doesn't), because EOF was encountered. After all, the checkForNewLine() function is checking for, well, null characters, not newlines. Use od, hexdump, etc. to verify what the last character of your input file is.





                share













                From man 3 fgets:



                The fgets() function shall read bytes from stream into the array
                pointed to by s, until n−1 bytes are read, or a <newline> is read and
                transferred to s, or an end-of-file condition is encountered.


                So your code counts the last line, irrespective of whether it had a newline at the end of it (which it doesn't), because EOF was encountered. After all, the checkForNewLine() function is checking for, well, null characters, not newlines. Use od, hexdump, etc. to verify what the last character of your input file is.






                share











                share


                share










                answered 6 mins ago









                murumuru

                38.1k590166




                38.1k590166






















                    slick is a new contributor. Be nice, and check out our Code of Conduct.










                    draft saved

                    draft discarded


















                    slick is a new contributor. Be nice, and check out our Code of Conduct.













                    slick is a new contributor. Be nice, and check out our Code of Conduct.












                    slick is a new contributor. Be nice, and check out our Code of Conduct.
















                    Thanks for contributing an answer to Unix & Linux Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f515945%2fdoes-the-wc-command-strip-the-trailing-line-feed-from-txt-files%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

                    Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

                    Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...