1

Hello I am using c++ and i read the file using fgets, i am using while loop and sscanf to push back to my vector double while i would like to do it using a single line, like in case of ifstream but i dont want to use get line.

%% 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);
              }
      }     

I am already using 2 while loop outside the above one for other purposes, hence i would like to avoid this..

Thank you for your help!! :) Priya

user3581370
  • 71
  • 1
  • 9
  • Don't use `feof` as loop condition, it doesn't do what you think it does. And you have an infinite loop because you keep reading the first number over and over. – M.M Sep 18 '15 at 13:22
  • I found the problem in the while loop, i was checking "\n" simply with strstr which is going to be always true. Hence I changed it like.. std::stringstream converter(line); while(converter >> temp_holder){ vec_TEC1D.push_back(temp_holder); } – user3581370 Sep 18 '15 at 13:45

2 Answers2

2

Why not use std::ifstream?

std::ifstream fin(filename);
std::vector<double> vec_TEC1D{ std::istream_iterator<double>{fin},
                               std::istream_iterator<double>{}};

(Adapted from this answer).

Community
  • 1
  • 1
Claudiu
  • 224,032
  • 165
  • 485
  • 680
  • Works also with ifstream which might be what OP wants to use. – Simon Kraemer Sep 17 '15 at 15:45
  • But my file has different lines, statements , so many sentences mixed up , where i check whether a particular condition and start reading the fixed number of doubles. if(strstr(line, "EPOCH START") != NULL ......then start reading – user3581370 Sep 18 '15 at 08:22
  • 1
    @user3581370 You didn't state that in your question. I would recommend to update your question and maybe provide an (mcve)[http://stackoverflow.com/help/mcve] and a example input file. – Simon Kraemer Sep 18 '15 at 09:28
0

Here are some pointers that may help you:

So your code may look like:

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

There are even more concise way of doing this, but I propose to process each line separatley as you do in your code. Maybe your file contains other lines and you want to process these differently.

fjardon
  • 7,921
  • 22
  • 31
  • Thnx, i am new to c++, can you pls tell me how to use ifstream when i input my file through argv argument. And by the way i do not want to use the second while loop instead i prefer to copy the entire stream of integers in a sequence into 1D vector.. – user3581370 Sep 18 '15 at 08:18
  • @user3581370 In that case you should use @Claudiu solution. Just replace `filename` with `argv[1]` if the filename is your first argument. Be sure to check that you have at least `argc > 1`. – fjardon Sep 18 '15 at 08:22
  • Oh that is good, but can i avoid the second while in the above answer and use some ifstream copy ?? – user3581370 Sep 18 '15 at 09:00
  • @user3581370 I updated the example to show how to read a single line with a `copy` call. – fjardon Sep 18 '15 at 09:23
  • But i have a pointer to the filename created and in use. Does ifstream accept file pointer as arguments in place of file name ? – user3581370 Sep 18 '15 at 09:31