用|替换在getline/ignore之后. [英] Replace , with | after getline/ignore.

查看:76
本文介绍了用|替换在getline/ignore之后.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

弄清楚了.这会将逗号替换为一个定界符,然后忽略该行的其余部分,并抓住该行的两个部分并重新创建它们:

Figured it out. This replaces commas up to a delimiter and then ignores the rest of the line, grabbing both parts of the line and recreating them:

if (incsv.is_open())
{
    int x=0;
    while (x<5)
    {
        getline(incsv, stop, '\n');
        x=(x + 1);
    }


    while (!incsv.eof())
        {
      getline(incsv, teststr, '\"');
      replace(teststr.begin(), teststr.end(), ',', '|');
      getline(incsv, teststr1, '\n');
      replace(teststr1.begin(), teststr1.end(), '\"', ',');
      outtxt << teststr << teststr1 << endl;
    }
    incsv.close();
        return 0;
}



我试图让我的程序从CSV文件中读取,在定界符处停止,然后忽略该行的其余部分.

输入来自example.csv

名称1,A,LNam1,Com1,Desc,"OU = 1,OU = 2,OU = 3,OU = 4,OU = 5"
名称2,A,LNam2,Com2,Desc,"OU = 1,OU = 2,OU = 3,OU = 4,OU = 5"
名称3,A,LNam3,Com3,Desc,"OU = 1,OU = 2,OU = 3,OU = 4,OU = 5"



I''m trying to have my program read from a CSV file, stop at the delimiter, and ignore the rest of the line.

Input from example.csv

Name1,A,LNam1,Com1,Desc,"OU=1,OU=2,OU=3,OU=4,OU=5"
Name2,A,LNam2,Com2,Desc,"OU=1,OU=2,OU=3,OU=4,OU=5"
Name3,A,LNam3,Com3,Desc,"OU=1,OU=2,OU=3,OU=4,OU=5"

// reading a text file
#include iostream
#include fstream
#include string

using namespace std;
int main (void) 
{
  string teststr;
  ifstream infile ("example.csv");
  if (infile.is_open())
  {
	  while (!infile.eof())
		  {
   	    getline(infile, teststr, '\"');
	    infile.ignore(80, '\n');
		cout << teststr << endl;
	  }
	  getchar();

	  infile.close();
		  return 0;

  }

推荐答案

这是C代码,而不是C ++.这使事情变得更加复杂.如果您使用的是C ++,我敢肯定您可以使用CSV解析类.规则很简单,您需要查找引号之外的逗号.例如,您可以编写一个iostreams阅读器来为您完成此操作,并随即更改定界符.但是,同样,这是C ++,此代码是C.stdio.h是C,而string.h不是标准C ++.您正在使用C文件处理.如果您在C ++项目中,则需要学习如何使用C ++而不是C,它会更好.
This is C code, not C++. That makes things a lot more complicated. If you were using C++, I''m sure there would be CSV parsing classes you could use. The rule is simple, you look for commas that are outside quotes. You could write an iostreams reader to do that for you, for example, and change the delimiter as it goes. But, again, that''s C++ and this code is C. stdio.h is C, and string.h is not standard C++. You are using C file handling. If you''re in a C++ project, then you need to learn how to use C++ instead of C, it is much better.


这篇关于用|替换在getline/ignore之后.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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