Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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/3/arrays/12.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
如何从FGET读取整个整数流并将其推回一维向量? 你好,我用C++,我用FFET读文件,我使用while while和sSCANF来推回我的向量倍,而我想用一行来做,就像IFFSUSTER的情况一样,但是我不想使用GET行。p> %% My stream of data 151 150 149 148 147 146 145 144 143 143 141 139 138 137 135 132 130 130 129 128 127 127 128 129 130 129 128 127 126 127 127 127 127 128 128 128 129 130 130 131 131 132 132 133 133 %% My code vector<double> vec_TEC1D; double temp_holder = 0.0; while(!feof(fileptr)) { fgets(line, LENGTH_LINE, fileptr); ..... while(strstr(line, '\n') != NULL){ sscanf(line, "%lf", &temp_holder); vec_TEC1D.push_back(temp_holder); } } %%我的数据流 151 150 149 148 147 146 145 144 143 143 141 139 138 137 135 132 130 130 129 128 127 127 128 129 130 129 128 127 126 127 127 127 127 128 128 128 129 130 130 131 131 132 132 133 133 %%我的代码 向量向量; 双温度保持架=0.0; 而(!feof(fileptr)) { fgets(线、长度线、文件管理器); ..... while(strstr(行“\n”)!=NULL){ sscanf(行“%lf”和临时支架); 车辆技术推回(临时支架); } }_C++_File_Vector_Ifstream_Fgets - Fatal编程技术网

如何从FGET读取整个整数流并将其推回一维向量? 你好,我用C++,我用FFET读文件,我使用while while和sSCANF来推回我的向量倍,而我想用一行来做,就像IFFSUSTER的情况一样,但是我不想使用GET行。p> %% My stream of data 151 150 149 148 147 146 145 144 143 143 141 139 138 137 135 132 130 130 129 128 127 127 128 129 130 129 128 127 126 127 127 127 127 128 128 128 129 130 130 131 131 132 132 133 133 %% My code vector<double> vec_TEC1D; double temp_holder = 0.0; while(!feof(fileptr)) { fgets(line, LENGTH_LINE, fileptr); ..... while(strstr(line, '\n') != NULL){ sscanf(line, "%lf", &temp_holder); vec_TEC1D.push_back(temp_holder); } } %%我的数据流 151 150 149 148 147 146 145 144 143 143 141 139 138 137 135 132 130 130 129 128 127 127 128 129 130 129 128 127 126 127 127 127 127 128 128 128 129 130 130 131 131 132 132 133 133 %%我的代码 向量向量; 双温度保持架=0.0; 而(!feof(fileptr)) { fgets(线、长度线、文件管理器); ..... while(strstr(行“\n”)!=NULL){ sscanf(行“%lf”和临时支架); 车辆技术推回(临时支架); } }

如何从FGET读取整个整数流并将其推回一维向量? 你好,我用C++,我用FFET读文件,我使用while while和sSCANF来推回我的向量倍,而我想用一行来做,就像IFFSUSTER的情况一样,但是我不想使用GET行。p> %% My stream of data 151 150 149 148 147 146 145 144 143 143 141 139 138 137 135 132 130 130 129 128 127 127 128 129 130 129 128 127 126 127 127 127 127 128 128 128 129 130 130 131 131 132 132 133 133 %% My code vector<double> vec_TEC1D; double temp_holder = 0.0; while(!feof(fileptr)) { fgets(line, LENGTH_LINE, fileptr); ..... while(strstr(line, '\n') != NULL){ sscanf(line, "%lf", &temp_holder); vec_TEC1D.push_back(temp_holder); } } %%我的数据流 151 150 149 148 147 146 145 144 143 143 141 139 138 137 135 132 130 130 129 128 127 127 128 129 130 129 128 127 126 127 127 127 127 128 128 128 129 130 130 131 131 132 132 133 133 %%我的代码 向量向量; 双温度保持架=0.0; 而(!feof(fileptr)) { fgets(线、长度线、文件管理器); ..... while(strstr(行“\n”)!=NULL){ sscanf(行“%lf”和临时支架); 车辆技术推回(临时支架); } },c++,file,vector,ifstream,fgets,C++,File,Vector,Ifstream,Fgets,我已经在上面一个之外使用2 while循环用于其他目的,因此我希望避免这种情况 谢谢你的帮助!!:) Priya为什么不使用 std::ifstream-fin(文件名); std::vector vec_TEC1D{std::istream_迭代器{fin}, std::istream_迭代器{}; (改编自)。以下是一些可以帮助您的提示: 文件: 字符串: 从字符串到int的转换: 阅读一行文字: 在向量中向后推_的迭代器: 因此,您的代码可能如下所示: #include <al

我已经在上面一个之外使用2 while循环用于其他目的,因此我希望避免这种情况

谢谢你的帮助!!:) Priya为什么不使用

std::ifstream-fin(文件名);
std::vector vec_TEC1D{std::istream_迭代器{fin},
std::istream_迭代器{};

(改编自)。

以下是一些可以帮助您的提示:

  • 文件:
  • 字符串:
  • 从字符串到int的转换:
  • 阅读一行文字:
  • 在向量中向后推_的迭代器:
因此,您的代码可能如下所示:

#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>


int main(int argc, char* argv[]) {
  if(argc < 2)
    return -1;

  std::ifstream input(argv[1]);
  std::vector<double> data;
  std::string line;
  while(std::getline(input, line)) {
    std::stringstream converter(line);
    std::copy(std::istream_iterator<double>(converter),
          std::istream_iterator<double>(),
          std::back_inserter(data));
  }

  // Do something with the data, like print it...
  std::copy(begin(data), end(data), std::ostream_iterator<double>(std::cout, " "));
  return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main(int argc,char*argv[]){
如果(argc<2)
返回-1;
std::ifstream输入(argv[1]);
std::矢量数据;
std::字符串行;
while(std::getline(输入,行)){
std::stringstream转换器(线路);
std::copy(std::istream_迭代器(转换器),
std::istream_迭代器(),
标准::反向插入器(数据);
}
//对数据做一些处理,比如打印它。。。
std::copy(开始(数据)、结束(数据)、std::ostream\u迭代器(std::cout“”);
返回0;
}

有更简洁的方法可以做到这一点,但我建议像在代码中一样单独处理每一行。可能您的文件包含其他行,您希望以不同的方式处理这些行。

也适用于ifstream,这可能是OP希望使用的。但我的文件有不同的行、语句,有太多的句子混在一起,在这里,我检查一个特定的条件,并开始读取固定数量的双倍。如果(strstr(行,“历元开始”)!=NULL……则开始reading@user3581370您的问题中没有说明这一点。我建议您更新您的问题,并提供(mcve)一个示例输入文件THNX,我是C++的新手,你可以告诉我如何使用IFSRoT当我通过ARGV参数输入我的文件时,顺便说一下,我不想使用第二个while循环,而是喜欢把整个整数序列中的整数拷贝到一维向量中。@ USE3581370,在这种情况下,你应该使用@ Celdiu解决方案。
filename
带有
argv[1]
如果文件名是你的第一个参数。一定要检查你是否至少有
argc>1
。哦,这很好,但是在上面的回答中,我可以避免第二个参数,并使用一些ifstream copy吗???@user3581370我更新了这个示例,展示了如何通过
copy
调用读取单行。但是我有一个指向文件名crea的指针ted和正在使用中。ifstream是否接受文件指针作为参数而不是文件名?不要使用
feof
作为循环条件,它不会做您认为它会做的事情。并且您有一个无限循环,因为您不断重复读取第一个数字。我在while循环中发现问题,我正在检查“\n”简单地使用strstr,这将永远是真的。因此我将其更改为..std::stringstream converter(line);while(converter>>temp_holder){vec_TEC1D.push_back(temp_holder);}
#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>


int main(int argc, char* argv[]) {
  if(argc < 2)
    return -1;

  std::ifstream input(argv[1]);
  std::vector<double> data;
  std::string line;
  while(std::getline(input, line)) {
    std::stringstream converter(line);
    std::copy(std::istream_iterator<double>(converter),
          std::istream_iterator<double>(),
          std::back_inserter(data));
  }

  // Do something with the data, like print it...
  std::copy(begin(data), end(data), std::ostream_iterator<double>(std::cout, " "));
  return 0;
}