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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:42:49+00:00 2026-06-12T18:42:49+00:00

I need to read a .dat file which looks like this: Atask1 Atask2 Atask3

  • 0

I need to read a .dat file which looks like this:

Atask1 Atask2 Atask3 Atask4 Atask5
Btask1 Btask2 Btask3 Btask4 Btask5
Ctask1 Ctask2 Ctask3 Ctask4 Ctask5
Dtask1 Dtask2 Dtask3 Dtask4 Dtask5

and i need to be able to output information like this:

cout << line(3) << endl; // required output shown below
>>Ctask1 Ctask2 Ctask3 Ctask4 Ctask5

cout << line(2)(4) << endl; // required output shown below
>>Btask4

I don’t know how to read 1 line and split it into an array of 5 different strings.
I’d ideally like to have the whole .dat file converted into a vector or a list or some kind of matrix/array structure for easy reference

any simple code or solutions for this??

PLEASE HELP?!?!?!? 🙂

EDIT:

vector<string> dutyVec[5];

dut1.open(dutyFILE);

if( !dut1.is_open() ){
    cout << "Can't open file " << dutyFILE << endl;
    exit(1);
}

    if(dut1.eof()){
    cout << "Empty file - no duties" << endl;
            exit(1);
    }

while ( !dut1.eof()){
    int count = 0;
    getline(dut1, dutyVec[count]);
    count++;
}
  • 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-12T18:42:50+00:00Added an answer on June 12, 2026 at 6:42 pm

    Your problem addresses a number of issues, all of which I will attempt to answer in one go. So, forgive the length of this post.

    #include <iostream>
    #include <sstream>
    #include <string>
    #include <vector>
    #include <fstream>
    
    int main(int argc, char argv[]){
      std::vector <std::string> v;//just a temporary string vector to store each line
    
      std::ifstream ifile;
      ifile.open("C://sample.txt");//this is the location of your text file (windows)
    
      //check to see that the file was opened correctly
      if(ifile.is_open()) { 
        //while the end of file character has not been read, do the following:
        while(!ifile.eof()) {
          std::string temp;//just a temporary string
          getline(ifile, temp);//this gets all the text up to the newline character
          v.push_back(temp);//add the line to the temporary string vector
        }
        ifile.close();//close the file
      }
    
      //this is the vector that will contain all the tokens that
      //can be accessed via tokens[line-number][[position-number]
      std::vector < std::vector<std::string> > tokens(v.size());//initialize it to be the size of the temporary string vector
    
      //iterate over the tokens vector and fill it with data
      for (int i=0; i<v.size(); i++) {
        //tokenize the string here:
        //by using an input stringstream
        //see here: http://stackoverflow.com/questions/5167625/splitting-a-c-stdstring-using-tokens-e-g
        std::istringstream f(v[i].c_str());
    
        std::string temp;
    
        while(std::getline(f, temp, ' ')) {
          tokens[i].push_back(temp);//now tokens is completely filled with all the information from the file
        }
      }
      //at this point, the tokens vector has been filled with the information
      //now you can actually use it like you wanted:
      //tokens[line-number][[position-number]
    
      //let's test it below:
    
      //let's see that the information is correct
      for (int i=0; i<tokens.size(); i++) {
        for(int j=0; j<tokens[i].size(); j++) {
          std::cout << tokens[i][j] << ' ';
        }
        std::cout << std::endl;
      }
    
      system("pause");//only true if you use windows. (shudder)
      return 0;
    }
    

    Note, I did not use iterators, which would have been beneficial here. But, that’s something I think you can attempt for yourself.

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

Sidebar

Related Questions

I have a flat CSV file which I need read from to end up
I have a file called points.dat which reads something like: 5 2 5 -1
I need to read test.TXT file(tab delimited) into MATLAB. TXT file have form: Datum
I need to read 16 bits from the binary file as std::string or char
I need to read a file line by line, and change a variable accordingly.
I need to read data from XML to a List<>. The XML file contains
I need to read the size of a file but the server is forcing
I need to read and write some data on .mdb Access file and over
I need to read in an expression from a file using a string stream
H, How to Read First 512 Bytes of data from a .dat file in

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.