0

I'd like to print ALL characters stored in a string including escape sequences.

I know printing escape sequences will just print blank so I've made some modifications which first checks whether the character is a specific escape sequence then print '\\escape sequence' so I can visualize it.

Below is a portion of my c-code and output run in linux gcc environment.

I've found out '\n' is printable with my code but comparing with the output below (no escape sequence check) there is something missing which I don't know.

The text file was created in Windows so I assume for every end of the line there exists characters '\r\n' but '\r' doesn't seem to be there.

What am I missing ?

char c;
while ((c = fgetc(fp)) != EOF) {
    if (c == '\n') { printf("\\n\n"); }
    else if (c == '\r') { printf("\\r\n"); }
    else if (c == '\0') { printf("null\n"); }
    else { printf("%c\n", c); }
}
printf("\n----------------\n");
fseek(fp, 0, SEEK_SET);
while ((c = fgetc(fp)) != EOF) {
    printf("%c\n", c);
}

contents of the text file:

1

2

blank

blank

3

4

Output Result:

Output result

Output Result [CoffeeTableEspresso] asked for:

Output result <CoffeeTableEspresso> asked for

someDude
  • 21
  • 4

3 Answers3

3

If you open it in text mode, it will treat \r\n as \n, which is why you don't see the \r anywhere. If you want to see the \r, try opening it in binary mode. and doing the same thing, you should see \r as well as \n, unlike when you open the file in text mode.

EDIT:

to open for reading in text mode: fopen(filename, "r").

to open for reading in binary mode: fopen(filename, "rb").

CoffeeTableEspresso
  • 2,614
  • 1
  • 12
  • 30
  • Text mode: `fopen(..., "r")` and binary mode: `fopen(..., "rb")` – pmg Mar 15 '19 at 18:37
  • @CoffeTableEspresso I see. But I'm not fond with the text and binary mode. I assume text mode is when you actually double click and open the text file or use fopen() like my code. Then how can I open it in binary mode? – someDude Mar 15 '19 at 18:39
  • @pmg Thanks a lot that helped quick – someDude Mar 15 '19 at 18:40
  • @someDude text mode and binary mode only refers to how C handles the data in the file, for example the newline thing you encountered. I've edited my post to show how to open in either mode in C. Opening a file with a text editor is a completely different thing (although the text editor probably does open the file in text mode). – CoffeeTableEspresso Mar 15 '19 at 18:46
  • @CoffeTableEspresso Opened it in binary mode. Still the same result... – someDude Mar 15 '19 at 18:47
  • How are you creating the file? If you don't see any `\r` in binary mode, they're not there. – CoffeeTableEspresso Mar 15 '19 at 18:49
  • @CoffeTableEspresso My mistake. The file was created in Windows not Linux. Tried it with a Linux file and now it's working. Thanks for the help though – someDude Mar 15 '19 at 19:23
-1

Use a ascii flag to check for escape sequence character since every character has a ascii value associated with it. For windows as far as i know \r\n gets incorporated as \n

Taher Hozefa
  • 21
  • 1
  • 4
-1

CODE:

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

int main () {
    FILE *fp = fopen("text.txt","r");
    char c;

    while ((c = fgetc(fp)) != EOF) {
        if (c == '\n') { printf("\\n\n"); }
        else if (c == '\r') { printf("\\r\n"); }
        else if (c == '\0') { printf("null\n"); }
        else { printf("%c\n", c); }
    }
    printf("\n----------------\n");
    fseek(fp, 0, SEEK_SET);
    while ((c = fgetc(fp)) != EOF) {
        printf("%c\n", c);
    }

    fclose(fp);
    return(0);
}

file.txt

HELLO
BYE

Output:

H
E
L
L
O
\n
B
E
Y
\n

----------------
H
E
L
L
O


B
E
Y

I'm using macOS. I'm not sure, but was this the behaviour that you wanted?

  • I've already implemented it. I just don't know why '\n' can be read and '\r' not. In macOS I believe '\r' is not used in new line so it's not a problem but I'm on Windows which uses '\r\n'. – someDude Mar 15 '19 at 18:36
  • Hey, I tried this program with a UTF-8 Unicode text, with CRLF line terminators file and it showed me both \r and \n. If you want you can send the file your testing and I can try it here. – Carlos Joel Tavares Mar 15 '19 at 19:03
  • Thanks Carlos but problem solved! – someDude Mar 15 '19 at 19:21