0

I'm trying to write a program that copies (from an input.txt file) 30 lines to one file, then loops and copies the next 30 lines to a second file, then the next 30 to a 3rd file, etc. etc. This is what I wrote to try and make that happen:

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

using namespace std;

int main()
{
    // initialize
    ifstream input("input.txt");
    string line;
    int i = 0;
    int fileNum = 0;
    cout << "Initialized";

    // Loop through text file and write to new files
    while (getline(input, line) && i < 30)
    {
        // Write to File
        ofstream outFile("frames/frame" + to_string(fileNum) + ".mcfunction");

        // Write Lines
        outFile << "data modify block " << (i + 1) << " -48 20 HeldItem.Item.tag.display.Name set value '{\"text\":\"" << line << "\"}'" << endl;

        // Increment Counters
        i++;
        if (i == 30)
        {
            //  Add Schedule and Scoreboard Commands to Bottom of Each File
            outFile << "scoreboard players set Frame: BadApple " + to_string(fileNum) << endl;
            outFile << "schedule frame" + to_string(fileNum + 1) + ".mcfunction 5s" << endl;

            // Reset Counter & File Number
            i = 0;
            fileNum++;
            cout << i << " ";

        }
    }

    return 0;
}

But it only ends up copying one line from the input file to each document. Everything seems to be working aside from that.

Any help would be much appreciated, I've been trying to debug this for a while now but as a naive beginner, I haven't gotten very far. Thank you all in advance.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
Alexander
  • 33
  • 6

2 Answers2

3

The problem is that you are opening and closing the output file once per line, instead of once every 30 lines.

I suggest that you restructure your program to use a nested loop. The outer loop should handle one output file (i.e. 30 lines) per loop iteration, and the inner loop should handle a single line per loop iteration.

Here is an example:

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

int main()
{
    std::ifstream input( "input.txt" );
    std::string line;
    int fileNum = 0;
    bool finished = false;

    std::cout << "Initialized\n";    

    //process one file per loop iteration
    for ( int fileNum = 0; !finished; fileNum++ )
    {
        //open new output file
        std::ofstream outFile("frames/frame" + std::to_string(fileNum) + ".mcfunction");

        //process one line per loop iteration
        for ( int i = 0; i < 30; i++ )
        {
            if ( !std::getline(input, line) )
            {
                finished = true;
                break;
            }

            //write lines
            outFile << "data modify block " << (i + 1) << " -48 20 HeldItem.Item.tag.display.Name set value '{\"text\":\"" << line << "\"}'\n";
        }

        //add schedule and scoreboard commands to bottom of each file
        outFile << "scoreboard players set Frame: BadApple " + std::to_string(fileNum) << '\n';
        outFile << "schedule frame" + std::to_string(fileNum + 1) + ".mcfunction 5s\n";
    }

    return 0;
}

Note that in my code above, I have removed using namespace std;, for the reason described here:

Why is "using namespace std;" considered bad practice?

Also, you should generally only use std::endl if you want to actually flush the buffer. Otherwise, you can simply print "\n", which is better for performance. See the following question for further information:

"std::endl" vs "\n"

I have therefore removed std::endl from the program.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
2

You keep reopening the file on every iteration of the loop. So then it keeps overwriting the first line.

Taylee
  • 133
  • 6
  • Alright, that makes sense. I guess I misunderstood how the opening works. So where should I place the line? – Alexander Dec 17 '22 at 02:26
  • 1
    You will have to change how you handle the new file name every 30 lines. – drescherjm Dec 17 '22 at 02:28
  • How? What? I don't get it. – Alexander Dec 17 '22 at 02:28
  • @AlexanderWeimer Now that you know what's causing the problem this is a prime opportunity for one of two things to happen. You can sit down and work through it to identify a solution using what you've already learned (i.e. you know how to do `while `loops and open files) --or-- Tayle can update their answer to provide a possible solution. – Captain Obvlious Dec 17 '22 at 02:30
  • Ok so I figured it out--the reason I was still confused because I somehow managed to misunderstand "overwrite the first line" as "overwrite the whole file", even though it was clearly not that. I didn't notice the 'endl' part. Thank you so, so much both of you. – Alexander Dec 17 '22 at 02:38