Results 1 to 6 of 6

Thread: Reading from file and assigning it to a vector

  1. #1
    Join Date
    Nov 2009
    Posts
    47

    Reading from file and assigning it to a vector

    I have a file that contains a certain number of rows (one for each time-location) and a number of columns (different variables). What I want to do is to read one file, and extract each column to a variable and then call a function with these varable.

    Code:
    while(!eof)
    {
        line = read_line('\t');
        time = line[0];
        xpos = line[1];
        ypos = line[2];
    
        compute_velocity(time,xpos, ypos);
    }
    Reading a string-line and print it out is fine, but how do I split up after the tab, and saved to variable?

  2. #2
    Join Date
    Nov 2008
    Posts
    996

    Re: Reading from file and assigning it to a vector

    "while ( !feof )" is typically an idiom that does not work in C++, but at the conceptual level it properly.

    You need a few auxiliary functions:

    * One to divide a string into pieces by an arbitrary amount of punctuation marks (which you then call with "\t")
    * One to convert a textual representation of a number to the specific number (which you then spend on each of the pieces you pull out).

    At first, I can tell about find_first_of / find_first_not_of. To the others it may be instructive to look at lexical_cast <> as found in boost. If the solution should be in C, is thinking the same thing, but it will be more regards to memory management and such.

    What have you written so far?

  3. #3
    Join Date
    Nov 2009
    Posts
    47

    Re: Reading from file and assigning it to a vector

    I have only sought and soon found the following (which I must add find_first_of ()?:
    Code:
    // reading a text file
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    
    int main () {
      string line;
      ifstream myfile ("example.txt");
      if (myfile.is_open())
      {
        while (! myfile.eof() )
        {
          getline (myfile,line);
          cout << line << endl;
          string::size_type found = line.find_first_of("\t");
         
      }
        }
        myfile.close();
      }
    
      else cout << "Unable to open file"; 
    
      return 0;
    }
    This file does the rest typically like this:
    Code:
    0.0    1.234    1.2434
    0.1    1.343    1.4533
    0.2    1.454    1.3534
    ...osv

  4. #4
    Join Date
    Nov 2008
    Posts
    996

    Re: Reading from file and assigning it to a vector

    I asked what you had written so far, not what you found. So, therefore probably a couple of follow-up question - how would you create a function that takes a textual representation of the 3 numbers so you set the example below and divide up such a line in 3 pieces? What looks like a function that converts "123.45" to 123.45? (Tip: atof, lexical_cast <>, stringstreams).

    This will not work as intended. The reason for this is that the eof indicator does not need to be before you have actually tried to read past the end of the file. Standard procedure would be something along the lines of:

    Code:
    while ( getline(myfile, line, '\n') ) {
        /* do something with line */
    }

  5. #5
    Join Date
    Nov 2009
    Posts
    47

    Re: Reading from file and assigning it to a vector

    If I have understood it just returns "found line.find_first_of = (" \ t ");" index of the tab. So it should be something like this:
    Code:
    double var[];
    int i = 0;
    while( found != string::npos ) {
        string temp1 = line[found];
        found = line.find_first_of("\t",found+1);
    
        var[i++] = atof(temp1);
      }
    Is there another thing I do not have those listed. I have to run this code on a machine where I can add libraries that do not exist. Thus, I am somewhat unsure if I can use that boost the case.

  6. #6
    Join Date
    Nov 2008
    Posts
    996

    Re: Reading from file and assigning it to a vector

    You need to declare how many elements the array should have (vector <double> is surely a better choice). Moreover, line [found] should have one character in that position. What you need is an interval, from (past) where numbers begin until the next place where something other than numbers occur. Hence the tip about find_first_not_of. Then just create your own variation you can include. lexical_cast <> was meant as a suggestion / inspiration.

Similar Threads

  1. Need some help reading part of a log file...
    By agangsto in forum Operating Systems
    Replies: 2
    Last Post: 09-07-2010, 12:44 PM
  2. Reading XML file with ASP
    By Dharamsi in forum Software Development
    Replies: 4
    Last Post: 10-03-2010, 10:47 PM
  3. Problem in reading vector
    By Gunner 1 in forum Software Development
    Replies: 5
    Last Post: 03-02-2010, 03:59 AM
  4. Reading file bit by bit
    By John Wilson in forum Software Development
    Replies: 4
    Last Post: 14-12-2009, 01:49 PM
  5. Java reading from a file
    By xqc72 in forum Software Development
    Replies: 1
    Last Post: 20-11-2009, 08:20 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,710,837,678.10603 seconds with 17 queries