• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 16:49
CEST 22:49
KST 05:49
  • 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
TL.net Map Contest #21: Voting2[ASL20] Ro4 Preview: Descent2Team TLMC #5: Winners Announced!3[ASL20] Ro8 Preview Pt2: Holding On9Maestros of the Game: Live Finals Preview (RO4)5
Community News
5.0.15 Patch Balance Hotfix (2025-10-8)61Weekly Cups (Sept 29-Oct 5): MaxPax triples up3PartinG joins SteamerZone, returns to SC2 competition295.0.15 Balance Patch Notes (Live version)119$2,500 WardiTV TL Map Contest Tournament 154
StarCraft 2
General
PartinG joins SteamerZone, returns to SC2 competition TL.net Map Contest #21: Voting 5.0.15 Patch Balance Hotfix (2025-10-8) TL.net Map Contest #21 - Finalists Geoff 'iNcontroL' Robinson has passed away
Tourneys
SC2's Safe House 2 - October 18 & 19 Sparkling Tuna Cup - Weekly Open Tournament RSL Offline Finals Dates + Ticket Sales! SC4ALL $6,000 Open LAN in Philadelphia $2,500 WardiTV TL Map Contest Tournament 15
Strategy
Custom Maps
External Content
Mutation # 495 Rest In Peace Mutation # 494 Unstable Environment Mutation # 493 Quick Killers Mutation # 492 Get Out More
Brood War
General
Whose hotkey signature is this? [ASL20] Ro4 Preview: Descent Any rep analyzer that shows resources situation? BW General Discussion BGH Auto Balance -> http://bghmmr.eu/
Tourneys
[ASL20] Semifinal A [Megathread] Daily Proleagues [ASL20] Ro8 Day 4 Small VOD Thread 2.0
Strategy
Current Meta BW - ajfirecracker Strategy & Training Siegecraft - a new perspective TvZ Theorycraft - Improving on State of the Art
Other Games
General Games
Nintendo Switch Thread Stormgate/Frost Giant Megathread ZeroSpace Megathread Dawn of War IV Path of Exile
Dota 2
Official 'what is Dota anymore' discussion LiquidDota to reintegrate into TL.net
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
SPIRED by.ASL Mafia {211640} TL Mafia Community Thread
Community
General
US Politics Mega-thread The Games Industry And ATVI Stop the Construction YouTube Thread Things Aren’t Peaceful in Palestine
Fan Clubs
The herO Fan Club! The Happy Fan Club!
Media & Entertainment
Anime Discussion Thread [Manga] One Piece Movie Discussion!
Sports
2024 - 2026 Football Thread Formula 1 Discussion MLB/Baseball 2023 NBA General Discussion TeamLiquid Health and Fitness Initiative For 2023
World Cup 2022
Tech Support
SC2 Client Relocalization [Change SC2 Language] Linksys AE2500 USB WIFI keeps disconnecting Computer Build, Upgrade & Buying Resource Thread
TL Community
The Automated Ban List Recent Gifted Posts
Blogs
Inbreeding: Why Do We Do It…
Peanutsc
From Tilt to Ragequit:The Ps…
TrAiDoS
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1390 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
Next event in 13h 12m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
CosmosSc2 208
IndyStarCraft 158
PiGStarcraft124
Nathanias 112
Livibee 81
SpeCial 18
StarCraft: Brood War
Britney 20890
yabsab 21
Counter-Strike
ScreaM1448
Stewie2K423
Heroes of the Storm
Liquid`Hasu496
Khaldor217
Other Games
FrodaN3315
Grubby2223
fl0m1023
B2W.Neo712
RotterdaM358
ceh9226
ToD200
KnowMe197
Skadoodle190
Organizations
Other Games
gamesdonequick1071
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 24 non-featured ]
StarCraft 2
• 3DClanTV 64
• StrangeGG 50
• Hupsaiya 47
• HeavenSC 26
• musti20045 24
• davetesta12
• Adnapsc2 12
• IndyKCrew
• Kozan
• sooper7s
• intothetv
• Migwel
• AfreecaTV YouTube
• LaughNgamezSOOP
StarCraft: Brood War
• Airneanach76
• FirePhoenix2
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• WagamamaTV863
• Ler71
League of Legends
• Doublelift2675
Other Games
• imaqtpie1295
• Shiphtur334
Upcoming Events
Afreeca Starleague
13h 12m
Barracks vs Snow
Wardi Open
14h 12m
Monday Night Weeklies
19h 12m
Replay Cast
1d 3h
Afreeca Starleague
1d 13h
Soma vs Bisu
OSC
1d 17h
OSC
1d 21h
PiGosaur Monday
2 days
The PondCast
3 days
OSC
3 days
[ Show More ]
Wardi Open
4 days
CranKy Ducklings
5 days
Safe House 2
5 days
Sparkling Tuna Cup
6 days
Safe House 2
6 days
Liquipedia Results

Completed

Acropolis #4 - TS2
Maestros of the Game
HCC Europe

Ongoing

BSL 21 Points
ASL Season 20
CSL 2025 AUTUMN (S18)
C-Race Season 1
IPSL Winter 2025-26
EC S1
ESL Pro League S22
StarSeries Fall 2025
FISSURE Playground #2
BLAST Open Fall 2025
BLAST Open Fall Qual
Esports World Cup 2025
BLAST Bounty Fall 2025
BLAST Bounty Fall Qual
IEM Cologne 2025

Upcoming

SC4ALL: Brood War
BSL Season 21
BSL 21 Team A
RSL Offline Finals
RSL Revival: Season 3
Stellar Fest
SC4ALL: StarCraft II
eXTREMESLAND 2025
ESL Impact League Season 8
SL Budapest Major 2025
BLAST Rivals Fall 2025
IEM Chengdu 2025
PGL Masters Bucharest 2025
Thunderpick World Champ.
CS Asia Championships 2025
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.