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

The Archive Base Latest Questions

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

I am wondering how to go through a text file and locate a given

  • 0

I am wondering how to go through a text file and locate a given word (“foobar”) at different locations within each line but then realign the word to the same position in a new text file, let me know if this doesn’t make sense.

***in text file***
1 foobar baz
2  foobar baz
3   foobar baz

****out text file***
1     foobar baz
2     foobar baz
3     foobar baz
  • 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-12T04:19:44+00:00Added an answer on June 12, 2026 at 4:19 am

    The io manipulator std::setw() from can be used to create fixed-length columns in text output, and std::setfill() is used to specify the fill character:

    std::cout << std::setw(5) << std::setfill('0') << 5 << std::endl;
    

    Will print:

    00005
    

    This can easily be used to create a small program which reads all lines from one file and writes them to another file and at the same time aligns all columns (in the below program >> is used to read one column, which means that the columns in the in file are supposed space-separated, by one or more whitespace characters):

    #include <iostream>
    #include <iomanip>
    #include <vector>
    #include <fstream>
    #include <map>
    #include <algorithm>
    
    int main (int argc, char* arv[])
    {
       using namespace std;
    
       std::vector<std::vector<std::string> > records;
       std::map<int, int> column_widths;
    
       std::ifstream in_file("infile.txt", std::ios::text);
       if (!in_file.is_open())
           return 1;
    
       std::ofstream out_file("outfile.txt", std::ios::text);
       if (!out_file.is_open())
           return 2;
    
       // read all the lines and columns into records
       std::string line;
       while (std::getline(in_file, line)) {
           std::istringstream is(line);
           std::vector<std::string> columns;
           std::string word;
           int column_index = 0;
           while (is >> word) {
               columns.push_back(word);
               column_widths[column_index] = std::max(column_width[column_index], word.length());
               ++column_index;
           }
    
           records.push_back(columns);
       }
    
       // now print all the records and columns with fix widths
       for (int line = 0; line < records.size(); ++line) {
           const std::vector<std::string>& cols = records[line]; 
           for (int column = 0; column < cols.size(); ++column) {
               out_file << std::setw(column_widths[column])
                        << std::setfill(' ')
                        << cols[column] << ' ';
           }
           out_file << "\n";
       }
    
       return 0;
    }
    

    I didn’t compile the program but it should work :).

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

Sidebar

Related Questions

I am crunching through many gigabytes of text data and I was wondering if
I'm new to using iterators and was wondering how one would iterate through each
I have a simple text file that is ~150mb. My code will read each
I was wondering if there was any way through C# or C++ to send
I'm going through the django tutorials and I was wondering what happens when you
I was wondering if you could protect xml files through forms authentication in ASP.NET
I am wondering if maybe ReSharper is able to run through every class and
I am wondering what is the best way to go iterate through all the
I was going through the RMI documents and was wondering if it is possible
I'm working through the NerdDinner MVC tutorial and came across this and was wondering

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.