0

I have a text file with some information that i need to format and withdraw in the same text file but with extension out. Only criteria of formatting is to have maximum 80 symbols in every row. I thought that it might be realised by creating a counter that will read words from text and by the last symbol of every word will start counting till 80. Thus,if the count reach 80 it moves the text and the word that exceeded the limit to the next row and the counter is reset to zero . Unfortunaly , i'm still at "baby steps" in programming so i don't know how to make it or if it is even the right approach for the problem . Can you help me?

Any other ideas or ways to do it are welcomed. That's the code i have :

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
string line;
int sum=0;
ifstream inData ;
inData.open("input.txt");
ofstream outFile("output.txt");
while(!inData.eof())
{
getline(inData,line);
    int numofChars= line.length();
    for (unsigned int n = 0; n<line.length();n++)
    {
    if (line.at(n) == ' ')
    {
    numofChars--;
    }
    }
sum=numofChars+sum;
}
outFile << sum << endl;
    return 0 ;
}

It's counting the characters in the text file but i can't get how to make it if the count reaches 80 text gets to the next row and counting starts again

tyler0504
  • 21
  • 5
  • If you "don't know how to make it," are you asking on StackOverflow so that someone will design and write the code for you? SO is not a code writing service, unfortunately. If not, can you clarify what you're looking for here? – scohe001 Dec 28 '20 at 19:37
  • 1
    Basically you need a loop, which reads words from a file and writes the same words to another file. I wouldn't try anything more than that to start with (even that might a bit too much if you are a beginner). Once you have that part working, all you need to add is to output new lines between some of the words, so you get the formatting you want. Your idea of a counter seems a reasonable approach. The key is to remember not to try to solve the whole problem in one go, take baby steps like you said and get there gradually. – john Dec 28 '20 at 19:41
  • I updated the question with the code that i have , maybe u know how i can edit it to get it done – tyler0504 Dec 28 '20 at 19:44
  • Just looking at your code, it's the wrong approach. You are going to find this **much** easier if you read words not lines. Do you know how to read words? If you read lines then you are going to have to split them up, which is more complicated. – john Dec 28 '20 at 19:44
  • @john that assumes he can remove white space. – Martin York Dec 28 '20 at 19:46
  • @john Sincerely, i don't know how to read words – tyler0504 Dec 28 '20 at 19:46
  • Sidenote: [Why "using namespace std;" is considered bad practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Nikita Demodov Dec 28 '20 at 19:47
  • 1
    @tyler0504 Like this `string word; ... inData >> word;` Where a word is considered to be any sequence of non-whitespace characters. Like Martin said I am assuming you can just ignore the spaces that are in your input file. If not then you have a more complicated problem. – john Dec 28 '20 at 19:47
  • As the specs do not require to keep whole words, I would suggest to read 80 characters in a row, skipping the newlines, and write them as a line. –  Dec 28 '20 at 19:48
  • See also: [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – Martin York Dec 28 '20 at 19:49
  • I missed to mention but the words must be kept whole , so we can't divide it – tyler0504 Dec 28 '20 at 19:58

1 Answers1

1

To give you an idea, something along these lines should be helpful:

#include <iostream>
#include <fstream>
#include <string>

int main() {
    std::fstream fIn;
    std::fstream fOut;

    fIn.open("data.input",  std::ios::in);
    fOut.open("data.output", std::ios::out | std::ios::trunc);
    std::string word;
    size_t cnt = 0;

    while(fIn >> word) 
    {
        cnt += word.size() + 1;
        if(cnt <=80){
            fOut << word << ' ';
        }
        else {
            fOut << '\n' << word << ' ';
            cnt = word.size() + 1;
        }
    }
    fIn.close();
    fOut.close();
    return 0;}
Tarun
  • 76
  • 7