Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/152.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
进程返回255 ,C++,程序停止工作 我的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!"; }_C++_Windows - Fatal编程技术网

进程返回255 ,C++,程序停止工作 我的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!"; }

进程返回255 ,C++,程序停止工作 我的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!"; },c++,windows,C++,Windows,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://fr

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/
表达方式:

fin>>线路


在while内,条件可能不会返回false,因为返回ifstream&并且如果至少设置了failbit或badbit中的一个,则可能返回true。我认为情况并非如此,那么您应该有一个无限循环。

我将稍微重新构造代码。首先也是最重要的一点,我将把读写数据的代码与修剪字符串的代码分开。其次,我会使用标准算法来处理大部分文件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());
}

在您的代码中,您实际上没有检查您读入的字符串的长度。假设它至少有2个字符,但这并不能解决问题:我对代码和示例输入文件没有问题。您在什么平台上运行此操作?将第[number]行更改为line.atnumber,并将第[i]行更改为line.ati。这将使您的程序在出现边界错误时更加优雅地失败。没有任何其他因素会导致此程序崩溃。在使用ifstream时,您建议在while语句中写入什么内容?[在这种情况下,while!fin.eof不是最佳选择]我建议您测试while fin.good,它将显示是否设置了任何不良状态。然后您将确信可以继续读取该文件。@lgabriellp:这几乎是最糟糕的建议。你也完全错误地认为fin>>行永远不会返回false。对不起,@JerryCoffin,你是对的。如果出现错误读取错误badbit set或逻辑错误failbit set,则返回false。我的意思是,在这种情况下,ifstreamapi的错误使用导致了错误,而不是读取或逻辑错误。我将编辑我的答案。@lgabriellp:虽然你的答案现在没有那么错,但基本上还是错的。事实上,虽然fin>>行是一种完全可以接受的做事方式,而fin.good通常会导致问题。他可能有一些问题,但这不是问题的根源。
#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());
}
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/