Click here to Skip to main content
14,664,409 members
Rate this:
Please Sign up or sign in to vote.
See more:
So i was trying to make a program to let me read from 2 separate files, one being csv 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
Used Car

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

the indented section of the above input is working correctly. The second part is not chopping out each section

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)

my code:
#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;

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
	//"c:\\c++\\Customer.csv"); //openfiles  MAKE SURE FILE NAMES AND DIRECTORIES MATCH!!!
	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;

	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);
	cout << "\n\n" << lastName;

	cout << "\n\n" << Customer;

	firstName = Customer.substr(0,currentcount);

	cout << "\n\n" << firstName;

		cout << "\n\n" << Customer;

	partNumber = Customer.substr(0,currentcount);

	cout << "\n\n" << partNumber;

		cout << "\n\n" << Customer;

	date = Customer.substr(0,currentcount);

	cout << "\n\n" << date;

		cout << "\n\n" << Customer;


   //output everything to xml

	// save results to outfile

	return 0;
Updated 5-Dec-11 10:25am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

One big problem you have is that you're treating the input as if it has fixed length fields. A comma delimited file is unlikely to be formatting this way.

Rather than fix the code you have, I suggest a rewrite.

Use the string::find(',') function to find the comma locations, and parse using those offsets, rather than using hard coded values.
xaviorin 5-Dec-11 17:21pm
This is the format i was handed that i have to code for so that's why i was trying to make the program accept that specific formatting. As for the string::find(',') function, since i am relatively new at coding I have never seen or heard it referenced before. (coding for literally like 2 months)

Even though the way I'm doing it isn't the preferred way of most devs (and i apologize for that) this is the only way i know how to code at the moment so would there be a way to salvage what i do have coded already? Or am i pretty much screwed until i have time to teach myself another way? But i appreciate the help you've given thus far
JackDingler 5-Dec-11 17:35pm
It's not really a new way of coding so much as an enhancement to what you're doing. You're reading fixed length fields, and that will definitely fail when reading comma delimited files.

The find function scans the string until it find the character or string you're looking for. Then it returns the position of that character or string.

Take a look at this link, it explains the function and includes an example:
xaviorin 5-Dec-11 18:41pm
Aha just look through it thank you so much! I appreciate all the help!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100