Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1975
  • Last Modified:

C++ Reading a .txt file

In C++, I am attempting to read several fields for a text file into my program. I was told I could use the code below to fetch the c-sting variable housed in the middle of the .txt file. I am having no luck getting the cin.get() to work in the middle of the code.

suggested code:
char name[21];
cin.get( name, 21, '\n' );
====================================
 The code that I am using is my .txt file is name "masterFile".:
masterFile >> ID >> name >>payRate >> dependents >> eType;

A sample of the text file is below:

 5 Christine Kim       30.00 2 1
15 Ray Allrich         10.25 0 0
16 Adrian Bailey      12.50 0 0
17 Juan Gonzales     30.00 1 1
18 Morris Kramer       8.95 0 0
22 Cindy Burke         15.00 1 0
24 Esther Bianco       10.25 0 0
25 Jim Moore           27.50 3 1
32 Paula Cameron       14.50 0 0
36 Melvin Ducan        10.25 0 0
37 Nina Kamran         30.00 1 1
38 Julie Brown         35.00 0 1
40 Imelda Buentello    14.50 0 0
42 J. P. Morgan        12.50 0 0
43 Maria Diaz          15.00 0 0





































0
luling1972
Asked:
luling1972
  • 4
  • 3
1 Solution
 
Jaime OlivaresCommented:
your main problem is that name has white spaces inside it, so the >> operator won't work correctly.
Fortunately, you data is fixed width, so you can use:

char line[100];

while(!masterFile.getline(buffer, 100).eof())
{

}

then you can extract string portions at specific positions to obtain the fields.

Fixed width data:
 5 Christine Kim       30.00 2 1
15 Ray Allrich         10.25 0 0
16 Adrian Bailey       12.50 0 0
17 Juan Gonzales       30.00 1 1
18 Morris Kramer        8.95 0 0
22 Cindy Burke         15.00 1 0
24 Esther Bianco       10.25 0 0
25 Jim Moore           27.50 3 1

Open in new window

0
 
luling1972Author Commented:
Could you please add more code. I am having a hard time understanding you concept.
0
 
Jaime OlivaresCommented:
are you able to show the lines of the file?
0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
luling1972Author Commented:
I am having trouble extracting the name field in the second field of the file. Could you provide an example?
0
 
Jaime OlivaresCommented:
OK, better you start putting the line into a STL string:

string line;
string id, name, payRate, dependents, eType;
while(!masterFile.getline(buffer, 100).eof())
{
       line = string(buffer);  // convert to STL string
       id = line.substr(0, 2);
       name = line.substr(3, 20);
       // etcetera
}
0
 
luling1972Author Commented:
That looks good!  I have solved my problem. Thanks for you time and patience!
0
 
luling1972Author Commented:
good work. great job!!!
0

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now