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

  • Home
  • SEARCH
  • 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 6226435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T09:01:49+00:00 2026-05-24T09:01:49+00:00

This is a very strange issue. I’m trying to print a large text file,

  • 0

This is a very strange issue. I’m trying to print a large text file, it’s a Wikipedia entry. It happens to be the page on Velocity. So, when I tell it to print the file, it prints “In”, when it should print “In physics, velocity is etc, etc etc”.
Here’s the code I’m using to print out:

#include <iostream>
#include <fstream>

using namespace std;

    int main()
        {
        ifstream wiki;
        wiki.open("./wiki/velocity.txt");
        char* wikiRead;
        wiki >> wikiRead;
        cout << wikiRead << endl;
        wiki.close();
        }

Please help.

  • 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-05-24T09:01:49+00:00Added an answer on May 24, 2026 at 9:01 am
    wiki >> wikiRead;
    

    The default delimiter for stream is space, so when the stream encounters a space, it simply stops reading, that is why it reads only one word.

    If you want the stream to read all words, the you’ve to use a loop as:

    char* wikiRead = new char[1024]; //must allocate some memory!
    while(wiki >> wikiRead)
    {
       cout << wikiRead << endl;
    }
    wiki.close();
    delete []wikiRead; //must deallocate the memory
    

    This will print all the words in the file, each on a new line. Note if any of the word in the file is more than 1024 character long, then this program would invoke undefined behavior, and the program might crash. In that case, you’ve to allocate a bigger chunk of memory.

    But why use char* in the first place? In C++, you’ve better choice: Use std::string.

    #include<string>
    
    std::string word;
    while(wiki >> word)
    {
       cout << word << endl;
    }
    wiki.close();
    

    Its better now.

    If you want to read line-by-line, instead of word-by-word, then use std::getline as:

    std::string line;
    while(std::getline(wiki, line))
    {
       cout << line << endl;
    }
    wiki.close();
    

    This will read a complete line, even if the line contains spaces between the words, and will print each line a newline.

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

Sidebar

Related Questions

Recently, I encountered a very strange issue, this issue only happens in Windows XP
I have this very strange issue with my MVC 2 project. Often times, I'll
Very strange issue... I have something like this in my Controller: public ActionResult Initialize(IEnumerable<MyModel>
Having a very strange issue regarding postback in ASP.NET. I have page dynamically populating
Oh! this is very strange issue. I'm going to register these two types, but
I'm having a very strange issue, it looks like my application can't create file
I spent few hours trying to resolve one very strange issue, but I really
We had a very strange issue on our servers this week, where a method
I have this strange issue where anytime I open a XAML file that contains
I find this very strange, must be something I'm doing wrong, but still... I'm

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.