Solved

Fetch Content of the file

Posted on 2004-10-14
15
200 Views
Last Modified: 2010-04-01
this is my reference
i asked s..t yesterday, and this prob is associated with that prob
http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_21167966.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
0
Comment
Question by:tom_mk
  • 5
  • 4
  • 3
15 Comments
 

Expert Comment

by:pcxboy
ID: 12313509
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.
0
 

Author Comment

by:tom_mk
ID: 12313979
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
0
 

Expert Comment

by:pcxboy
ID: 12314751
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
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Expert Comment

by:pcxboy
ID: 12314867
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
0
 
LVL 12

Expert Comment

by:OnegaZhang
ID: 12315102
#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
*/
0
 

Author Comment

by:tom_mk
ID: 12315889
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?
0
 
LVL 12

Expert Comment

by:OnegaZhang
ID: 12316190
//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
*/
0
 

Author Comment

by:tom_mk
ID: 12316222
OnegaZhang

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

rgiht?
0
 

Author Comment

by:tom_mk
ID: 12316274
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
0
 
LVL 12

Expert Comment

by:OnegaZhang
ID: 12316637
#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
*/
0
 

Author Comment

by:tom_mk
ID: 12316835
is there any easier code than this?

thx again .
tom
0
 
LVL 12

Accepted Solution

by:
OnegaZhang earned 500 total points
ID: 12332133
parse_line parse a string into an array of values,
while(getline(input,line) && bfound==false) // read all lines from a file
 if(valarray[0] == 1.0 ) //find the line start with 1.0
                  for(int i=0;i<fields_per_row;i++)
                  {
                      if(valarray[i] == -40) // find the position of -40
                      {
                          bfound = true;
                          std::cout<<"fount -40 in line "<<lines.size()<<" at column "<<(1+i)<<std::endl;
                          break;
                      }    
                  }  
It is pure C++, not too complicated:-)
welcome to www.fruitfruit.com
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question