Delimited CSV file headache

So i was trying to make a program to let me read from 2 separate files, one being .csv and one being .dat and then taking information from both and combining them into an xml outfile. Here is the issue, the first half of my code works fine from what i can see, but the second half where i have to read from a file formatted like this:

Bright, Rich, PC12K2RT, 10/21/2011

and No matter what i change my values to it keeps giving me the same results when the program executes and looks like this in my output:

PC12K2RTUsed Car 02350.00
PC12K2RT
2
Used Car
02350.00

Bright, Rich, PC12K2RT, 10/21/2011
Bright
Bright, Rich, PC12K2RT, 10/21/2011
Bright
Bright, Rich, PC12K2RT, 10/21/2011
Bright
Bright, Rich, PC12K2RT, 10/21/2011
Bright
Bright, Rich, PC12K2RT, 10/21/2011

the first grouped section of the above input is working correctly. The second part is not chopping out each section and storing the strings separately


My intentions are to first take everything from the file into a variable, then save everything from Start to the first comma minus the comma to a second variable, and so on so forth till EOF where each string or value between commas is saved as a separate variable. Not looking to have the code just given to me but if you can show an example and explain why you did what you did i would greatly appreciate it. Been trying to mess with this on and off for almost 3 weeks now and figured someones outside opinion could be beneficial:

notes: the way i have it set up currently displays in the output prompt what will be written to the file so i know if its working or not without having to continually open the output file (the xml section is incomplete because i do not need help with that section i'm just waiting until i figure out the delimited csv portion)

my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
 
using namespace std;
 
void pause()
{   
	cout << "\n Press enter to continue..."; //pause
	char junk;
	cin.ignore();
	cin.get(junk);
}
 

int main()
{
	// declare constants
 

	// declare variables
	ifstream myInfile1;
	ifstream myInfile2;
	ofstream myOutfile;
	
	string Items, partNumber, partColor, partDescript, price;
	
	string Customer, lastName, firstName, date; 
 

	// get input
	
	//myInfile1.open("c:\\c++\\Customer.csv"); //openfiles  MAKE SURE FILE NAMES AND DIRECTORIES MATCH!!!
	//myInfile2.open("c:\\c++\\Items.dat");
	//myOutfile.open("c:\\c++\\Transaction.xml"); 
 
 
	myInfile1.open("c:\\c++\\Items.dat");
	getline(myInfile1, Items);
	cout << "\n\n" << Items;
 
	partNumber = Items.substr (0,8);
	cout << "\n\n" << partNumber;
 
	partColor = Items.substr (5,1);
	cout << "\n\n" << partColor;
 
	partDescript = Items.substr (8,14);
	cout << "\n\n" << partDescript;
 
	price = Items.substr (23,8);
	cout << "\n\n" << price <<endl;
 
	myInfile1.close();
 
	
	myInfile2.open("c:\\c++\\Customer.csv");
	getline(myInfile2, Customer);
	cout << "\n\n" << Customer;
 

	//delimCount = Customer.find(",");
	//stringName = string.substring (1,intNUmber);
 

	int currentcount;
 
    currentcount = Customer.find(",");
	
	lastName = Customer.substr(0,currentcount);
    Customer.substr(currentcount,Customer.length()-currentcount+1);
	
	cout << "\n\n" << lastName;
 
	cout << "\n\n" << Customer;
 
	
	firstName = Customer.substr(0,currentcount);
 
	cout << "\n\n" << firstName;
 
	Customer.substr(currentcount,Customer.length()-currentcount+2);
		cout << "\n\n" << Customer;
 
	partNumber = Customer.substr(0,currentcount);
 
	cout << "\n\n" << partNumber;
 
	Customer.substr(currentcount,Customer.length()-currentcount+3);
		cout << "\n\n" << Customer;
 
	date = Customer.substr(0,currentcount);
 
	cout << "\n\n" << date;
 
	Customer.substr(currentcount,Customer.length()-currentcount+4);
		cout << "\n\n" << Customer;
 
		myInfile2.close();
 

   //output everything to xml
 

 

 
	// save results to outfile
 

 

 
	pause();
	return 0;
}
</fstream></string></iomanip></iostream>

Reading a (simple) CSV file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
struct CSVData_t
  {
  string lastName;
  string firstName;
  string partNumber;
  string date;
  };

istream& operator >> ( istream& ins, CSVData_t csv )
  {
  string s;
  getline( ins, s );
  istringstream ss( s );
  getline( ss, s, ',' );  csv.lastName   = trim( s );
  getline( ss, s, ',' );  csv.firstName  = trim( s );
  getline( ss, s, ',' );  csv.partNumber = trim( s );
  getline( ss, s, ',' );  csv.date       = trim( s );
  return ins;
  }

For the code to trim(), see http://www.cplusplus.com/forum/beginner/6265/#msg28410

Now you can read the entire file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
  // Here's the list of the CSV file's records
  deque <CSVData_t> csv_data;

  // Read the file into our list
  ifsteam csvf( "c:\\c++\\Customer.csv" );
  CSVData_t csv_datum;
  while (csvf >> csv_datum)
    csv_data.push_back( csv_datum );
  if (!csvf.eof())
    complain();
  csvf.close();

  // Do something with the list here
  // (like write it to your XML file) 

Hope this helps.
OK Ill try this out tonight. I appreciate the help, and also that link has some great info too. Thank you so much!
Topic archived. No new replies allowed.