How can i cut out every 4 characters from a text file?

Example: my text file rows of binary numbers like: 101010101000010101010111
i wanted to cut out every 4 characters from it becomes:
1010
1010
1000
0101
....
....
.....

i have been bale to cut out the 4 characters but is looping the 4 character for the length of the row...

string line, bits;
while(getline(fin,line))
    {    
         for(int i=0; i<line.length(); i++)
         {
               for(int n=0; n<4; n++)
               {
                       bits = line[n];
                       fout << bits;
               }fout<<endl;
         }
    }

Recommended Answers

All 6 Replies

Here's how Ed would do it:

#include <iostream>
#include <string>

int main()
{
  std::string line;

  std::getline(std::cin, line);

  for (int i = 0; i < line.length(); i++) {
    std::cout << line[i];

    if ((i + 1) % 4 == 0)
      std::cout << '\n';
  }
}

If the value of the index is evenly divisible by 4, you can print a newline and that should give you 4 character blocks. 0 makes things tricky, so Edward avoids that problem by adding 1 before taking the remainder.

thanks edward!!! it works!!!

The codes works fine!
But now i have another problem, my text file contain rows of binary bits. if the row of the binary bits cannot be modula by 4, it will take that previous bits add to the new row bit.

example: my first row got 010100101001010
my 2nd row got 010100101001110

so it is add my first row reminder 010 with 2nd row 0

wat i wanted is that it if the row binary wif the bits then add a 0 to it. so the 010 becomes 0100 -> adding a 0 to it. how m i going to do it?

>>wat i wanted is that it if the row binary wif the bits
Stop writing like this is some chat room. It isn't. Spell out the words.

You are not going to add any 0's Its already there in the text file isnt it, just skip '\n' in a if statement & read the file char by char. use mod like ed did & the code will be fine

You can edit Edward's code slightly so that it reads multiple lines:

#include <iostream>
#include <string>
#include <sstream>

int main()
{
  std::istringstream iss("010100101001010\n010100101001110");
  std::string line;
  int k = 1;

  while (std::getline(iss, line)) {
    for (int i = 0; i < line.length(); i++) {
      std::cout << line[i];

      if (k++ % 4 == 0)
        std::cout << '\n';
    }
  }
}

This time Ed used a stringstream to represent your file stream instead of cin because it's easier to get the automated handling of a newline that way. Just replace iss with your ifstream object.

One problem with this approach is that you risk overflowing the value of k if the file contains more than INT_MAX characters. You can get around that by resetting k to 0 after it reaches 4 instead of using a modulus operation:

#include <iostream>
#include <string>
#include <sstream>

int main()
{
  std::istringstream iss("010100101001010\n010100101001110");
  std::string line;
  int k = 0;

  while (std::getline(iss, line)) {
    for (int i = 0; i < line.length(); i++) {
      std::cout << line[i];

      if (++k == 4) {
        std::cout << '\n';
        k = 0;
      }
    }
  }
}

This one is closer to what you were trying to do originally, but it matches the effect of using modulus with an unbounded index.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.