1

I'm using a modified fgets() function called s_gets() that removes a newline from input or discards any of the remaining characters in the input buffer. It looks like the following;

char *s_gets(char *str, int n, FILE *pf) {

    char *ret_val;
    char *find;

    ret_val = fgets(str, n, pf);
    if (ret_val) {
        find = strchr(str, '\n');
        if (find) {
            puts("Newline was found.");
            printf("Character before \\n is %c\n", *(find - 1));
            *find = '\0';
        } else {
            while (getchar() != '\n')
                continue;
        }
    }
    return ret_val;
}

When I use this function and pass it a FILE* to a file containing just the string apple on a single line, the puts() inside the if clause runs and the printf() statement prints Character before \n is e. My question is where is this mysterious newline coming from? Does this have anything to do with EOF? I'm compiling this with Apple LLVM version 10.0.0 (clang-1000.10.44.2) on macOS 10.14.

BlaqICE
  • 309
  • 2
  • 11
  • 4
    Your file.contains a newline character. Your editor adds it for you. Text files should generally contain complete lines. – n. m. could be an AI Oct 06 '18 at 13:01
  • Why do you read from stdin if you do read a line from a file that doesn't end in a newline? (If you change that `getchar()` to `fgetc()` reading from the same file pointer, it'll be an infinite loop, btw) – Shawn Oct 06 '18 at 13:09
  • 2
    Use the command `printf apple>text.file` to create a file without a final newline. Verify with `od -c text.file` or `xxd -g1 text.file`. – Jonathan Leffler Oct 06 '18 at 13:51

1 Answers1

3

Even if the string "apple" is written on a single line, a newline character is automatically added to the end of that line by the editor (gedit for example). That's why you see it.


PS: As rici mentioned: Why should text files end with a newline?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • 1
    . o O ( I'd throw an editor that does something like that away ) – Swordfish Oct 06 '18 at 13:20
  • @Swordfish that would take you some time: https://askubuntu.com/questions/13317/how-to-stop-gedit-gvim-vim-nano-from-adding-end-of-file-newline-char – gsamaras Oct 06 '18 at 13:25
  • 2
    [Why should files end with a newline?](https://stackoverflow.com/questions/729692/why-should-files-end-with-a-newline) – rici Oct 06 '18 at 13:51
  • 1
    *If* a final new-line is added "*automagically*" or not heavily depends on which editor is used and how it is configured. – alk Oct 06 '18 at 14:11
  • Thanks I had no idea this happened. I should have mentioned that I used vim. Not sure what version as I'm not currently near my mac, but there's nothing crazy in my .vimrc. What happens if you actually add the newline? Does the line end up having 2 newline characters, or does it only add one if you don't? – BlaqICE Oct 06 '18 at 15:44
  • Any decent text editor is binary-clean and does not do this. Such a file (ending in an incomplete line) is not a text file, but that's not a valid reason for the editor to corrupt it. – R.. GitHub STOP HELPING ICE Oct 06 '18 at 16:36
  • 1
    @BlaqICE: You might want to make yourself comfortable with a hex-editor or hex-dumper at least (see the `hexdump` tool on Linux for example) to understand the real content of a file. – alk Oct 06 '18 at 17:35