Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用ifstream读取带有逗号的.txt文件_C++_String_Stream_Ifstream - Fatal编程技术网

C++ 使用ifstream读取带有逗号的.txt文件

C++ 使用ifstream读取带有逗号的.txt文件,c++,string,stream,ifstream,C++,String,Stream,Ifstream,基于ifstream,我使用以下函数读取filename.txt文件 //从文本文件读取 静态std::向量向量向量; 双a[18]//从txt文件读取的值 int i=0; void readDATA() { 双重价值; std::ifstream myFile; 打开(“filename.txt”,std::ios::app); 如果(myFile.is_open()) { std::cout值) { 向量推回(值); std::cout您想读取数字后跟冒号吗 定义将处理此“任务”的类: 然

基于ifstream,我使用以下函数读取filename.txt文件

//从文本文件读取
静态std::向量向量向量;
双a[18]//从txt文件读取的值
int i=0;
void readDATA()
{
双重价值;
std::ifstream myFile;
打开(“filename.txt”,std::ios::app);
如果(myFile.is_open())
{
std::cout值)
{
向量推回(值);

std::cout您想读取数字后跟冒号吗

定义将处理此“任务”的类:

然后在代码中-将
双值;
替换为
数值;


像这样的东西你可以试试

std::string data;
while(std::getline(myFile, data)) {
    std::istringstream ss(data);
    std::string token;

    while(std::getline(ss, token, ','))
        double d = std::stod(token);
}

我想从txt文件0,0,40,45,15\n 0,1,40,-45,10\n 0,0,180,90,15中读取后跟逗号的数字\n@Iko-SOF-我编写的代码不需要编译、运行或验证。现在应该可以运行了。感谢演示。实际上,我所说的\n是新行。为了更清楚,请在链接中找到我试图读取的txt文件'it’修改我在问题中已经提供的代码,让它读取上传到链接中的文件,这对我来说很重要。添加我提供的代码-然后在代码中“用数值替换双精度值”;这是否回答了你的问题?
struct Number 
{
   double value;
   operator double() const { return value; }
}; 
std::istream& operator >>(std::istream& is, Number& number)
{
     is >> number.value;
     // fail istream on anything other than ',' or whitespace
     // end reading on ',' or '\n'
    for (char c = is.get();; c = is.get()) {
        if (c == ',' or c == '\n')
            break;
        if (std::isspace(c))
            continue;

        is.setstate(std::ios_base::failbit);
        break;
    }
     return is;
}
std::string data;
while(std::getline(myFile, data)) {
    std::istringstream ss(data);
    std::string token;

    while(std::getline(ss, token, ','))
        double d = std::stod(token);
}