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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:37:12+00:00 2026-05-26T07:37:12+00:00

I have a text file, that is formatted somewhat like this: 1 3 4

  • 0

I have a text file, that is formatted somewhat like this:

1 3 4 5 6
6 7 8
4 12 16 17 18 19 20
20
0

A line can contain 1 to 10000 integers. What I need to do, is read all of them line by line.

Pseudocode like this:

line=0;
i=0;
while(!file.eof()){
 while(!endLine){

 array[0][i++]=file.readChar();
 }
line++;i=0;
}

So, I have an array , into which I would like to read every line, and each line would consist of each of these integers.

The problem I’m having, is how to check if the end of a line has come.

Note, I can’t use strings.

Yes, This is for a homework, but the main task for the assignment is to build a tree and then transform it. I can do that, but I’ve no idea how to read the integers from the file.

  • 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-26T07:37:13+00:00Added an answer on May 26, 2026 at 7:37 am

    Probably something like this:

    after reading an int, I manually skip spaces, tabs, carriage return and end of line (for this one you’ll have to implement your logic).
    To read an int I read it directly using the C++ functions of ifstream. I don’t read it character by character and then recompose it as a string 🙂
    Note that I skip \r as “spaces. The end of line for me is \n.

    #include <iostream>
    #include <fstream>
    #include <vector>
    
    int main() 
    {
        std::ifstream file("example.txt");
    
        std::vector<std::vector<int>> ints;
    
        bool insertNewLine = true;
    
        int oneInt;
    
        //The good() here is used to check the status of 
        //the opening of file and for the failures of
        //peek() and read() (used later to skip characters).
        while (file.good() && file >> oneInt)
        {
            if (insertNewLine)
            {
                std::vector<int> vc;
                ints.push_back(vc); 
    
                //With C++11 you can do this instead of the push_back
                //ints.emplace_back(std::vector<int>());
    
                insertNewLine = false;
            }
    
            ints.back().push_back(oneInt);
    
            std::cout << oneInt << " ";
    
            int ch;
    
            while ((ch = file.peek()) != std::char_traits<char>::eof())
            {
                if (ch == ' '|| ch == '\t' || ch == '\r' || ch == '\n')
                {
                    char ch2;
    
                    if (!file.read(&ch2, 1))
                    {
                        break;
                    }
    
                    if (ch == '\n' && !insertNewLine)
                    {
                        std::cout << std::endl;
                        insertNewLine = true;
                    }
                }
                else
                {
                    break;
                }
            }
        }
    
        //Here we should probably check if we exited for eof (good)
        //or for other file errors (bad! bad! bad!)
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a text file with hundreds of entries formatted like this, I need
Say I have a text file formatted like this: 100 20 the birds are
I have a text file that is formatted like JSON, but in a print/view
I have a text file that looks a bit like: random text random text,
I have a text file that might contain thousands and thousands of numbers(0-9 -->
I have a text file that I want to send over the network, this
I have a text file formatted so that there are 3 numbers, separated by
Let's say I have a binary file that is formatted like [unsigned int(length of
I have an html table that is populated from a text file, formatted and
I have memory mapped a large formatted (text) file containing one integer per line

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.