Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Fetch Content of the file

Posted on 2004-10-14
15
Medium Priority
?
216 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

971 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