进程返回255< 0xFF>,c ++,程序停止工作

时间:2014-04-28 00:59:50

标签: c++ windows

我的c ++代码如下:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    ifstream fin("input.txt");
    ofstream fout("out.txt");

    string line;
    unsigned int number=0;
    int counter=0;
    while(fin>>line)
    {
        while(counter<=2)
        {
            if(line[number]=='/')
                counter++;
            number++;
        }
        for(int i=0;i<number;i++)
        {
            fout.put(line[i]);
        }
        fout.put('\n');
        number=0;
        counter=0;
        cin.clear();
    }

    cout<<"DONE!";
}

当我尝试运行它时,程序停止工作,可能导致此问题的原因是什么?没有无限循环,因为有很多&#39; /&#39; input.txt中的符号。程序输出文件,该文件不包含完整信息,只包含结果的一部分......

如果需要任何信息来解决此问题,我们将很乐意与您分享。

input.txt的样本:

http://www.ttsgs.com/page/51/
http://meshing.it/companies/61855-Granify
http://www.theglobeandmail.com/report-on-business/small.....
https://venngage.com/blog/index.php/page/5/
http://www.klasscapital.com/portfolio/granify
http://content.granify.com/why-ab-testing-is-not-enough
http://meetups.shopify.com/meetups/edmonton-shopify-meet-up
http://www.klasscapital.com/partners/jeff-lawrence
https://medium.com/startup-communities/81bb8f8ddfcb
http://freshit.net/blog/internet-marketing/chyortova-dy.....
http://www.higeek.cn/granify?????????.....
http://savepearlharbor.com/?paged=2557
http://www.sellerforum.de/small-talk-allgemeines-f1/irc.....
https://trango.co/preventing-abandoned-carts-using-ai/
http://www.imdevice.com/204602/
http://www.ifanr.com/news/page/17
http://www.webdesign-inspiration.com/web-designs/style/.....
http://worthyofnote.co.uk/tag/ecommerce/page/3/
http://www.siliconsolutions-inc.com/granify-raises-1-5-.....
http://crowdfundingnews.com/category/tech/page/425/
http://meetups.shopify.com/meetups/30

out.txt的样本:

http://www.ttsgs.com/
http://meshing.it/
http://www.theglobeandmail.com/
https://venngage.com/
http://www.klasscapital.com/
http://content.granify.com/
http://meetups.shopify.com/
http://www.klasscapital.com/
https://medium.com/
http://freshit.net/
http://www.higeek.cn/
http://savepearlharbor.com/
http://www.sellerforum.de/
https://trango.co/
http://www.imdevice.com/
http://www.ifanr.com/
http://www.webdesign-inspiration.com/
http://worthyofnote.co.uk/
http://www.siliconsolutions-inc.com/
http://crowdfundingnews.com/
http://meetups.shopify.com/
https://angel.co/
http://cdling.com/
http://www.sunwei.asia/
https://angel.co/

2 个答案:

答案 0 :(得分:1)

我重新构造了一下代码。首先,我将代码分开,从代码中读取和写入数据,以便根据需要修剪字符串。其次,我使用标准算法来处理大部分文件I / O.

代码看起来像这样:

#include <string>
#include <algorithm>
#include <vector>
#include <fstream>

struct trim {
    std::string operator()(std::string const &input) { 
        unsigned pos = 0;
        for (int i=0; i<3; i++)
            pos = input.find('/', pos+1);
        return std::string(input, 0, pos+1);
    }
};

int main() {
    std::ifstream in("input.txt");
    std::ofstream out("output.txt");

    std::transform(std::istream_iterator<std::string>(in),
        std::istream_iterator<std::string>(),
        std::ostream_iterator<std::string>(out, "\n"),
        trim());
}

请注意,这取决于URL不应包含任何空格的事实。如果您的输入确实包含除分隔行的换行符之外的空格,那么您还需要查看a previous question关于如何一次迭代行的答案。虽然专门针对std::cin编写,但相同的原则基本上适用于任何输入流。

无论如何,对于您的示例输入,此代码会生成以下输出:

http://www.ttsgs.com/
http://meshing.it/
http://www.theglobeandmail.com/
https://venngage.com/
http://www.klasscapital.com/
http://content.granify.com/
http://meetups.shopify.com/
http://www.klasscapital.com/
https://medium.com/
http://freshit.net/
http://www.higeek.cn/
http://savepearlharbor.com/
http://www.sellerforum.de/
https://trango.co/
http://www.imdevice.com/
http://www.ifanr.com/
http://www.webdesign-inspiration.com/
http://worthyofnote.co.uk/
http://www.siliconsolutions-inc.com/
http://crowdfundingnews.com/
http://meetups.shopify.com/

答案 1 :(得分:-2)

表达式:

  

鳍&GT;&GT;线

由于ifstream::operator>>返回ifstream&amp ;;而while条件中的

可能不会返回false如果设置了failbit或badbit中的至少一个,那么ifstream test可能会返回true(我不认为是这种情况),那么你应该有一个无限循环。