Link to home
Start Free TrialLog in
Avatar of tom_mk
tom_mk

asked on

Fetch Content of the file

this is my reference
i asked s..t yesterday, and this prob is associated with that prob
https://www.experts-exchange.com/questions/21167966/ask-content-of-the-file-into-array.html

in that post.
i say that i only have one line ...
says now , i have multiline of that.
can any one suggest me the best way to store them.

i thinking of multidimension array.
and vector. in the case of vector , i think i need to read in each array first and then store each into the vector. is that a good idea?

however, i don't have experience w/ vector before.
can any1 help me a bit of this?


Thx a lot
Tom
Avatar of pcxboy
pcxboy

could you clarify exactly what it is you are trying to do? I'm taking Data Structures right now, and Ive dealt with this before.
Avatar of tom_mk

ASKER

1.0 2.0 1.0 10 20 10 10 -20 10 10 10 20 0 20 10 -10 50 -40 50 10
2.0 2.0 1.0 10 20 10 10 -20 10 10 10 20 0 20 10 -10 50 -40 50 10
3.0 2.0 1.0 10 20 10 10 -20 10 10 10 20 0 20 10 -10 50 -40 50 10

is my file
i wanna each line into my program
so that i can use one of the value in each line directly

thx
TOm
Do you want each seperate number, or each seperate line? If you want each seperate line, here is some code:

//This opens up the file, and gives the object that handles that file the name of READER
std::ifstream READER("WHATEVER THE NAME OF YOUR FILE IS");                

//This stores each line
std::string lines;

//This creates a vector of strings, and calls it line_holder
std::vector<std::string> line_holder;

//This reads each line from your file (READER) and puts it inside the std::string lines. It will terminate once there are no more lines to get from the file
while(std::getline(READER,lines))
{
  //This puts each line into the vector.
  line_holder.push_back(lines);
}

If you want to get each individual number, then let me know.

Mike
Here is some code if you want each number, instead of each line:

std::ifstream READER("WHATEVER THE NAME OF YOUR FILE IS");              

std::string lines;

std::vector<std::string> lines_holder;

while(READER>>lines)
{
  lines_holder.push_back(lines);
}

If you want the ACTUAL numbers, and not just strings, then try this code:

std::ifstream READER("WHATEVER THE NAME OF YOUR FILE IS");              

std::string lines;

//This is a double to handle the negative numbers and the decimal numbers
std::vector<double> number_holder;

while(READER>>lines)
{
 //Creates a string-stream object. This can convert between different data types, depending on what the data types are
 std::sstream convert;

// A Temporary double object to store each number from your file as it comes in.
 double temp;

//Shoves the string from the file into the string-stream
 convert << lines;

//Shoves it right back into the temporary double object, onverting it to a double
 convert >> temp;
 number_holder.push_back(temp);
}

Again, if this isnt what youre looking for, please let me know.

Mike
#include <fstream>
#include <string>
#include <deque>
#include <iomanip>
#include <iostream>
#include <stdlib.h>
#include <sstream>
int parse_line(std::string line, std::deque<float> &que)
{
      //return number of float parsed from the line
      int existing  = que.size();
      std::string separators = " ";
          int n = line.length();
          int start, stop;
      std::stringstream oss;
          start = line.find_first_not_of(separators);
          while ((start >= 0) && (start < n))
      {
             stop = line.find_first_of(separators, start);
             if ((stop < 0) || (stop > n))
                     stop = n;
            std::string field = line.substr(start, stop - start);
            std::stringstream oss;
            oss<<field;
            float val = 0;
            oss>>val;
             que.push_back(val);
             start = line.find_first_not_of(separators, stop+1);
       }
      int nret = que.size() - existing;      
      std::cout<<line<<"  values in line:"<<nret<<std::endl;
      return nret;
}
int main()
{
      std::ifstream input("C:\\test\\aaa\\input.txt");
      std::deque<std::string> lines;
      std::string line;
      std::deque<float> values;
      while(getline(input,line))
            {
            lines.push_back(line);
            parse_line(line,values);
            }
      int index = 0;
      for(std::deque<float>::const_iterator it = values.begin();
      it!=values.end();
      it++)
            {
            std::cout<<std::setw(4)<<std::setfill(' ')<<*it;
            index++;
            if(index%20 == 0)
                  std::cout<<std::endl;
            }
      std::cout<<"total values:"<<values.size()<<std::endl;
}

/*
   g++ -c parse.cpp
   g++ -o parse parse.o

    welcome to www.fruitfruit.com
*/
Avatar of tom_mk

ASKER

onegaZHang,

i not kinda understnad how to get the value out .
 (pls c the example of my txt file above)

says i wanna get the value '-40' out from the line begins with 1.0

how can i get tat?
//deque is similar to an array

#include <fstream>
#include <string>
#include <deque>
#include <iomanip>
#include <iostream>
#include <sstream>
int parse_line(std::string line, std::deque<float> &que)
{
      //return number of float parsed from the line
      int existing  = que.size();
      std::string separators = " ";
          int n = line.length();
          int start, stop;
      std::stringstream oss;
          start = line.find_first_not_of(separators);
          while ((start >= 0) && (start < n))
      {
             stop = line.find_first_of(separators, start);
             if ((stop < 0) || (stop > n))
                     stop = n;
            std::string field = line.substr(start, stop - start);
            std::stringstream oss;
            oss<<field;
            float val = 0;
            oss>>val;
             que.push_back(val);
             start = line.find_first_not_of(separators, stop+1);
       }
      int nret = que.size() - existing;      
      std::cout<<line<<"  values in line:"<<nret<<std::endl;
      return nret;
}
int main()
{
      std::ifstream input("C:\\test\\aaa\\input.txt");
      std::deque<std::string> lines;
      std::string line;
      std::deque<float> values;
      int fields_per_row = 0;
      while(getline(input,line))
            {
            lines.push_back(line);
            fields_per_row = parse_line(line,values);
            }
      int index = 0;
      for(std::deque<float>::const_iterator it = values.begin();
      it!=values.end();
      it++)
            {
            std::cout<<std::setw(4)<<std::setfill(' ')<<*it;
            index++;
            if(index%20 == 0)
                  std::cout<<std::endl;
            }
      std::cout<<"total values:"<<values.size()<<std::endl;
      int line_count = lines.size();
      for(int i=0;i<line_count;i++)
            {
            //line = lines[i];
            if(values[i*fields_per_row] == 1.0)
                  {
                  //found the line start with 1.0
                  for(int y=0;y<fields_per_row;y++)
                        {
                        if(values[i*fields_per_row+y] == -40)
                              {
                              //find value 40
                              std::cout<<"the value was found in line "<<i<<" column "<<(y+1)<<std::endl;
                              break;
                              }                        
                        }
                  break;
                  }
            }
}

/*
   g++ -c parse.cpp
   g++ -o parse parse.o

    welcome to www.fruitfruit.com
*/
Avatar of tom_mk

ASKER

OnegaZhang

the one u doing is.  read eachline into array.
and put all array into queue ?

rgiht?
Avatar of tom_mk

ASKER

can s.o help me ?

i want to use vector to staore each array. which each array consistf of number from a line

Thx
TOm
#include <fstream>
#include <string>
#include <deque>
#include <iomanip>
#include <iostream>
#include <vector>
#include <sstream>

template<typename T>
int parse_line(std::string line, T& que)
{
      //return number of float parsed from the line
      int existing  = que.size();
      std::string separators = " ";
          int n = line.length();
          int start, stop;
          start = line.find_first_not_of(separators);
          int field_index = 0;
          while ((start >= 0) && (start < n))
      {
             stop = line.find_first_of(separators, start);
             if ((stop < 0) || (stop > n))
                     stop = n;
            std::string field = line.substr(start, stop - start);
            std::stringstream oss;
            oss<<field;
            float val = 0;
            oss>>val;
             que[field_index] = (val);
             field_index++;
             start = line.find_first_not_of(separators, stop+1);
       }
      int nret = field_index;
      return nret;
}
int main()
{
      std::ifstream input("C:\\test\\aaa\\input.txt");
      std::deque<std::string> lines;
      std::string line;
      int fields_per_row = 0;
      bool bfound = false;
      while(getline(input,line) && bfound==false)
            {
                lines.push_back(line);
                std::vector<float> valarray(100);
                fields_per_row = parse_line(line,valarray);
                if(valarray[0] == 1.0 ) //find the line start with 1.0
                {//be careful to compare floating point number, should not use ==
                    for(int i=0;i<fields_per_row;i++)
                    {
                        if(valarray[i] == -40)
                        {
                            bfound = true;
                            std::cout<<"fount -40 in line "<<lines.size()<<" at column "<<(1+i)<<std::endl;
                            break;
                        }    
                    }  
                }  
            }
            system("PAUSE");
}

/*
   g++ -c parse.cpp
   g++ -o parse parse.o

    welcome to www.fruitfruit.com
*/
Avatar of tom_mk

ASKER

is there any easier code than this?

thx again .
tom
ASKER CERTIFIED SOLUTION
Avatar of OnegaZhang
OnegaZhang

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial