帮助读取逗号分隔文件 [英] Help Reading Comma Delimited file

查看:75
本文介绍了帮助读取逗号分隔文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图制作一个程序,让我从2个单独的文件中读取文件,一个是csv文件,一个是.dat文件,然后从这两个文件中获取信息并将它们组合成一个xml输出文件.这是问题所在,从我所看到的来看,我的代码的前半部分工作正常,但是下半部分必须从格式如下的文件中读取:

明亮,丰富,PC12K2RT,2011年10月21日

并且无论我将其值更改为什么,当程序执行并在输出中如下所示时,都会一直给我相同的结果:

PC12K2RT二手车02350.00
PC12K2RT
2
二手车
02350.00

Bright,Rich,PC12K2RT,2011年10月21日
明亮
Bright,Rich,PC12K2RT,2011年10月21日
明亮
Bright,Rich,PC12K2RT,2011年10月21日
明亮
Bright,Rich,PC12K2RT,2011年10月21日
明亮
Bright,Rich,PC12K2RT,2011年10月21日

上述输入的缩进部分工作正常.第二部分不是将每个部分都切掉


我的意图是首先将文件中的所有内容都放入一个变量中,然后将所有内容从开始"到第一个逗号(减去逗号)保存到第二个变量中,依此类推,直到EOF,其中逗号之间的每个字符串或值都另存为一个单独的变量多变的.不想仅仅将代码提供给我,但是如果您可以显示一个示例并解释您为什么这样做,那么我将不胜感激.尝试将这种情况断断续续搞了将近3个星期,并认为某人的外部意见可能是有益的:

注意:目前我设置的方式会在输出提示中显示将写入文件的内容,因此我知道其是否正常运行而不必不断打开输出文件(xml部分不完整,因为我不需要帮助该部分)

我的代码:

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
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 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;
	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>

推荐答案

您遇到的一个大问题是,您将输入视为具有固定长度的字段.以逗号分隔的文件不太可能以这种方式格式化.

建议不要重写您拥有的代码.

使用string :: find('','')函数查找逗号位置,并使用这些偏移量而不是使用硬编码值进行解析.
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.


这篇关于帮助读取逗号分隔文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆