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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:07:53+00:00 2026-06-09T21:07:53+00:00

#include <iostream> #include <fstream> #include <sstream> #include <string> #include <vector> using namespace std; int

  • 0
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

int main()
{
    ifstream inFile("test.txt");
    string line;

    while(getline(inFile, line))
    {
        istringstream meh(line);
        int n;
        vector<int> v;

        while(meh >> n)
            v.push_back(n);
    }
}

My test.txt file looks like:

429384
392041
230138
099938
243324

If I try to print v[0] I get the entire sequence of numbers back (42938…3324) instead of just the first number 4. Can anyone explain why this is happening?

  • 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-09T21:07:54+00:00Added an answer on June 9, 2026 at 9:07 pm

    You probably made a mistake when you output your data.Your code stores in fact only one int-value for each line – the whole line was stored inside v[0]. Then you wrote that whole number inside a stream and forgot to write a new line before your while-loop started to process the next line. Thus, your output was one large number.

    #include <iostream>
    #include <fstream>
    #include <sstream>
    #include <string>
    #include <vector>
    using namespace std;
    
    int main()
    {
        istringstream inFile("text.txt");
        string line;
        vector<vector<int> > vector_container;
    
        while(getline(inFile, line))
        {
            istringstream meh(line);
            char n;
            vector<int> v;
    
            while(meh >> n)
                v.push_back(static_cast<int>(n)-48);
    
            vector_container.push_back(v);
        }
        // this would output your numbers the way they were
        // stored inside your file
        for(int i = 0; i < vector_container.size(); ++i){
            for(int j = 0; j < vector_container[i].size(); ++j){
                 cout << vector_container[i][j];
            }
            cout << endl;
        }
    
    }
    

    This should work the way you expected it to be. You will have a vector<int> container inside a vector<vector<int> >. The first one will store your number in a line, whereas each character will be stored individually. The latter vector simply stores the vectors for the numbers for your lines. The char to int conversion should explain itself when you take a look at the ASCII-table.

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

Sidebar

Related Questions

#include<iostream> #include<fstream> #include<cstdlib> #include<iomanip> using namespace std; int main() { ifstream in_stream; // reads
Here's my code so far: #include<iostream> #include<string> #include<fstream> using namespace std; int main() {
#include <iostream> #include <sstream> #include <fstream> #include <iomanip> #include <string> using namespace std; int
#include <fstream> #include <iostream> #include <sstream> #include <string> #include <vector> using namespace std; typedef
#include <iostream> #include <string> #include <fstream> using namespace std ; string strWord( int index
#include <iostream> #include <fstream> using namespace std; int main () { ofstream myfile; myfile.open
I created this program: #include <iostream> #include <fstream> using namespace std; int main ()
#include<iostream> #include<time.h> #include<list> #include<stdlib.h> #include<fstream> using namespace std; typedef struct diskBtNode { int parent;
Here is my code: #include <iostream> #include <stdlib.h> #include <fstream> using namespace std; int
Given this code: #include <iostream> #include <vector> #include <fstream> #include <sstream> #include <boost/regex.hpp> using

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.