• 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
  On 17/05/2011 at 01:08, Flawed said:

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
  On 17/05/2011 at 01:25, Majesticmerc said:

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.

  On 17/05/2011 at 01:25, Majesticmerc said:

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.

  On 17/05/2011 at 01:25, Majesticmerc said:

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.

  On 17/05/2011 at 01:25, Majesticmerc said:

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():

  Quote

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.

  On 17/05/2011 at 01:25, Majesticmerc said:

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
  On 17/05/2011 at 13:53, Flawed said:
  Quote
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
  On 17/05/2011 at 16:07, Dr_Asik said:

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
  Quote
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.
  Quote
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:
  Quote
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
  On 17/05/2011 at 16:36, Dr_Asik said:

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

    • It is fundamentally impossible to secure delete files, especially on newer HDDs and all SSDs. You never know how the controller firmware is going to layout data or copy data around to optimize access or invoke wear leveling. The only way to securely “delete” something is to put it on an encrypted file system and forget the encryption key, assuming any apps you open the files in don’t copy the files to a local cache on the boot volumes.
    • TBF, it has had PST support for quite a while now. But I still want them to add the ability to drag & drop between accounts / PSTs.
    • LibreOffice closes in on Office, leaves Windows 7/8 behind in 25.8 Beta 1 by David Uzondu The Document Foundation has released LibreOffice 25.8 Beta 1 for public testing on Linux, macOS, and Windows. This is the second pre-release for the 25.8 cycle and the foundation says that the final, stable version of LibreOffice 25.8 is expected to land at the end of August 2025. Starting off with Writer, LibreOffice's Word, the developers have finally addressed some long-standing annoyances, including a new command to easily insert a paragraph break right before a table. This beta also introduces a useful privacy feature in its Auto-Redact tool, letting you strip all images from a document with a single option. To use it, go to Tools and select the Auto-Redact option: The application has improved its ability to handle different languages for punctuation, preventing mix-ups in multilingual documents. Other notable improvements have also been made. A new hyphenation rule lets you choose to prevent a word from splitting at the end of a page, moving the whole line to the next page instead. Microsoft Word has had this feature for years now. The Navigator now displays a handy tooltip with word and character counts for headings and their sub-outlines. Scrolling behavior when selecting text has been improved, making it less erratic. A new command with a keyboard shortcut was added for converting fields into plain text. Calc gets a lot of new functions that bring it closer to its competitors like Excel, including TEXTSPLIT, VSTACK, and WRAPROWS. Impress now properly supports embedded fonts in PPTX files, which should reduce headaches when sharing presentations with PowerPoint users. Alongside these additions, the project is also cleaning house; support for Windows 7, 8, and 8.1 has been completely dropped. There are also smaller UI tweaks across the suite, like allowing a single click to enter rotation mode for objects in Writer and Calc. macOS users get better integration, with proper support for native full screen mode and new window management features from the Sequoia update. In terms of performance, the team has optimized everything from loading huge DOC files and XLSX spreadsheets with tons of conditional formatting to simply switching between sheets in Calc. These improvements should be noticeable, especially when working with complex documents. A new application-wide "Viewer mode" has also been implemented, which opens all files in a read-only state for quick, safe viewing. On a related note, The Document Foundation has joined efforts by the likes of KDE to encourage Windows 10 users to switch to Linux. Also, you might have heard that Denmark, in a bid to lessen its reliance on Microsoft, has decided to make a full switch to LibreOffice, with plans to begin phasing out Office 365 in certain ministries as early as next month. If you're interested in this release, you can read the full release notes and download the binaries for your platform: Windows, macOS (Intel | Apple Silicon), or Linux (DEB | RPM). You can also get the latest stable version from our software stories page.
  • Recent Achievements

    • Explorer
      Case_f went up a rank
      Explorer
    • Conversation Starter
      Jamie Smith earned a badge
      Conversation Starter
    • First Post
      NeoToad777 earned a badge
      First Post
    • Week One Done
      JoeV earned a badge
      Week One Done
    • One Month Later
      VAT Services in UAE earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      545
    2. 2
      ATLien_0
      227
    3. 3
      +FloatingFatMan
      160
    4. 4
      Michael Scrip
      113
    5. 5
      +Edouard
      104
  • Tell a friend

    Love Neowin? Tell a friend!