LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


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

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


Hi,

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

Thanks
Shirazi
 
Old 08-05-2004, 08:13 PM   #2
dakensta
Member
 
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

N.B.
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, 10:02 PM   #3
ashirazi
Member
 
Registered: Jul 2004
Posts: 60

Original Poster
Rep: Reputation: 15
In

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

Thanks
Shirazi
 
Old 08-06-2004, 05:26 AM   #4
dakensta
Member
 
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:

Code:
#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";
  ofs.close();

  // 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 )
    {
      text.push_back(tmp);
    }

  ifs.close();

  // 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 05:34 AM.
 
  


Reply


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 08:48 AM
getline delimiter + move to next line? blizunt7 Programming 3 07-09-2005 12:08 AM
Issues calling sort from perl using pipe as delimiter amytys Programming 1 10-20-2004 09:35 PM
sed with delimiter, bourne uribo Programming 17 04-23-2003 01:42 AM
tab delimiter codename000 Programming 3 04-04-2003 10:18 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:38 PM.

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