-3

Below is my code;

int main(){
    ifstream infile;
    infile.open(fin);

    ofstream outfile;
    outfile.open(fout);

    char c; 
    int input_order = 0;
    string comp_str = "";
    vector <string> pfx_str;

    srand(time(NULL));

    if (infile.fail())
    {
        cout << "cannot open file!" << endl;
        return 0;
    }

    while (!infile.fail())
    {
        cout << input_order << endl;

        c = infile.get();

        if (c == '\n')
        {
            if (strcmp(comp_str.c_str(), "") != 0)
            {
                pfx_str.push_back(comp_str);
            }


            int num = rand() % pfx_str.size();

            while (num == 0)
            {
                num = rand() % pfx_str.size();
            }

            for (int i = 0; i < num; i++)
            {
                outfile << "/" << pfx_str.at(i);
            }
            outfile << "\n";

            input_order++;
            pfx_str.clear();
        }
        else if (c == '/')
        {
            if (comp_str != "")
            {
                pfx_str.push_back(comp_str);
            }
            comp_str = "";
        }
        else
        {
            comp_str = comp_str + c;
        }
    }

    infile.close();
    outfile.close();

    return 0;
}

For small set which consist of 10k inputs, it works. However, for big set such as using 1600k inputs, it prints out 00000, and does not work. What makes it happened? and how to make it correctly working? (Previously, I used this code for 1600k input and it works correctly....) In compile, I used g++ -std=gnu++0x ..... I googled this issue but could not find out the right answer.. And also I could not figure out what this issue comes from....

Thanks,

+ This code is for randomly cutting the input.

This is the example of 1 input set; (to show the input pattern) /aa/ab/bc/aaa/ Here, I consider 'aa', 'ab', 'bc', and 'aaa' as one component. And I want randomly cut this input as components unit. this is the brief step of the code; 1. generate the random number(except 0) 2. ex) I use the above input and the random number is 2. then I cut this input and only '2' components is left, which is /aa/ab/ (repeat this procedure for each inputs in input text file

=> this input; /aa/ab/bc/aaa/ (inside, it generate random number 2) output to be printed in output file; /aa/ab/

jjjhseo
  • 3
  • 2
  • 1
    Did you try stepping through your code with a debugger? – Algirdas Preidžius Mar 03 '18 at 23:22
  • 1
    Though not `eof()` but `fail()` is used, suspicious: [c++ - Why is iostream::eof inside a loop condition considered wrong? - Stack Overflow](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – MikeCAT Mar 03 '18 at 23:23
  • 4
    You could at least explain what your program should do. Give some example input/output. Not just paste code and write "it doesn't work". – ElChupacabra Mar 03 '18 at 23:24
  • @ElChupacabra this is the code for randomly cutting the input set. I add more for better understanding of my question – jjjhseo Mar 03 '18 at 23:47
  • @AlgirdasPreidžius yes, but I could not find any serious issues... – jjjhseo Mar 03 '18 at 23:54
  • @jjjhseo 1) You don't check if `pfx_str.size()` is 0, thus the `%` operator will fail if `pfx_str.size()` is empty. 2) Why are you using `strcmp`, and not just simply `if (!comp_str.empty())`? – PaulMcKenzie Mar 04 '18 at 00:03
  • 2
    @AlgirdasPreidžius *yes, but I could not find any serious issues.* -- The debugger has no idea what your program is supposed to do -- only *you* know that. So of course it won't say "your program is wrong". It is your responsibility to see if each variable, each function, each line of code, etc. when executed performs the correct operations as what you had in your design. Where in the code does the program deviate from your design? That is what is meant when we say "did you use the debugger". – PaulMcKenzie Mar 04 '18 at 00:11

1 Answers1

0

There is nothing wrong with your code.

I updated your code so that it compiles with g++:

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

using namespace std;

int main(){
    ifstream infile;
    infile.open("fin.txt"); // substituted a real file name in here to test

    ofstream outfile;
    outfile.open("fout.txt"); // ditto here

...the rest is the same as what you put above.

named it test.cpp, and compiled it with:

g++ -lm test.cpp -o test.exe

I wrote a Ruby script to make the input file according to the test set you specified in the comments:

#!/usr/bin/env ruby

File.open( 'fin.txt', 'w') do |f|

    1600000.times do
        f << "/aa/ab/bc/aaa/\n"
    end

end

I then ran the compiled program test.exe, and you totally owe me a beer for watching your line numbers go that high. This is time spent from my life for you that I will never get back.

and got an expected fout.txt with

/aa
/aa/ab
/aa
/aa/ab
/aa/ab/bc
/aa/ab/bc
/aa/ab
/aa/ab/bc
/aa/ab

etc.

My guess is that your system's constraints are causing a system fault. I ran this on a typical desktop machine with a ton of memory, etc. If you're running it on an embedded system, it may not have similar resources. At this point, I was tempted to test on a Raspberry Pi, but I'm burning too much time on this already. Just know that there is nothing wrong with your code.

In the future, try to figure out what your coding problem is. Stack Overflow is super forgiving if you've really tried and can't figure out the solution, but you need to show what you've tried and what happened as a result. For problems that started out like this, where you think that you're trying to figure out an algorithmic problem, always give the data set and the unexpected result that occurred.

Good luck!

Dribbler
  • 3,531
  • 6
  • 27
  • 47