Thread: String - spaced out words

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    String - spaced out words

    Hi,

    I have a few lines of text, looks something like:

    QF File
    47.0030 output1
    A B Long Result
    659.6406 107.3926 37.0000 0
    659.8189 107.4216 37.0100 0
    659.9972 107.4506 37.0200 0
    660.1755 107.4797 37.0300 0
    660.3538 107.5087 37.0400 0
    I want to make this in to a CSV.

    Code:
    			getline(multiple[i], linestr);
    
    			if ((linestr == "") || (multiple[i].eof()))
    			{
    				if(i == 1)
    					resf << ",";
    				else
    					resf << ",,";
    			}
    			else
    			{
    
    
    }
    Whats the best way to do this?

    I just want to collect the data (not the white space) and replace the whitespace by a comma.

    Cheers

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    I'd do it like this(Using console for now, adapt for any ifstream) :
    Code:
    #include<iostream>
    #include<iterator>
    int main()
    {
        std::istream_iterator<double> it(std::cin),eos;
        for(;it!=eos;++it)
            std::cout<<*it<<',';
    }

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Is this just a one-time task? If you don't mind using scripting languages it's pretty easy...
    Code:
    $ cat /tmp/input 
    QF File
    47.0030 output1
    A B Long Result
    659.6406 107.3926 37.0000 0
    659.8189 107.4216 37.0100 0
    659.9972 107.4506 37.0200 0
    660.1755 107.4797 37.0300 0
    660.3538 107.5087 37.0400 0
    $ awk '{ print $1 "," $2 "," $3 "," $4 }' /tmp/input 
    QF,File,,
    47.0030,output1,,
    A,B,Long,Result
    659.6406,107.3926,37.0000,0
    659.8189,107.4216,37.0100,0
    659.9972,107.4506,37.0200,0
    660.1755,107.4797,37.0300,0
    660.3538,107.5087,37.0400,0
    $
    Just as an example. In C++ I'd do something similar to manasij, except without fancy stream iterators...
    Code:
    std::string line;
    std::getline(std::cin, line);
    
    // now let's make a stringstream out of "line"
    std::istringstream stream(line);
    
    // okay, so read 4 words from the stringstream. If there are fewer than 4, we'll get empty strings.
    std::string word;
    for(int i = 0; i < 4; i ++) {
        word = "";
        stream >> word;
        std::cout << word;
        if(i + 1 < 4) std::cout << ",";
    }
    Something like that.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Words in a string
    By mrbains in forum C Programming
    Replies: 8
    Last Post: 10-25-2010, 08:47 AM
  2. Replies: 7
    Last Post: 10-01-2010, 04:09 PM
  3. String with two words: 1)String, 2) $double
    By Roger in forum C Programming
    Replies: 4
    Last Post: 11-20-2009, 02:14 PM
  4. Double spaced posts.
    By VirtualAce in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 07-17-2003, 05:54 PM
  5. Reading a spaced file
    By Korhedron in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2003, 09:49 AM