Hi everybody,

I want to write enter in a text file. How to do this. I wrote the code as follows. see if i write like this, it is printing one special character like arrow. I want to print in the following line, instead it is printing in single line with arrow.

How to do next line in the file write..

fprintf(out_fp,"----------------------------------------------------------\n");
fprintf(out_fp,"\tFBNO  FBCode         入力FB番号    出力FB番号  \n");
fprintf(out_fp,"----------------------------------------------------------\n");

Dani AI

Generated

Short summary and cause: the problem came from opening the file in binary mode so the C runtime did not perform the usual text-mode newline translation on Windows. correctly pointed out that text vs binary mode changes how line breaks are written, and 's switch to a text-mode open fixed the symptom. The “arrow”/weird glyph normally signals either a newline/encoding mismatch or that the viewer/editor is not interpreting the byte sequence the way it was written.

Practical guidance (portable and explicit):

  • For portable source code, emit a single platform-agnostic newline token in your strings and open the file in text mode so the C runtime does the translation on that platform.
  • If the program must emit a specific sequence (for example CR+LF on Windows) or if the file is opened in binary mode intentionally, write the platform bytes explicitly.

Example helper (choose newline at compile time):

#if defined(_WIN32)
#define NL "\x0D\x0A"   /* CR LF */
#else
#define NL "\x0A"       /* LF */
#endif

Encoding note (why non-ASCII looked odd): the vertical/arrow-like glyphs often mean the viewer/editor is using a different character encoding than the bytes in the file. If the text is UTF-8 but the viewer expects ANSI, characters look garbled. To make plain Notepad detect UTF-8 reliably, write the UTF‑8 BOM bytes before any text:

unsigned char bom[3] = { 0xEF, 0xBB, 0xBF };
fwrite(bom, 1, sizeof(bom), fp);

Quick checklist: confirm the fopen mode (text vs binary), decide whether to rely on runtime newline translation or emit CRLF explicitly, ensure the file encoding matches the editor (or write a BOM for UTF‑8), and use an editor (Notepad++, VS Code, etc.) that shows raw bytes/encodings while debugging.

Recommended Answers

All 2 Replies

how was the file opened? as text or binary? If binary then the next line character(s) depends on the operating system. In MS-Windows/MS-DOS you need "\r\n". *nix just "\n" and MAC "\r". If the file is opened in text mode then the operating system will make the appropriate translation of "\n" to whatever it needs.

I tried your three lines and it seemed to work ok for me. Opened the file with Notepad.exe and it looked ok (except for the Chinese characters). What editor are you using? If not English language then find out what it expects as line feeds.

Before i was opened the file as follows.

out_fp = fopen(oppath,"wb");

i am not writing into dat file. I am writing to text file. so i changed the code as follows and its working fine.. thanks

out_fp = fopen(oppath,"w");
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.