如何使用类将文本文件转换为CSV? [英] How can I convert a text file to CSV using classes?

查看:102
本文介绍了如何使用类将文本文件转换为CSV?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个将管道分隔的文本文件转换为csv的C ++类。我是OOP的新手。



谢谢!

I want to create a C++ class that will convert a pipe-delimited text file to csv. I new to OOP.

Thanks!

推荐答案

从技术上讲,一个文件的元素由'|分隔。 '字符是CSV文件 - 名称涵盖多个分隔符,大多数CSV阅读器允许您指定分隔符。

http://en.wikipedia.org/wiki/Comma-separated_values [ ^ ]



将数据从|转换为简单是微不足道的',' - 只需读入它,扫描'|'并用','来表示它们 - 它不需要任何插入或删除,因此文件长度没有变化。但是,您可能需要检查内容 - 如果|之间的字段包含,并且未被字符包围,那么您还需要替换它们。



但在大多数情况下,您根本无需进行任何更改即可阅读|分隔文件。
Technically, a file with elements delimited by '|' characters is a CSV file - the name covers a multitude of separators and most CSV readers allow you to specify the separator character.
http://en.wikipedia.org/wiki/Comma-separated_values[^]

It would be trivial to do a "simple" conversion of your data from '|' to ',' - just read it in, scan for '|' and splat them with a ',' - it doesn't require any inserts or deletes so there is no change in the file length. However, it may be that you need to check the content as well - if the fields between '|' contain ',' and are not surrounded by '"' characters then you will need to replace those as well.

But in most case, you don't need to make any changes at all to read the '|' separated file.


这篇关于如何使用类将文本文件转换为CSV?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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