C++ g++;印刷品不受欢迎,为什么?

C++ g++;印刷品不受欢迎,为什么?,c++,C++,下面是我的代码 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.

下面是我的代码

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;
}
intmain(){
河流充填;
填充开放式(fin);
出流孔的直径;
开放式(fout);
字符c;
int input_order=0;
字符串comp_str=“”;
载体pfx_-str;
srand(时间(空));
if(infle.fail())
{
你的代码没有问题

我更新了您的代码,以便使用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
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
河流充填;
infle.open(“fin.txt”);//在此处替换了一个真实的文件名以进行测试
出流孔的直径;
outfile.open(“fout.txt”);//此处同上
…剩下的和你上面写的一样

将其命名为test.cpp,并使用以下代码进行编译:

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

我编写了一个Ruby脚本,根据您在注释中指定的测试集生成输入文件:

#!/usr/bin/env ruby

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

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

end
!/usr/bin/env ruby
打开('fin.txt','w')do | f|
1600000.00倍

f您是否尝试过使用调试器单步执行代码?虽然没有使用
eof()
,但使用了
fail()
,但很可疑:您至少可以解释您的程序应该做什么。给出一些输入/输出示例。不仅仅是粘贴代码并编写“它不工作”@ElChupacabra这是随机剪切输入集的代码。为了更好地理解我的question@AlgirdasPreidžius是的,但我找不到任何严重的问题。--调试器不知道你的程序应该做什么——只有你知道。所以它当然不会说“你的程序错了”。您有责任查看每个变量、每个函数、每行代码等在执行时是否执行了与您的设计相同的正确操作。程序在代码中与您的设计有何不同?这就是我们所说的“您是否使用了调试器”的意思。