Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 08-05-2004, 06:24 PM   #1
Registered: Jul 2004
Posts: 60

Rep: Reputation: 15
c++ get() delimiter


If we were reading text would it be possible to set the delimiter to ' ' (space) and '\t' (tabbed space).

Old 08-05-2004, 09:13 PM   #2
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
Take your pick:

1) int istream::get(char *str, streamsize count, char delim)
2) istream& istream::getline(char *str, streamsize count, char delim)
3) istream& std::getline(istream& strm, string& str, char delim)
4) extraction operator, >>, is whitespace delimited

1) does not read delim
2) and 3) do read delim but do not append it to str

(Hope I got that the right way round )
Old 08-05-2004, 11:02 PM   #3
Registered: Jul 2004
Posts: 60

Original Poster
Rep: Reputation: 15

int istream::get(char *str, streamsize count, char delim)

if I wish to delimit space i.e ' ' and tab '\t', how would I go about that. Cause im tryin to read some text and some are spaced and some are tabbed, so if i were to delimit just '\t' the spaced columns would be read as one. I hope you get what im saying...

Old 08-06-2004, 06:26 AM   #4
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
If you are reading whitespace delimited text, then I wouldn't use get().

This partly depends on the file format and what you want to do with it.
For example, a config or tabular format, I might read into one whole line, using std::getline(istream&, string) and then parse each line into it's constituent parts.

Just for reading a whole load of text though, you could do something like this:

#include <fstream>       // for std::ifstream, std:: ofstream
#include <iostream>      // for std::cout
#include <string>        // for std::string
#include <vector>        // for std::vector
#include <iterator>      // for std:: ostream_iterator
#include <algorithm>     // for std::copy

using namespace std;

int main()
  // First write some text - I have put tab delimited line and space demited
  // lines in
  ofstream ofs("test.txt");
  ofs << "Line one using space\nLine\ttwo\tusing\ttab\t\nLine  three   big   spaces  no   eol";

  // Now open the file
  ifstream ifs("test.txt");

  // Create a buffer to read the string
  string tmp;
  // Use a vector to store all the words separately
  vector<string> text;

  // Just keep reading until the end of the file (or an error!)
  // the operator >> will discard anything that it sees as 'whitespace'
  // tab's, spaces, newlines
  while( ifs >> tmp )


  // This line will just print out every word to stdout
  // I put the "#\n" bit, appended after each word, so you can see exactly
  // what is in each string (i.e. you will see a '#' at the end of every line)
  copy(text.begin(), text.end(), ostream_iterator<string>(cout, "#\n"));

Last edited by dakensta; 08-06-2004 at 06:34 AM.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Divide up lines with string delimiter elmu Programming 3 10-07-2005 09:48 AM
getline delimiter + move to next line? blizunt7 Programming 3 07-09-2005 01:08 AM
Issues calling sort from perl using pipe as delimiter amytys Programming 1 10-20-2004 10:35 PM
sed with delimiter, bourne uribo Programming 17 04-23-2003 02:42 AM
tab delimiter codename000 Programming 3 04-04-2003 11:18 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:20 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration