Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7863673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T23:33:22+00:00 2026-06-02T23:33:22+00:00

In my C++ code, I want to read from a text file (*.txt) and

  • 0

In my C++ code, I want to read from a text file (*.txt) and tokenize every entry. More specifically, I want to be able to read individual words from a file, such as “format”, “stack”, “Jason”, “europe”, etc.

I chose to use fstream to perform this task, and I do not know how to set it’s delimiter to the ones I want to use (space, \n, as well as hyphens and even apostrophes as in “Mcdonal’s”). I figured space and \n are the default delimiters, but hyphens are not, but I want to treat them as delimiters so that when parsing the file, I will get words in “blah blah xxx animal–cat” as simply “blah”, “blah”, “xxx”, “animal”, “cat”.

That is, I want to be able to get two strings from “stack-overflow”, “you’re”, etc, and still be able to maintain \n and space as delimiters at the same time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T23:33:25+00:00Added an answer on June 2, 2026 at 11:33 pm

    An istream treats “white space” as delimiters. It uses a locale to tell it what characters are white space. A locale, in turn, includes a ctype facet that classifies character types. Such a facet could look something like this:

    #include <locale>
    #include <iostream>
    #include <algorithm>
    #include <iterator>
    #include <vector>
    #include <sstream>
    
    class my_ctype : public
    std::ctype<char>
    {
        mask my_table[table_size];
    public:
        my_ctype(size_t refs = 0)  
            : std::ctype<char>(&my_table[0], false, refs)
        {
            std::copy_n(classic_table(), table_size, my_table);
            my_table['-'] = (mask)space;
            my_table['\''] = (mask)space;
        }
    };
    

    And a little test program to show it works:

    int main() {
        std::istringstream input("This is some input from McDonald's and Burger-King.");
        std::locale x(std::locale::classic(), new my_ctype);
        input.imbue(x);
    
        std::copy(std::istream_iterator<std::string>(input),
            std::istream_iterator<std::string>(),
            std::ostream_iterator<std::string>(std::cout, "\n"));
    
        return 0;
    }
    

    Result:

    This
    is
    some
    input
    from
    McDonald
    s
    and
    Burger
    King.
    

    istream_iterator<string> uses >> to read the individual strings from the stream, so if you use them directly, you should get the same results. The parts you need to include are creating the locale and using imbue to make the stream use that locale.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want to read the text from the file in raw directory(rules.txt) file and
I want to read a text file called config.txt from my application's bundle and
I am using following code to read text from a .txt file. But I
I want to read the string after delimiter from a text file in Objective-C.i'm
I'm want to read a simple string from a text file which is around
there is a text file which we read from it , then we want
I'm working with a .txt file. I want a string of the text from
I wrote some code to read a text file from C drive directly given
I'm programming Java in Eclipse IDE. Here is code I want to read file:
I want to read a text file word by word in Qt4 (to which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.