-3

I don't understand why my iterator(nr) doesn't increase.

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <string.h>
using namespace std;
ifstream f("date.in");
ofstream g("date.out");

int main()
{
    int l, nr = 0;
    char  x, s[100];
    f >> l;

    while(!f.eof())
    {
        f.getline(s, 100);
        {
            g << s;
            nr++;
        }
        if(nr == 19)
        {
            g << '\n';
            nr = 0;
        }
    }
    return 0;
}

I expect to get the output to start on a new line every 20 characters.

user4581301
  • 29,019
  • 5
  • 26
  • 45
  • 2
    Can we see an example from `int main()` please? It's probably due to the use of an uninitialised `nr` but I can't be sure. – Bathsheba Jul 04 '19 at 16:22
  • 3
    Unrelated to your problem, but please read [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) – Some programmer dude Jul 04 '19 at 16:24
  • 1
    As for your problem, do you remember to initialize `nr` to zero before the loop? – Some programmer dude Jul 04 '19 at 16:24
  • Yes, I did initialize, but it only goes to 3. I tried a lot of ways, working around that while loop and also not using the !eof() and many other things, but none of them worked out... – Banana Aurie Jul 04 '19 at 16:47
  • 2
    Don't tell us that you've initialized it. [Edit](https://stackoverflow.com/review/suggested-edits/23441741) the question and put the missing code in it. – Ted Lyngmo Jul 04 '19 at 16:51
  • 1
    You increment `nr` once per line, yet you expect `nr` to count characters up to 20. The code and your goals need to be reconciled. – nanofarad Jul 04 '19 at 17:21
  • Almost there @BananaAurie. It will still not compile, but it's getting close. – Ted Lyngmo Jul 04 '19 at 17:26
  • Placed all the code now, I couldn't post it with copy paste from CodeBlocks.. – Banana Aurie Jul 04 '19 at 17:26
  • Turbo C++, is it? – Ted Lyngmo Jul 04 '19 at 17:27
  • I am using CodeBlocks if that was your question :) – Banana Aurie Jul 04 '19 at 17:32
  • *I couldn't post it with copy paste from CodeBlocks* that is truly bizarre and worth further exploration. You should be able to copy and paste from Code::Blocks. – user4581301 Jul 04 '19 at 17:35
  • Be sure the file "date.in" is in the same directory as your executable (or the dir you set C::B to work for your app). If not, you're opening an empty flle, `f.eof()` is true and the loop exits. – Ripi2 Jul 04 '19 at 17:39
  • Suggestion: replace the output file with `cout` so you can see what's happening more easily. Also make use of a debugger so you can watch what happens as you do it. – user4581301 Jul 04 '19 at 17:42
  • @user4581301 It has to do with code formating. This site needs 4 spaces before any line to be taken as code. Copying and pasting from CodeBlocks set all the lines that are not 4 spaces from the first column to normal text. – Banana Aurie Jul 04 '19 at 17:45
  • @BananaAurie There's a `{ }` button over the editor. if you select all your code and click that button, it'll add 4 spaces. Another way is to place three backticks before and after your code section. Also read this: [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers) :-) – Ted Lyngmo Jul 04 '19 at 17:58
  • Step 1: paste code into question editor box. Step 2: select code. Step 3 press CTRL+K or click the `{}` button atop the editor box. Formatted. You can also surround the code with three backticks `[```your code here ```]`. Close enough – user4581301 Jul 04 '19 at 18:01

1 Answers1

0

The problem is that you read and count complete lines as @Andrey Akhmetov said in the comments. If you want to inject a \n every 20 chars, the easiest way would be to read one character at a time:

void add_newlines(std::istream& in, std::ostream& out) {
    char ch;
    int nr = 0;
    // Read one char with "<istream>.get()". The returned file descriptor (in) will
    // be true or false in a boolean context (the while(<condition>)) depending on
    // the state of the stream. If it fails extracting a character, the failbit will
    // be set on the stream and "in" will be "false" in the boolean context and
    // the while loop will end.
    while( in.get((ch)) ) {
        out.put(ch);
        if(++nr == 19) {
            out << '\n';
            nr = 0;
        }
    }
}

Call it with add_newlines(f, g);.

Note that get() and put() use Unformatted I/O while the out << '\n' uses Formatted output and will widen() \n to \r\n on Windows which probably could cause sequences like \r\r\n\n to appear in your output (if you run on Windows).

Ted Lyngmo
  • 37,764
  • 5
  • 23
  • 50
  • Thank you! I am really bad when it comes to using char functions. I understand now what I was doing wrong and what should've been done! Really appreciate it – Banana Aurie Jul 04 '19 at 17:40
  • @BananaAurie I appreciate your thanks too but please read [What should I do when someone answers my question?](https://stackoverflow.com/help/someone-answers). – Ted Lyngmo Aug 04 '19 at 23:28