Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › [C++] Reading in from a file
New Posts  All Forums:Forum Nav:

[C++] Reading in from a file

post #1 of 11
Thread Starter 
Ok, so my input file goes something like this:

Code:
LastName  FirstName  ID
LastName  FirstName  ID
LastName  FirstName  ID
 
ID  Address
ID  Address
ID  Address
I'm used to using "while (!InputFile.eof())" to stop reading in from the file.
How do I get the program to stop reading at the blank line?
Megadoomer
(14 items)
 
Family Computer
(13 items)
 
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T @ 4.0Ghz ASUS M4A89GTD PRO Sparkle GTS 450 2x4GB G-Skill Sniper 
Hard DriveCoolingOSMonitor
Samsung F1 1TB CM Hyper 212+ Windows 7 Professional x64 Samsung T220 
KeyboardPowerCaseMouse
Logitech MX3000 Laser CM 1000M HAF 922 Logitech VX Revolution 
CPUMotherboardGraphicsRAM
Q6600 Asus PN5-D 750i Evga GTS 250 2x2GB Crucial Ballistix 
Hard DriveOptical DriveOSMonitor
750GB Hitachi Samsung Super Writemaster Windows 7 Professional x64 19" Dell 
PowerCase
Corsair 450VX Antec 900 
  hide details  
Reply
Megadoomer
(14 items)
 
Family Computer
(13 items)
 
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T @ 4.0Ghz ASUS M4A89GTD PRO Sparkle GTS 450 2x4GB G-Skill Sniper 
Hard DriveCoolingOSMonitor
Samsung F1 1TB CM Hyper 212+ Windows 7 Professional x64 Samsung T220 
KeyboardPowerCaseMouse
Logitech MX3000 Laser CM 1000M HAF 922 Logitech VX Revolution 
CPUMotherboardGraphicsRAM
Q6600 Asus PN5-D 750i Evga GTS 250 2x2GB Crucial Ballistix 
Hard DriveOptical DriveOSMonitor
750GB Hitachi Samsung Super Writemaster Windows 7 Professional x64 19" Dell 
PowerCase
Corsair 450VX Antec 900 
  hide details  
Reply
post #2 of 11
I don't know [much] C++, but logically, won't it be easy enough to loop through the file, while !xxx.empty(), where xxx is a variable pertaining to the line of data being read?

I might be way off, but trying to help
post #3 of 11
Code:
if(line.size() == 0)
{
      // Stop reading here
}
That might work, just tell it to stop reading where I put the comment.
And change line to whatever the reader is called.
    
CPUMotherboardGraphicsGraphics
FX-8350 Asus Crosshair V MSI GTX460 Hawk 1gb MSI GTX460 Hawk 1gb 
RAMHard DriveHard DriveHard Drive
Kingston HyperX 8gb (2x4gb) Crucial M4 64GB Samsung F3 1TB Western Digital 320GB 
CoolingOSMonitorMonitor
Custom WC Windows 7 Ultimate X64 Dell E2311H Dell E2311H 
MonitorKeyboardPowerCase
LH 23EN43 Ducky Year of the Dragon 2012 SilverStone Strider 1000W-P Corsair 800D 
Audio
Asus Xonar Essence STX 
  hide details  
Reply
    
CPUMotherboardGraphicsGraphics
FX-8350 Asus Crosshair V MSI GTX460 Hawk 1gb MSI GTX460 Hawk 1gb 
RAMHard DriveHard DriveHard Drive
Kingston HyperX 8gb (2x4gb) Crucial M4 64GB Samsung F3 1TB Western Digital 320GB 
CoolingOSMonitorMonitor
Custom WC Windows 7 Ultimate X64 Dell E2311H Dell E2311H 
MonitorKeyboardPowerCase
LH 23EN43 Ducky Year of the Dragon 2012 SilverStone Strider 1000W-P Corsair 800D 
Audio
Asus Xonar Essence STX 
  hide details  
Reply
post #4 of 11
You could try using a pointer for your buffer and extract the characters with much greater control.

From cplusplus:
Code:
// show file content - sgetc () example
#include <iostream>
#include <fstream>
using namespace std;

int main () {

  char ch;
  streambuf * pbuf;
  ifstream istr ("test.txt");

  pbuf = istr.rdbuf();

  while (pbuf->sgetc()!=EOF)
  {
     ch = pbuf->sbumpc();
     cout << ch;
  }

  istr.close();

  return 0;
}
sgetc() is a function used by streambuf objects. in this example they name the pointer pbuf and point it to the stream buffer object, in this case, an ifstream object named istr. you could simply modify this code with your own adjustments and look for white spaces and/or a newline instead of the EOF bit. the great thing about using pointers in this scenario is that you have better control because of the ability to increment through the stream and examine the individual bytes in the stream.
Leviathan
(19 items)
 
  
CPUMotherboardGraphicsRAM
i7 930 @ 4.2 GHz - 200*21 @1.38V Asus Rampage II Extreme 2x 7970's Crossfired -- 1000 | 1425 (Sapphire r... 12GB Corsair XMS3 1600MHz | 7-8-7-20 1.65v 1T 
Hard DriveCoolingCoolingCooling
2x120 GB OCZ Agility 3 SSD RAID 0 + 1.5 TB Seag... EK Supreme HF copper block Feser X360 radiator Danger Den Laing DDC 12V / 18W pump 
CoolingCoolingOSOS
iandh multi-option reservoir 1/2" ID 3/4" OD tubing, G 1/2" compression fitt... Windows 7 x64 Professional Arch x64 with Awesome wm 
MonitorMonitorKeyboardPower
Asus VG248QE 1080p@144Hz Sceptre X24W6 1080p@60Hz Razer Tarantula 750W Xigmatek w/ 4 +12V DC Rails ~60A 
CaseMouseMouse Pad
Corsair Obsidian 800D Razer Mamba @ 600 DPI QcK+ Heavy 
  hide details  
Reply
Leviathan
(19 items)
 
  
CPUMotherboardGraphicsRAM
i7 930 @ 4.2 GHz - 200*21 @1.38V Asus Rampage II Extreme 2x 7970's Crossfired -- 1000 | 1425 (Sapphire r... 12GB Corsair XMS3 1600MHz | 7-8-7-20 1.65v 1T 
Hard DriveCoolingCoolingCooling
2x120 GB OCZ Agility 3 SSD RAID 0 + 1.5 TB Seag... EK Supreme HF copper block Feser X360 radiator Danger Den Laing DDC 12V / 18W pump 
CoolingCoolingOSOS
iandh multi-option reservoir 1/2" ID 3/4" OD tubing, G 1/2" compression fitt... Windows 7 x64 Professional Arch x64 with Awesome wm 
MonitorMonitorKeyboardPower
Asus VG248QE 1080p@144Hz Sceptre X24W6 1080p@60Hz Razer Tarantula 750W Xigmatek w/ 4 +12V DC Rails ~60A 
CaseMouseMouse Pad
Corsair Obsidian 800D Razer Mamba @ 600 DPI QcK+ Heavy 
  hide details  
Reply
post #5 of 11
Thread Starter 
Quote:
Originally Posted by omega17 View Post
I don't know [much] C++, but logically, won't it be easy enough to loop through the file, while !xxx.empty(), where xxx is a variable pertaining to the line of data being read?

I might be way off, but trying to help
But doesn't C++ just ignore the blank spaces? So it'll go right through the blank line, and continue reading in when the next batch of info comes up?

Quote:
Originally Posted by FiX View Post
Code:
if(line.size() == 0)
{
      // Stop reading here
}
That might work, just tell it to stop reading where I put the comment.
And change line to whatever the reader is called.
Got an error "has no member 'size'".

Quote:
Originally Posted by DuDeInThEmOoN42 View Post
You could try using a pointer for your buffer and extract the characters with much greater control.

From cplusplus:
Code:
// show file content - sgetc () example
#include <iostream>
#include <fstream>
using namespace std;
 
int main () {
 
  char ch;
  streambuf * pbuf;
  ifstream istr ("test.txt");
 
  pbuf = istr.rdbuf();
 
  while (pbuf->sgetc()!=EOF)
  {
     ch = pbuf->sbumpc();
     cout << ch;
  }
 
  istr.close();
 
  return 0;
}
sgetc() is a function used by streambuf objects. in this example they name the pointer pbuf and point it to the stream buffer object, in this case, an ifstream object named istr. you could simply modify this code with your own adjustments and look for white spaces and/or a newline instead of the EOF bit. the great thing about using pointers in this scenario is that you have better control because of the ability to increment through the stream and examine the individual bytes in the stream.
Seems a tad complex right now... I'm gonna see if I can get it variable controlled... if not, I'll give this a go.
Megadoomer
(14 items)
 
Family Computer
(13 items)
 
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T @ 4.0Ghz ASUS M4A89GTD PRO Sparkle GTS 450 2x4GB G-Skill Sniper 
Hard DriveCoolingOSMonitor
Samsung F1 1TB CM Hyper 212+ Windows 7 Professional x64 Samsung T220 
KeyboardPowerCaseMouse
Logitech MX3000 Laser CM 1000M HAF 922 Logitech VX Revolution 
CPUMotherboardGraphicsRAM
Q6600 Asus PN5-D 750i Evga GTS 250 2x2GB Crucial Ballistix 
Hard DriveOptical DriveOSMonitor
750GB Hitachi Samsung Super Writemaster Windows 7 Professional x64 19" Dell 
PowerCase
Corsair 450VX Antec 900 
  hide details  
Reply
Megadoomer
(14 items)
 
Family Computer
(13 items)
 
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T @ 4.0Ghz ASUS M4A89GTD PRO Sparkle GTS 450 2x4GB G-Skill Sniper 
Hard DriveCoolingOSMonitor
Samsung F1 1TB CM Hyper 212+ Windows 7 Professional x64 Samsung T220 
KeyboardPowerCaseMouse
Logitech MX3000 Laser CM 1000M HAF 922 Logitech VX Revolution 
CPUMotherboardGraphicsRAM
Q6600 Asus PN5-D 750i Evga GTS 250 2x2GB Crucial Ballistix 
Hard DriveOptical DriveOSMonitor
750GB Hitachi Samsung Super Writemaster Windows 7 Professional x64 19" Dell 
PowerCase
Corsair 450VX Antec 900 
  hide details  
Reply
post #6 of 11
What data type is line? If a string, wouldn't it be line.length() ? Wow, 10 years is too long to be away from a language! I'm all spoiled by php now. google + php = fast solution + works anywhere.
Fridge
(18 items)
 
e-peen
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7-4930k SABERTOOTH X79 NVIDIA Quadro K2000 Corsair 4 x 8GB 
Hard DriveHard DriveOptical DriveCooling
OCZ Revodrive 350 PCIe  Seagate Barracuda 7200.12 1TB 7200 rpm ST31000... Acer HL-DT-ST DVDRAM GH70N  Noctua NH-D14 SE2011 
OSMonitorKeyboardPower
windows 7 professional 64 bit ASUS PB278Q 27-Inch WQHD PLS Monitor razer black widow ultimate Raidmax 630w 
CaseMouseOtherOther
TT Commander Snow Logitech M-U0007 (M500) NextEngine Laser Scanner Mobile Computer Desk 
OtherOther
3D Connexion Space Navigator Huion H58L Tablet 
CPUMotherboardGraphicsRAM
xeon 3440 Maximus III Gene GTS250 G.Skill 2GB F3-10666CL8-2GBHK 
Hard DriveCoolingOSMonitor
SAMSUNG SpinPoint P Series SP2004C 200GB 7200 RPM Zalman windows 7 64 bit Gateway FHX3200B 
MonitorKeyboardPowerCase
Acer AL2216W Razer black widow ultimate CM 500w Coolermaster USP100 red 
Mouse
Logitech MX518 
  hide details  
Reply
Fridge
(18 items)
 
e-peen
(15 items)
 
 
CPUMotherboardGraphicsRAM
Intel i7-4930k SABERTOOTH X79 NVIDIA Quadro K2000 Corsair 4 x 8GB 
Hard DriveHard DriveOptical DriveCooling
OCZ Revodrive 350 PCIe  Seagate Barracuda 7200.12 1TB 7200 rpm ST31000... Acer HL-DT-ST DVDRAM GH70N  Noctua NH-D14 SE2011 
OSMonitorKeyboardPower
windows 7 professional 64 bit ASUS PB278Q 27-Inch WQHD PLS Monitor razer black widow ultimate Raidmax 630w 
CaseMouseOtherOther
TT Commander Snow Logitech M-U0007 (M500) NextEngine Laser Scanner Mobile Computer Desk 
OtherOther
3D Connexion Space Navigator Huion H58L Tablet 
CPUMotherboardGraphicsRAM
xeon 3440 Maximus III Gene GTS250 G.Skill 2GB F3-10666CL8-2GBHK 
Hard DriveCoolingOSMonitor
SAMSUNG SpinPoint P Series SP2004C 200GB 7200 RPM Zalman windows 7 64 bit Gateway FHX3200B 
MonitorKeyboardPowerCase
Acer AL2216W Razer black widow ultimate CM 500w Coolermaster USP100 red 
Mouse
Logitech MX518 
  hide details  
Reply
post #7 of 11
Can you just post your code? I will probably be able to fix your problem, if not I'm sure someone else will be able too.
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i5 750 Gigabyte GA-H55M-S2V ASUS 9600 GSO 384 2x2gb G.Skill DDR3 
Hard DriveOptical DriveOS
2x250GB DVD Windows 7 64 bit 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
Core i5 750 Gigabyte GA-H55M-S2V ASUS 9600 GSO 384 2x2gb G.Skill DDR3 
Hard DriveOptical DriveOS
2x250GB DVD Windows 7 64 bit 
  hide details  
Reply
post #8 of 11
You can use istream.getline to read line by line and check each read line to see if it's the blank one. If the line is actually blank, use string.empty to catch it. If there may be whitespace on that line you'll have to do something a bit more complex
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
post #9 of 11
Quote:
Originally Posted by rabidgnome229 View Post
You can use istream.getline to read line by line and check each read line to see if it's the blank one. If the line is actually blank, use string.empty to catch it. If there may be whitespace on that line you'll have to do something a bit more complex
why not just use string.size to find out if its empty or not?
Pepe Silvia
(13 items)
 
  
CPUMotherboardGraphicsRAM
255 @ 4 Ghz MSI 870-C45 GT 520 @ 1Ghz Crucial Rendition 4GB DDR3 
Hard DriveOSMonitorPower
Hitachi 1TB Win 7 19" 420W 
  hide details  
Reply
Pepe Silvia
(13 items)
 
  
CPUMotherboardGraphicsRAM
255 @ 4 Ghz MSI 870-C45 GT 520 @ 1Ghz Crucial Rendition 4GB DDR3 
Hard DriveOSMonitorPower
Hitachi 1TB Win 7 19" 420W 
  hide details  
Reply
post #10 of 11
No reason not to. There's a convenience method for checking for an empty string, though, so I'd use it
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › [C++] Reading in from a file