Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C++ Reading a .txt file

Posted on 2007-11-25
7
Medium Priority
?
1,963 Views
Last Modified: 2010-04-21
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
Comment
Question by:luling1972
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20348370
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
 

Author Comment

by:luling1972
ID: 20354905
Could you please add more code. I am having a hard time understanding you concept.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 20354908
are you able to show the lines of the file?
0
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

Author Comment

by:luling1972
ID: 20355062
I am having trouble extracting the name field in the second field of the file. Could you provide an example?
0
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 20355297
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
 

Author Comment

by:luling1972
ID: 20355349
That looks good!  I have solved my problem. Thanks for you time and patience!
0
 

Author Closing Comment

by:luling1972
ID: 31410928
good work. great job!!!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question