for C++ Create an ASCII file with the following data, or use the shipped.dat fil
ID: 3682661 • Letter: F
Question
for C++ Create an ASCII file with the following data, or use the shipped.dat file available
on this book’s Web site. The headings aren’t part of the file but indicate what the data represents.
Shipped
Date
Tracking
Number
Part
Number
First
Name
Last
Name
Company
04/12/11 D50625 74444 James Lehoff Rotech
04/12/11 D60752 75255 Janet Lezar Rotech
04/12/11 D40295 74477 Bill McHenry Rotech
04/12/11 D23745 74470 Diane Kaiser Rotech
04/12/11 D50892 75155 Helen Richardson NapTime
The format of each line in the file is identical, with fixed-length fields defined as follows:
Field
Position
Field Name Starting
Col. No.
Ending
Col. No.
Field Length
1 Shipped Date 1 8 8
2 Tracking Number 12 17 6
3 Part Number 22 26 5
4 First Name 31 35 5
5 Last Name 39 48 10
6 Company 51 64 14
Using this data file, write a C++ program that reads the file and produces a report listing the
shipped date, part number, first name, last name, and company name.
Explanation / Answer
I have ececuted it under codeblocks IDE:
#include <iostream>
#include<fstream>
#include<sstream>
using namespace std;
std::string line;
int main()
{
string arr[20];
int i,j,k;
ifstream myfile("C:/Users/Kanth/Desktop/babu/Placement/DataFromFile/data2.txt");
while(getline(myfile, line))
{
std::stringstream linestream(line);//to get the entire line
std::string value;
i=0;//for every line it is initiated to 0.
//for every line I am reusing the same array by printing it immediately
while(getline(linestream,value,' '))//to split line where ' ' is find
{
arr[i]=value;
i++; //array index
}
for(k=0;k<i;k++){
if(k==1) //skipping the 2nd column
;
else
cout << arr[k] << " ";
}
cout << endl;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.