Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C how to write whichever end of line character is appropriate to the OS?

Tags:

c

portability

Unix has \n, Mac was \r but is now \n and DOS/Win32 is \r\n. When creating a text file with C, how to ensure whichever end of line character(s) is appropriate to the OS gets used?

like image 424
Rob Kam Avatar asked Apr 21 '09 20:04

Rob Kam


People also ask

How do you check if you are at the end of a line in C?

“how to check end of line in c” Code Answer EOF) { printf("%d",n); //other operations with n.. }

What is line feed character in C?

The newline character ( \n ) is called an escape sequence, and it forces the cursor to change its position to the beginning of the next line on the screen. This results in a new line.

What character is EOF in C?

The EOF in C/Linux is control^d on your keyboard; that is, you hold down the control key and hit d. The ascii value for EOF (CTRL-D) is 0x05 as shown in this ascii table . Typically a text file will have text and a bunch of whitespaces (e.g., blanks, tabs, spaces, newline characters) and terminate with an EOF.

What is the basic difference between \n and t?

The \n symbol means literally new line. This will go to the start of the next new line. The \t symbol means add a tab (which is usually 4 spaces but can easily be 2 or 8 depending on the context).


1 Answers

fprintf(your_file, "\n");

This will be converted to an appropriate EOL by the stdio library on your operating system provided that you opened the file in text mode. In binary mode no conversion takes place.

like image 83
hlovdal Avatar answered Mar 22 '23 23:03

hlovdal