• 0

[C++] carriage return with fputs()?


Question

Okay, it's 2am, and I'm at my wits end.

I'm trying to output a bunch of variables to a file, with 2 numbers and a carriage return on each line.

I have two global int arrays declared all with variables in them.

void FileOutput()
{
	FILE *f = fopen ("myfile.txt", "wb");

	if (f == NULL) die("Couldn't open myfile.txt for writing, sorry");
	else {
		int x;
		char temp_string[20];
		for (x = 0; x <= 4; x++)
		{
			sprintf(temp_string, "%i %i \n", x_pos[x], y_pos[x]);
			//printf("%i %i", x_pos[0], y_pos[0]);
			printf("Calm down sir, PLEASE\n");
			fputs (temp_string, f);
		}
	fclose(f);
	}

	printf("Don't worry sir, I'm from the internet\n");
}

Now, it puts it in the file fine, however, it all comes out as one line. The output on the screen comes out fine and I get carriage returns as expected there, however. I'd like it to come out this like this:

82 103

49 134

953 535

354 53

3535 234

The variables are purely aribtrary, this is an extension of a tutorial I'm following (I'm doing Wii homebrew development). I have to say, I've been doing programming for a while, and yet, I've never come across an issue quite like this that I haven't been able to resolve so easily.

Shoving \n everywhere does nothing (in regards to file output). I've tried injecting it when doing fputs() and also at the sprintf part, neither work. Honestly running out of ideas here. Anyone able to give me a hand figuring out what I'm doing wrong here?

Major thanks in advance.

Link to comment
https://www.neowin.net/forum/topic/997904-c-carriage-return-with-fputs/
Share on other sites

14 answers to this question

Recommended Posts

  • 0

Windows usually treats \r\n as a newline, whereas in unix/linux we just use a \n. Blame Microsoft for the inconsistency.

Not to distract from the topic, but Microsoft is not alone here. There are (or have been) at least 5 different newline combinations, \r\n being the widest employed on different Operating systems. According the Wiki, \r\n is even used in UNIX TTYs in "Raw Mode" although I'd be the first to admit that I'm no expert on the matter. If you want to get into semantics, Microsoft are arguably the ones doing it right, since both characters would have served an individual purpose in the beginning (although the purpose has long since passed). For example:

\r == Carriage Return, return the Caret to the start of the line. (Equivilent to pushing the paper back to the right side on a typewriter)

\n == Line Feed, move the caret down a line. (Equivalent to hitting the return key on a typewriter)

\r\n == Move the caret to the beginning of the line, and then down.

Based on that logic, \r\n is the one true newline. Such logic has long since been deprecated in computing, but if you're going to get into "inconsistency", Microsoft aren't the one's being inconsistent here, although a 2 character newline has long since surpassed being useful.

Thats right, I DID just make a 10 line post about the almighty newline!

  • Like 2
  • 0

Not to distract from the topic, but Microsoft is not alone here.

Perhaps not, but they could set an example and standardise on LF. The other main OS's, GNU/Linux, OSX, and unix all use LF to represent a line-break in text mode. It only serves to create portability problems and confuse programmers.

According the Wiki, \r\n is even used in UNIX TTYs in "Raw Mode" although I'd be the first to admit that I'm no expert on the matter.

Perhaps so, but "Raw Mode" is not standard text mode, which is what we are discussing here.

If you want to get into semantics, Microsoft are arguably the ones doing it right, since both characters would have served an individual purpose in the beginning (although the purpose has long since passed). For example:

\r == Carriage Return, return the Caret to the start of the line. (Equivilent to pushing the paper back to the right side on a typewriter)

\n == Line Feed, move the caret down a line. (Equivalent to hitting the return key on a typewriter)

\r\n == Move the caret to the beginning of the line, and then down.

CRLF is unnecessary verbosity in my opinion. I think we can agree though that standardisation would be a good thing for everybody.

Based on that logic, \r\n is the one true newline. Such logic has long since been deprecated in computing, but if you're going to get into "inconsistency", Microsoft aren't the one's being inconsistent here, although a 2 character newline has long since surpassed being useful.

The bigger problem is not necessarily how a newline is represented, but how the compiler's C library handles the escape characters in I/O functions such as printf(), fwrite():

The C standard only guarantees two things:

1. Each of these escape sequences maps to a unique implementation-defined number that can be stored in a single char value.

2. When writing a file in text mode, '\n' is transparently translated to the native newline sequence used by the system, which may be longer than one

character. When reading in text mode, the native newline sequence is translated back to '\n'. In binary mode no translation is performed, and the internal representation produced by '\n' is output directly.

Source.

The second one is interesting. I suppose technically, It means the OP's compiler/C library isn't standards compliant.

Thats right, I DID just make a 10 line post about the almighty newline!

No need to be ashamed, at least you put some thought into it :D

  • 0
The C standard only guarantees two things:

1. Each of these escape sequences maps to a unique implementation-defined number that can be stored in a single char value.

2. When writing a file in text mode, '\n' is transparently translated to the native newline sequence used by the system, which may be longer than one

character. When reading in text mode, the native newline sequence is translated back to '\n'. In binary mode no translation is performed, and the internal representation produced by '\n' is output directly.

Source.

The second one is interesting. I suppose technically, It means the OP's compiler/C library isn't standards compliant.

I did a little investigating, and MinGW (GCC for windows) at least does automatically translate a \n into a CRLF for you.

Code:

post-355430-0-00257400-1305644525.png

Terminal output:

post-355430-0-83335600-1305644533.png

Textmode file:

post-355430-0-71854200-1305644543.png

Edit: I just noticed you are opening the file with a "wb" mode. That indicates binary mode. Try just "w" for text mode. That seems to be the problem.

  • 0

I always use \n, whether it's C++, C#, Windows or Linux. And it always works as intended, although I'm not sure of the specifics. A very dumb editor like Notepad doesn't recognize \n as newline I think, but anything better than that (Wordpad, Notepad++, Visual Studio etc.) will display the text as intended, and reading line-by-line using any standard programming library (C++, Java, etc.) will also recognize \n as newline.

Just stop using notepad and you should be set. :p

  • 0

I always use \n, whether it's C++, C#, Windows or Linux. And it always works as intended, although I'm not sure of the specifics. A very dumb editor like Notepad doesn't accept \n as newline I think, but anything better than that (Wordpad, Notepad++, Visual Studio etc.) will display the text as intended, and reading line-by-line using any standard programming library (C++, Java, etc.) will also recognize \n as newline.

If the compiler and the C library it links programs with are standards compliant it will. Unfortunately a lot of compilers aren't very standards compliant.

However in the case of the OP, as stated in my last post, he's opening the file in binary writing mode (wb), when he should be using text writing mode (w). I tested it myself in Windows XP, and it works fine in text writing mode (w) with MinGW (GCC for windows).

  • 0
If the compiler and the C library it links programs with are standards compliant it will. Unfortunately a lot of compilers aren't very standards compliant.
I don't know of any recent compiler that does not implement that correctly.
However in the case of the OP, as stated in my last post, he's opening the file in binary writing mode (wb), when he should be using text writing mode (w).
That's true. From Wikipedia:
When writing a file in text mode, '\n' is transparently translated to the native newline sequence used by the system, which may be longer than one character. When reading in text mode, the native newline sequence is translated back to '\n'. In binary mode no translation is performed, and the internal representation produced by '\n' is output directly. http://en.wikipedia.org/wiki/Newline#In_programming_languages

However, as I said, the \n character is interpreted correctly by virtually anything but Notepad, including the standard C++ library.

Btw, since the language is C++, why not use the C++ standard library? Would have avoided the whole discussion in the first place.

void FileOutput() {
	ifstream f("myfile.txt");
	if (!f.is_open())
		die("Couldn't open myfile.txt for writing, sorry");
	else {
		for (int x = 0; x <= 4; x++) {
			f << x_pos[x] << " " << y_pos[x] << endl;
		}
	}        
	cout << "Don't worry sir, I'm from the internet" << endl;
}

Fool-proof and much more simple!

  • 0

Btw, since the language is C++, why not use the C++ standard library? Would have avoided the whole discussion in the first place.

void FileOutput() {
	cout << "Don't worry sir, I'm from the internet" << endl;
}

Fool-proof and much more simple!

AFAIK, 'cout << "Don't worry sir, I'm from the internet" << endl;' and 'cout << "Don't worry sir, I'm from the internet\n";' are analogous, that is, the standard library translates both the endl manipulator and the '\n' escape sequence into the system (OS) specific newline character(s), which in the case of Windows is CRLF, and in GNU/Linux is LF.

With regard to using the C++ standard library, I tend to agree. If you want to write C++, use the C++ extensions. The OP's code is just plain C, and so it can just be compiled as C without the C++ bloat. That being said, I think we should cut him some slack given that he's probably just experimenting.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • I think this will appeal to a lot of enthusiasts. If the thermals are similar to those of a single system in a compact case, many people running multiple systems may choose to combine them into one machine, provided the case footprint remains reasonable.
    • No, "a great deal" for 32GB of DDR5 is $50, not $350. I mean I see what you mean, that it's a decent price compared to what's currently available, but you really should put a disclaimer in this articles explaining that it's still multiple times more expensive than it used to be.
    • Linux 7.1 stable launch looms as Linus Torvalds releases the final release candidate by Paul Hill Linus Torvalds has just released what’s expected to be the final release candidate of Linux 7.1, rc7. The Linux founder said that this RC is not small, but smaller than recent releases, which is a good sign because he expects the stable version to drop next week if things continue on this trajectory. Linux kernels see a merge window for the first two weeks of their life, where developers add new features, then there are about seven or eight weeks of release candidates before the stable version. Typically, there are seven release candidates, but if more time is needed, then an eighth release candidate is released too. This week’s RC’s biggest area of fixes was for GPUs, with networking just behind. Torvalds said that the rest of the release was “pretty random and spread out” with some architecture fixes, driver fixes, filesystem improvements, and build fixes for more unusual configs. In terms of specific pieces of hardware receiving improvements in this update, we had more AMD Zen6 models supported and fixes for AMD SDMA 7.1 and GFX11. Hardware that got improvements includes Lenovo laptops, HONOR laptops, and MSI laptops. Here are the changelogs for those: ASoC: amd: acp: Add DMI quirk for Lenovo Yoga Pro 7 15ASH11 Input: atkbd - add DMI quirk for Lenovo Yoga Air 14 (83QK) Input: atkbd - skip deactivate for HONOR BCC-N's internal keyboard ASoC: amd: yc: Add MSI Raider A18 HX A9WJG to quirk table ASoC: amd: yc: Enable internal mic on MSI Bravo 17 C7VF When the stable Linux 7.1 is released, it will be up to distribution maintainers, such as Canonical and Red Hat, to release the update to their users via the update manager. Some versions of Linux will get it before others, and some will never get it at all. Fedora and Arch-based distros will be among the first to get it, though. If you don’t get it, the security fixes will be backported to your system’s kernel, so you won’t be at risk, but you won’t get newer hardware support, which is fine if your computer works now.
  • Recent Achievements

    • One Month Later
      DJC50PLUS earned a badge
      One Month Later
    • Week One Done
      DJC50PLUS earned a badge
      Week One Done
    • Proficient
      Eric Biran went up a rank
      Proficient
    • Dedicated
      Conjor earned a badge
      Dedicated
    • Week One Done
      Windows Guy earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      493
    2. 2
      PsYcHoKiLLa
      248
    3. 3
      Steven P.
      71
    4. 4
      ATLien_0
      68
    5. 5
      +Edouard
      68
  • Tell a friend

    Love Neowin? Tell a friend!