• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 18:39
CET 00:39
KST 08:39
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
RSL Revival - 2025 Season Finals Preview8RSL Season 3 - Playoffs Preview0RSL Season 3 - RO16 Groups C & D Preview0RSL Season 3 - RO16 Groups A & B Preview2TL.net Map Contest #21: Winners12
Community News
Weekly Cups (Jan 5-11): Clem wins big offline, Trigger upsets4$21,000 Rongyi Cup Season 3 announced (Jan 22-Feb 7)15Weekly Cups (Dec 29-Jan 4): Protoss rolls, 2v2 returns7[BSL21] Non-Korean Championship - Starts Jan 103SC2 All-Star Invitational: Jan 17-1828
StarCraft 2
General
Weekly Cups (Jan 5-11): Clem wins big offline, Trigger upsets SC2 All-Star Invitational: Jan 17-18 When will we find out if there are more tournament SC2 Spotted on the EWC 2026 list? Weekly Cups (Dec 29-Jan 4): Protoss rolls, 2v2 returns
Tourneys
SC2 AI Tournament 2026 Sparkling Tuna Cup - Weekly Open Tournament $21,000 Rongyi Cup Season 3 announced (Jan 22-Feb 7) $25,000 Streamerzone StarCraft Pro Series announced WardiTV Winter Cup
Strategy
Simple Questions Simple Answers
Custom Maps
Map Editor closed ?
External Content
Mutation # 508 Violent Night Mutation # 507 Well Trained Mutation # 506 Warp Zone Mutation # 505 Rise From Ashes
Brood War
General
[ASL21] Potential Map Candidates BGH Auto Balance -> http://bghmmr.eu/ BW General Discussion A cwal.gg Extension - Easily keep track of anyone Potential ASL qualifier breakthroughs?
Tourneys
[Megathread] Daily Proleagues [BSL21] Grand Finals - Sunday 21:00 CET [BSL21] Non-Korean Championship - Starts Jan 10 SLON Grand Finals – Season 2
Strategy
Game Theory for Starcraft Simple Questions, Simple Answers Current Meta [G] How to get started on ladder as a new Z player
Other Games
General Games
Beyond All Reason Nintendo Switch Thread Awesome Games Done Quick 2026! Mechabellum Stormgate/Frost Giant Megathread
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
Vanilla Mini Mafia Mafia Game Mode Feedback/Ideas
Community
General
US Politics Mega-thread Russo-Ukrainian War Thread Things Aren’t Peaceful in Palestine European Politico-economics QA Mega-thread Trading/Investing Thread
Fan Clubs
White-Ra Fan Club
Media & Entertainment
Anime Discussion Thread
Sports
2024 - 2026 Football Thread
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
The Automated Ban List
Blogs
My 2025 Magic: The Gathering…
DARKING
Physical Exercise (HIIT) Bef…
TrAiDoS
Life Update and thoughts.
FuDDx
How do archons sleep?
8882
James Bond movies ranking - pa…
Topin
Customize Sidebar...

Website Feedback

Closed Threads



Active: 2838 users

Little C++ Question

Blogs > EsX_Raptor
Post a Reply
EsX_Raptor
Profile Blog Joined February 2008
United States2802 Posts
November 12 2008 18:00 GMT
#1
Assume I have a file with this content:

a b c d
e f g h i
j k l

and I have a char variable a:

char a;

Now suppose I read each letter and do something with it, such as converting it to a number. The desired output in a new file should be:

1 2 3 4
5 6 7 8 9
10 11 12

Don't pay attention to the numbers, but rather the format of the output (3 lines).

My problem is: I don't know how to read the newline character correctly. I've already tried

File >> a;
if (a == '\n')
Out << '\n';

That would create a new line in the output file. But for some reason it just doesnt work and the output is the following:

1 2 3 4 5 6 7 8 9 10 11 12

No formatting. Anyone has suggestions on how to create that newline?

*
EsX_Raptor
Profile Blog Joined February 2008
United States2802 Posts
November 12 2008 18:09 GMT
#2
Apparently the newline character '\n' cannot be read by the char variable a and thus the "if" isn't executed. -.-
dyodyo
Profile Blog Joined December 2005
Philippines578 Posts
Last Edited: 2008-11-12 18:11:43
November 12 2008 18:10 GMT
#3
edit
+ Show Spoiler +

Try this and see if it works...
Try viewing the file in a hex editor to see if the newline character is correctly inserted in the file (ascii code 10). You can also try viewing the file in wordpad. Note that windows uses the \r\n combination as line endings. So if you just insert \n and open the file in notepad, it wont have multiple lines (but you would see a block character where the \n characters are located.

nice to see you figured it out
TeamLiquid CJ Entusman #26
freelander
Profile Blog Joined December 2004
Hungary4707 Posts
November 12 2008 18:15 GMT
#4
>> operator doesn't read whitespace characters such space, /n , tab I think.
You should use the std function getline(i instream, string s), it reads a whole a line from an instream and writes to string s.
And all is illuminated.
EsX_Raptor
Profile Blog Joined February 2008
United States2802 Posts
November 12 2008 18:21 GMT
#5
But my program is designed to read each input in the file one at a time and perform some operation on them.

And as you said, >> operator doesn't read whitespaces nor '\n' symbol.

if I add, say a letter 'x' at the end of each line and go:

if (a == 'x')
Out << '\n';

it works fine -.-
freelander
Profile Blog Joined December 2004
Hungary4707 Posts
Last Edited: 2008-11-12 18:31:53
November 12 2008 18:29 GMT
#6
On November 13 2008 03:21 EsX_Raptor wrote:
But my program is designed to read each input in the file one at a time and perform some operation on them.

And as you said, >> operator doesn't read whitespaces nor '\n' symbol.

if I add, say a letter 'x' at the end of each line and go:

if (a == 'x')
Out << '\n';

it works fine -.-


ok.. but adding an additional x sounds silly because if you have to work with files like this(for example ini files) you should use the getline.
if you read a line into a string var, you can see what was the last character in that line.
If you know this, you can add a /n after converting the said letter to number.
And all is illuminated.
MasterZilla
Profile Blog Joined November 2003
Sweden234 Posts
November 12 2008 18:30 GMT
#7
You can open the stream with the 'noskipws' flag, which allows you to read whitespace.

As for the line breaking, use Out << endl; in stead.
For Aiur! - If you reach for the stars and miss, you still might end up walking among the clouds.
freelander
Profile Blog Joined December 2004
Hungary4707 Posts
November 12 2008 18:34 GMT
#8
On November 13 2008 03:30 MasterZilla wrote:
You can open the stream with the 'noskipws' flag, which allows you to read whitespace.

As for the line breaking, use Out << endl; in stead.


hey, how should this be done?

ifstream f("fname.txt");

where to put this flag?
And all is illuminated.
Moaf_
Profile Joined September 2006
Austria76 Posts
November 12 2008 18:41 GMT
#9
Google is your friend

Link
No Risk - No Fun
freelander
Profile Blog Joined December 2004
Hungary4707 Posts
November 12 2008 18:45 GMT
#10
On November 13 2008 03:41 Moaf_ wrote:
Google is your friend

Link


thanks (and wikipedia is your girlfriend, i know i know)
And all is illuminated.
Moaf_
Profile Joined September 2006
Austria76 Posts
November 12 2008 19:08 GMT
#11
nah, wikipedia is actually a whore
No Risk - No Fun
Please log in or register to reply.
Live Events Refresh
Next event in 10h 21m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
Nathanias 129
UpATreeSC 126
CosmosSc2 47
StarCraft: Brood War
Shuttle 578
Artosis 541
910 34
NaDa 20
Dota 2
syndereN419
capcasts85
canceldota28
Counter-Strike
Foxcn342
Super Smash Bros
Mew2King94
AZ_Axe57
Other Games
tarik_tv5853
Liquid`RaSZi2455
Grubby1958
FrodaN1212
shahzam560
C9.Mang0218
B2W.Neo211
KnowMe93
Fnx 74
Maynarde69
ZombieGrub39
ViBE11
Organizations
Other Games
gamesdonequick2192
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 19 non-featured ]
StarCraft 2
• Hupsaiya 70
• davetesta37
• HeavenSC 25
• sooper7s
• Migwel
• AfreecaTV YouTube
• LaughNgamezSOOP
• intothetv
• IndyKCrew
• Kozan
StarCraft: Brood War
• Azhi_Dahaki40
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• masondota21391
League of Legends
• Doublelift4324
• Scarra1186
Other Games
• imaqtpie2397
• Shiphtur293
Upcoming Events
The PondCast
10h 21m
OSC
12h 21m
Jumy vs sebesdes
Nicoract vs GgMaChine
ReBellioN vs MaNa
Lemon vs TriGGeR
Gerald vs Cure
Creator vs SHIN
OSC
1d 12h
All Star Teams
2 days
INnoVation vs soO
Serral vs herO
Cure vs Solar
sOs vs Scarlett
Classic vs Clem
Reynor vs Maru
uThermal 2v2 Circuit
2 days
All Star Teams
3 days
MMA vs DongRaeGu
Rogue vs Oliveira
Sparkling Tuna Cup
3 days
OSC
3 days
Replay Cast
4 days
Wardi Open
4 days
[ Show More ]
Monday Night Weeklies
4 days
The PondCast
6 days
Liquipedia Results

Completed

Proleague 2026-01-13
Big Gabe Cup #3
NA Kuram Kup

Ongoing

C-Race Season 1
IPSL Winter 2025-26
BSL 21 Non-Korean Championship
CSL 2025 WINTER (S19)
OSC Championship Season 13
Underdog Cup #3
BLAST Bounty Winter Qual
eXTREMESLAND 2025
SL Budapest Major 2025
ESL Impact League Season 8
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025

Upcoming

Escore Tournament S1: W4
Acropolis #4
IPSL Spring 2026
Bellum Gens Elite Stara Zagora 2026
HSC XXVIII
Rongyi Cup S3
SC2 All-Star Inv. 2025
Nations Cup 2026
BLAST Open Spring 2026
ESL Pro League Season 23
ESL Pro League Season 23
PGL Cluj-Napoca 2026
IEM Kraków 2026
BLAST Bounty Winter 2026
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2026 TLnet. All Rights Reserved.