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 438207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:39:19+00:00 2026-05-12T20:39:19+00:00

I would like to read a text file and input its contents into an

  • 0

I would like to read a text file and input its contents into an array. Then I would like to show the contents of the array in the command line.

My idea is to open the file using:

inFile.open("pigData.txt")

And then to get the contents of the file using:

inFile >> myarray [size]

And then show the contents using a for loop.

My problem is that the file I am trying to read contain words and I don’t know how to get a whole word as an element in the array. Also, let’s say that the words are divided by spaces, thus:

hello goodbye

Could be found on the file. I would like to read the whole line “hello goodbye” into an element of a parallel array. How can I do that?

  • 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-12T20:39:19+00:00Added an answer on May 12, 2026 at 8:39 pm

    For context, you could have provided a link to your previous question, about storing two lists of words in different languages. There I provided an example of reading the contents of a text file into an array:

    const int MaxWords = 100;
    std::string piglatin[MaxWords];
    int numWords = 0;
    std::ifstream input("piglatin.txt");
    std::string line;
    while (std::getline(input, line) && numWords < MaxWords) {
      piglatin[numWords] = line;
      ++numWords;
    }
    if (numWords == MaxWords) {
      std::cerr << "Too many words" << std::endl;
    }
    

    You can’t have one parallel array. For something to be parallel, there must be at least two. For parallel arrays of words, you could use a declarations like this:

    std::string piglatin[MaxWords];
    std::string english[MaxWords];
    

    Then you have two options for filling the arrays from the file:

    1. Read an entire line, and the split the line into two words based on where the first space is:

      while (std::getline(input, line) && numWords < MaxWords) {
        std::string::size_type space = line.find(' ');
        if (space == std::string::npos)
          std::cerr << "Only one word" << std::endl;
        piglatin[numWords] = line.substr(0, space);
        english[numWords] = line.substr(space + 1);
        ++numWords;
      }
      
    2. Read one word at a time, and assume that each line has exactly two words on it. The >> operator will read a word at a time automatically. (If each line doesn’t have exactly two words, then you’ll have problems. Try it out to see how things go wrong. Really. Getting experience with a bug when you know what the cause is will help you in the future when you don’t know what the cause is.)

      while (input && numWords < MaxWords) {
        input >> piglatin[numWords];
        input >> english[numWords];
        ++numWords;
      }
      

    Now, if you really one one array with two elements, then you need to define another data structure because an array can only have one “thing” in each element. Define something that can hold two strings at once:

    struct word_pair {
      std::string piglatin;
      std::string english;
    };
    

    Then you’ll have just one array:

    word_pair words[MaxWords];
    

    You can fill it like this:

    while (std::getline(input, line) && numWords < MaxWords) {
      std::string::size_type space = line.find(' ');
      if (space == std::string::npos)
        std::cerr << "Only one word" << std::endl;
      words[numWords].piglatin = line.substr(0, space);
      words[numWords].english = line.substr(space + 1);
      ++numWords;
    }
    

    Notice how the code indexes into the words array to find the next word_pair object, and then it uses the . operator to get to the piglatin or english field as necessary.

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

Sidebar

Related Questions

I would like to load the contents of a text file into a vector<char>
I would like to know how can i read a value of input text
I would like to read an input file in C++, for which the structure
I would like to read text file in PHP which break or exit after
I would like to read the text and binary attachments in a saved Outlook
I would like to write a function which will read values from a text
I would like to read a DICOM file in C#. I don't want to
I would like to read the last 1 megabyte of a MP3 file and
I would like to do something like this: std::wistream input = std::wifstream(text); if (!input)
I would like to extract some text from an html file using Regex. I

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.