-2

The file has empty new lines.

"a"
""
"b"
""
"c"

File is included into the program via the following method

fputs (
#include "file"
,stdout
);

actual result:

abc

Expected result:

a

b

c

3 Answers3

3

C merges string literals separated by whitespace. When you write

"a"
""
"b"
""
"c"

C compiler merges them into "abc"; you cannot detect the difference between the two sources once the code is compiled.

If you want line breaks, insert them into your string literals explicitly:

"a\n"
"\n"
"b\n"
"\n"
"c"
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2
 fputs (
 #include "file"
 ,stdout
 );

is legal, but poor taste and against most coding conventions.

What happens is well explained by dashblinkenight's answer, that I won't repeat.

However, I would suggest another approach. Modify your build system (e.g. your Makefile if using GNU make) to generate a better (and prettier) file.inc which would contain complete statements, perhaps a succession of

WRITELINE("a");
WRITELINE("");
WRITELINE("b");
WRITELINE("");

then, before your #include "file.inc" add in your C file:

#define WRITELINE(Lin) do { fputs(Lin,fil); fputc('\n', fil); } while(0)

(this question has good answers explaining why I want to use do{ ... }while(0) ....)

You could generate easily such a fil.inc (perhaps from your fil, with some make rule invoking sed or something else).

PS. Actually, generating C code is a good idea.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
-2

I agree with the answer above, but you may also want to add "\r". Some systems require it. This character will be the carriage return (13). So you would place "\n\r" between characters.

Jonathon S.
  • 1,928
  • 1
  • 12
  • 18
  • 1
    No, notably because on those systems you could `fopen` in *text* mode, not *binary* mode. – Basile Starynkevitch Jul 28 '17 at 15:25
  • 2
    You should never add `\r` if you want a newline! If you have a broken system which uses `"\r\n"`, leave it to the file functions as @BasileStarynkevitch wrote. – too honest for this site Jul 28 '17 at 15:26
  • The two commands are related to typewriters, but still have some application to terminal applications. `"\n"` indicates that a new line is needed, so in the absence of `"\r"` the terminal should place the character in the column to the right of the prior character and on the next row. `"\r"` indicates that the typing should resume at the beginning of the current line, so in the absence of `"\n"`, characters should begin overwriting characters at the beginning of a row. If the string is being communicated serially, it is important to include both in some applications. – Jonathon S. Jul 28 '17 at 16:43
  • [link] https://learn.sparkfun.com/tutorials/terminal-basics/basic-terminology- – Jonathon S. Jul 28 '17 at 16:44
  • 1
    @JonathonS: The stdio library automatically converts \n to \r\n for text streams in systems that requires it. It is only in very unusual circumstances, such as when the file is opened in binary mode, or when writing directly to the file descriptor, that you have to add the carriage return yourself. Not in this case, when writing to stdout. – Thomas Padron-McCarthy Jul 28 '17 at 19:49
  • That's probably correct. I am working with an embedded system. I am using customized functions for transmitting data to a serial port for efficiency. – Jonathon S. Jul 29 '17 at 21:19