Wednesday, May 20, 2020

Reading Numbers from a Text File and removing leading zeros Using C++

A program in C++ written by my friend Tom to read a series of numbers from a text file and removing leading zeros.

I am currently accepting programming work inventory system, enrollment system, accounting system, payroll system, information system, website design and development using WordPress, IT projects, school and application development, programming projects, thesis, and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me in the following email address for further details. If you want to advertise on my website kindly contact me also in my email address also. Thank you.

My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.My mobile number here in the Philippines is 09173084360. My telephone number at home here in Bacolod City, Negros Occidental Philippines is +63 (034) 4335675.

Here in Bacolod City, Negros Occidental I also accepting computer repair, web development using WordPress, Computer Networking, and Arduino Project development at a very affordable price. My personal website is http://www.jakerpomperada.com

My programming website is http://www.jakerpomperada.blogspot.comI am also a book author you can purchase my books on computer programming and information technology in the following links below.
https://www.unlimitedbooksph.com/


Program Listing

main.cpp

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>

using namespace std;

void process_line(string& line);
vector<string> process_lines(const std::string& filename);
void output_lines(std::vector<std::string> &lines);

int main()
{
   vector<string> lines = process_lines("input.txt");
   output_lines(lines);
}

vector<string> process_lines(const std::string& filename)
{
   vector<string> lines;
   string line;

   ifstream src(filename);
   while (getline(src, line))
   {
     process_line(line);
     lines.push_back(line);
   }
   return lines;
}

void process_line(string& line)
{
   istringstream iss(line);
   ostringstream oss;
   int num;

   while(iss >> num)
     oss << num << ' ';

   line = oss.str();
}

void output_lines(std::vector<std::string> &lines)
{
   cout << "Output numbers without leading zeros\n";
   for (const string& s : lines)
     cout << s << '\n';
}

input.txt

1 2 3 4 5 6 7 8 9 10
01 002 03 0004 005 006 007 00008 0009 010


No comments:

Post a Comment