0

I run this code: (On Ubuntu 16.04 with VMware)

#include <stdio.h>
#include <string.h>
#define ARRAY_SIZE 50


int main()
{
    char str[ARRAY_SIZE];
    int i;

    FILE* fp = fopen("test.txt", "r");
    if(fp == NULL)
    {
        printf("File hasn't opened.\n");
        return 1;
    }

    while(fgets(str, ARRAY_SIZE, fp) != NULL)
    {
        printf("str length is %d\n", strlen(str));
        for(i = 0; i < strlen(str); i++)
            printf("index is %d, char is %d\n", i, str[i]);
    }

    fclose(fp);

    return 0;
}

test.txt has only 1 character in it (the character '1'), no newline character after the only character.

Why strlen(str) returns 2 and not 1?

And why the second character is a newline character if there's no newline character in test.txt?

  • 2
    "*if there's no newline character in test.txt?*" Are you really sure there isn't? What is the file's size in bytes? – alk Dec 22 '20 at 16:35
  • @alk the size is 2 bytes – Albert Leibnitz Dec 22 '20 at 16:37
  • @AlbertLeibnitz: That's because there's a newline on the end. I ran your code and verified manually. No newline on the end, no newline from `fgets`. – Joshua Dec 22 '20 at 16:38
  • 3
    If the file size is 2 bytes then there is no more question. – Jabberwocky Dec 22 '20 at 16:38
  • 3
    @AlbertLeibnitz: So, what would the 2nd byte most likely be then? ;) – alk Dec 22 '20 at 16:40
  • 3
    The file might have only one *visible* character, but if the size is two then there must be a second character hiding in it. What is the output you get from your program? More specifically, what is the output of the inner loop where you print out the contents of the array `str`? Please [edit] your question to show the output. – Some programmer dude Dec 22 '20 at 16:46
  • @alk I didn't put the newline character there. Maybe it's the app I use (gedit)? The `test.txt` file includes only '1' character in it. It has only 1 line in it, so how there's a newline character if there's only 1 line and 1 character in that only line? – Albert Leibnitz Dec 22 '20 at 16:46
  • Especially under Linux, many texteditors will end a file with a new-line character, because that's the "proper" way text files should look. Perhaps gedit has a setting for that? (But I think your program should assume that there can be a new-line character at the end of input you get from `fgets` and act accordingly.) – M Oehm Dec 22 '20 at 16:49
  • What is your platform? If it's linux/macos try `od -x test.txt` in a shell and tell us what the output is. I'm sure it's something like `30 0A` – Jabberwocky Dec 22 '20 at 16:51
  • A text file that does _not_ have a newline at the end is considered "malformed" by most editors [under posix systems]. For example if you _did_ have a file that had _no_ newline at the end, if you do: `vi file` and then `:w`, and `:q`, then `vi` will "correct" the file – Craig Estey Dec 22 '20 at 16:52
  • @Jabberwocky 1)`0000000 0a31` 2)`0000002` – Albert Leibnitz Dec 22 '20 at 16:59
  • @AlbertLeibnitz so there _is_ a newline in the file. The `0a` is the newline, and the `31` is the `'1'`. That's exactly what everybody has been telling you during the last 20 minutes. – Jabberwocky Dec 22 '20 at 17:00
  • you can also verify with `wc -c test.txt` and see how many bytes its printing. – IrAM Dec 22 '20 at 17:11
  • Try `oc -c test.txt` to see the contents of the file – Ed Heal Dec 22 '20 at 17:27
  • If your file has 1 line in it, that would indicate that it contains a newline. If it does not have a newline, then the file would have 0 lines. – William Pursell Dec 22 '20 at 17:36
  • @WilliamPursell not really, if a textfile contains just the 3 letters `ABC` it contains 1 line that you can read with `fgets` without problems. If it contains the 3 letters `ABC` followed by a newline ( `ABC\n`) it contains 2 lines, the second line being empty. At least that's what all my text editors tell me. – Jabberwocky Dec 22 '20 at 17:54
  • @Jabberwocky You've got it wrong. ABC\n is exactly one line. An editor telling you otherwise has an off-by-one error or worse. A file with ABC does not contain anything looking like a line as defined by POSIX, because POSIX lines *must* end with a newline. C implementations are free to handle non-POSIXly formed input as they see fit. – Jens Dec 22 '20 at 18:08
  • @Jens OK if we're talking about POSIX the definition it maybe different, but in real world there are many text files around that don't have a `\n` at the end of the last line, and I never had the slightest problems with them. All text editors I use have no problem with dealing with such files either, and I've never seen an `fgets` implementation that can't handle text files that don't end with a `\n` – Jabberwocky Dec 22 '20 at 18:15
  • If we allow `wc` to be the arbiter of the count, then indeed a file with no newline has zero lines. `printf abc | wc -l` – William Pursell Dec 22 '20 at 20:15
  • OT: regarding: `printf("str length is %d\n", strlen(str));` The function: `strlen()` returns a `size_t`. not a `int`. – user3629249 Dec 23 '20 at 19:03

0 Answers0