• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 09:27
CEST 15:27
KST 22:27
  • 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
[ASL20] Ro24 Preview Pt1: Runway112v2 & SC: Evo Complete: Weekend Double Feature3Team Liquid Map Contest #21 - Presented by Monster Energy9uThermal's 2v2 Tour: $15,000 Main Event18Serral wins EWC 202549
Community News
Weekly Cups (Aug 11-17): MaxPax triples again!10Weekly Cups (Aug 4-10): MaxPax wins a triple6SC2's Safe House 2 - October 18 & 195Weekly Cups (Jul 28-Aug 3): herO doubles up6LiuLi Cup - August 2025 Tournaments7
StarCraft 2
General
RSL Revival patreon money discussion thread Is it ok to advertise SC EVO Mod streaming here? Maestros of the Game 2v2 & SC: Evo Complete: Weekend Double Feature Playing 1v1 for Cash? (Read before comment)
Tourneys
Master Swan Open (Global Bronze-Master 2) $5,100+ SEL Season 2 Championship (SC: Evo) Sparkling Tuna Cup - Weekly Open Tournament RSL: Revival, a new crowdfunded tournament series LiuLi Cup - August 2025 Tournaments
Strategy
Custom Maps
External Content
Mutation # 487 Think Fast Mutation # 486 Watch the Skies Mutation # 485 Death from Below Mutation # 484 Magnetic Pull
Brood War
General
BW General Discussion Flash Announces (and Retracts) Hiatus From ASL ASL 20 HYPE VIDEO! New season has just come in ladder [ASL20] Ro24 Preview Pt1: Runway
Tourneys
[ASL20] Ro24 Group C [ASL20] Ro24 Group B [Megathread] Daily Proleagues [ASL20] Ro24 Group A
Strategy
Simple Questions, Simple Answers Fighting Spirit mining rates [G] Mineral Boosting Muta micro map competition
Other Games
General Games
Beyond All Reason Stormgate/Frost Giant Megathread Nintendo Switch Thread Total Annihilation Server - TAForever [MMORPG] Tree of Savior (Successor of Ragnarok)
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
Heroes of StarCraft mini-set
TL Mafia
TL Mafia Community Thread Vanilla Mini Mafia
Community
General
Russo-Ukrainian War Thread US Politics Mega-thread Things Aren’t Peaceful in Palestine European Politico-economics QA Mega-thread The Games Industry And ATVI
Fan Clubs
INnoVation Fan Club SKT1 Classic Fan Club!
Media & Entertainment
Anime Discussion Thread Movie Discussion! [Manga] One Piece [\m/] Heavy Metal Thread
Sports
2024 - 2026 Football Thread TeamLiquid Health and Fitness Initiative For 2023 Formula 1 Discussion
World Cup 2022
Tech Support
Gtx660 graphics card replacement Installation of Windows 10 suck at "just a moment" Computer Build, Upgrade & Buying Resource Thread
TL Community
TeamLiquid Team Shirt On Sale The Automated Ban List
Blogs
The Biochemical Cost of Gami…
TrAiDoS
[Girl blog} My fema…
artosisisthebest
Sharpening the Filtration…
frozenclaw
ASL S20 English Commentary…
namkraft
StarCraft improvement
iopq
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1781 users

Little C++ Question

Blogs > EsX_Raptor
Post a Reply
EsX_Raptor
Profile Blog Joined February 2008
United States2801 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 States2801 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 States2801 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
WardiTV Summer Champion…
11:00
Group Stage 2 - Group B
Clem vs goblin
ByuN vs SHIN
WardiTV910
Liquipedia
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
Harstem 336
Rex 154
ProTech39
trigger 12
EnDerr 9
StarCraft: Brood War
Britney 50721
Calm 12025
Bisu 3385
Jaedong 1116
EffOrt 767
firebathero 747
BeSt 486
ggaemo 328
Stork 311
ZerO 254
[ Show more ]
Light 239
Last 214
Soulkey 184
Hyun 114
Barracks 111
Snow 111
Mind 102
hero 96
Rush 80
Nal_rA 60
TY 59
Movie 56
Backho 26
Icarus 25
Sacsri 22
sorry 20
scan(afreeca) 14
JulyZerg 12
JYJ12
Terrorterran 8
IntoTheRainbow 7
ivOry 4
actioN 0
Dota 2
Gorgc7464
qojqva2096
XcaliburYe264
Fuzer 205
League of Legends
Dendi975
Counter-Strike
hiko590
edward57
Super Smash Bros
Mew2King79
Other Games
singsing2000
B2W.Neo1710
DeMusliM474
crisheroes458
XaKoH 155
ToD86
ArmadaUGS67
QueenE37
Trikslyr30
ZerO(Twitch)2
Organizations
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 16 non-featured ]
StarCraft 2
• davetesta14
• intothetv
• AfreecaTV YouTube
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• HerbMon 28
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Dota 2
• WagamamaTV526
• C_a_k_e 283
League of Legends
• Nemesis3358
• Jankos1114
Upcoming Events
Online Event
10h 33m
The PondCast
20h 33m
WardiTV Summer Champion…
21h 33m
Zoun vs Bunny
herO vs Solar
Replay Cast
1d 10h
LiuLi Cup
1d 21h
BSL Team Wars
2 days
Team Hawk vs Team Dewalt
Korean StarCraft League
2 days
CranKy Ducklings
2 days
SC Evo League
2 days
WardiTV Summer Champion…
2 days
Classic vs Percival
Spirit vs NightMare
[ Show More ]
CSO Cup
3 days
[BSL 2025] Weekly
3 days
Sparkling Tuna Cup
3 days
SC Evo League
3 days
BSL Team Wars
4 days
Team Bonyth vs Team Sziky
Afreeca Starleague
4 days
Queen vs HyuN
EffOrt vs Calm
Wardi Open
4 days
Replay Cast
5 days
Afreeca Starleague
5 days
Rush vs TBD
Jaedong vs Mong
Afreeca Starleague
6 days
herO vs TBD
Royal vs Barracks
Liquipedia Results

Completed

Jiahua Invitational
uThermal 2v2 Main Event
HCC Europe

Ongoing

Copa Latinoamericana 4
BSL 20 Team Wars
KCM Race Survival 2025 Season 3
BSL 21 Qualifiers
ASL Season 20
CSL Season 18: Qualifier 1
SEL Season 2 Championship
WardiTV Summer 2025
Esports World Cup 2025
BLAST Bounty Fall 2025
BLAST Bounty Fall Qual
IEM Cologne 2025
FISSURE Playground #1
BLAST.tv Austin Major 2025

Upcoming

CSLAN 3
CSL 2025 AUTUMN (S18)
LASL Season 20
BSL Season 21
BSL 21 Team A
Chzzk MurlocKing SC1 vs SC2 Cup #2
RSL Revival: Season 2
Maestros of the Game
EC S1
PGL Masters Bucharest 2025
MESA Nomadic Masters Fall
Thunderpick World Champ.
CS Asia Championships 2025
Roobet Cup 2025
ESL Pro League S22
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 2025
BLAST Open Fall Qual
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 © 2025 TLnet. All Rights Reserved.