Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.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
C++;用于在.c文件中查找和修改/替换字符串的代码 我的任务是在.c文件中搜索字符串,并用C++代码修改它。我一直在搜索字符串,但修改它会产生错误。如果我将c文件的内容复制到文本文件并尝试修改它,则会出现相同的错误。所以我确信我的代码有问题。我是初学者,请帮忙。提前谢谢。 我的代码: #包括 #包括 #包括 使用名称空间std; int main() { 字符串s1、s2; ifstream测试(“test.csv”); while(test.eof()==0)//test.eof()如果未到达文件结尾,则返回0 { getline(test,s1,,);//读取整行(行),直到“,”到s1为止 getline(测试,s2,'\n'); cout_C++_C_File Handling - Fatal编程技术网

C++;用于在.c文件中查找和修改/替换字符串的代码 我的任务是在.c文件中搜索字符串,并用C++代码修改它。我一直在搜索字符串,但修改它会产生错误。如果我将c文件的内容复制到文本文件并尝试修改它,则会出现相同的错误。所以我确信我的代码有问题。我是初学者,请帮忙。提前谢谢。 我的代码: #包括 #包括 #包括 使用名称空间std; int main() { 字符串s1、s2; ifstream测试(“test.csv”); while(test.eof()==0)//test.eof()如果未到达文件结尾,则返回0 { getline(test,s1,,);//读取整行(行),直到“,”到s1为止 getline(测试,s2,'\n'); cout

C++;用于在.c文件中查找和修改/替换字符串的代码 我的任务是在.c文件中搜索字符串,并用C++代码修改它。我一直在搜索字符串,但修改它会产生错误。如果我将c文件的内容复制到文本文件并尝试修改它,则会出现相同的错误。所以我确信我的代码有问题。我是初学者,请帮忙。提前谢谢。 我的代码: #包括 #包括 #包括 使用名称空间std; int main() { 字符串s1、s2; ifstream测试(“test.csv”); while(test.eof()==0)//test.eof()如果未到达文件结尾,则返回0 { getline(test,s1,,);//读取整行(行),直到“,”到s1为止 getline(测试,s2,'\n'); cout,c++,c,file-handling,C++,C,File Handling,您面临的错误不清楚,但我可以看到一个大问题,您在空字符串上运行replace 您的代码: string str; search=str; str.replace(str.begin()+25,str.begin()+31,"=s2 //"); 您创建str(默认情况下初始化为空字符串),将其分配给search(因此该字符串变为空),然后调用replace,尝试将字符25更改为31,因为str为空,因此不存在该字符 更新 可能您需要修复替换,但是您不能期望文件发生更改:您正在修改的字符串在内存

您面临的错误不清楚,但我可以看到一个大问题,您在空字符串上运行
replace

您的代码:

string str;
search=str;
str.replace(str.begin()+25,str.begin()+31,"=s2  //");
您创建
str
(默认情况下初始化为空字符串),将其分配给
search
(因此该字符串变为空),然后调用
replace
,尝试将字符25更改为31,因为
str
为空,因此不存在该字符

更新
可能您需要修复替换,但是您不能期望文件发生更改:您正在修改的字符串在内存中,而不是文件的一部分

因此,我将更改代码(尽可能使用您的代码):
*添加输出文件
*固定并更换变速箱
*在输出上保存输入文件的每一行(如果需要,请替换)

fileInput.open("IO_CAN_0_User.c");
ofstream  fileOutput;
fileOutput.open("output.c");

if(fileInput.is_open() && fileOutput.is_open() ) {

  while(!fileInput.eof()) {

    getline(fileInput, line);

    if ((offset = line.find(search, 0)) != string::npos) {

        cout << "found: " << search << endl;

        line.replace( offset, offset+search.size(), s2 );
    }

    fileOutput << line << '\n';
}
fileInput.open(“IO_CAN_0_User.c”);
流文件输出;
fileOutput.open(“output.c”);
if(fileInput.is_open()&&fileOutput.is_open()){
而(!fileInput.eof()){
getline(fileInput,line);
if((offset=line.find(search,0))!=string::npos){

cout
但是修改它会产生错误
您不认为错误可能是问题的重要部分吗?
while(test.eof()==0)//test.eof()如果未到达文件结尾,则返回0
不完全正确。这意味着尚未到达文件结尾。下一次读取可能会找到文件结尾,导致该读取无效。请阅读此处的详细信息:许多源代码编辑器支持搜索和替换,通常甚至跨越多个文件。是否确实要自己执行此操作?错误:调试断言失败。表达式:字符串迭代器+偏移量超出范围Ulrich Eckhardt:是的,先生,我确定我想自己做这件事。它的str=search;我更新了它,仍然是同一个错误。错误:调试断言失败。表达式:字符串迭代器+偏移量超出范围我可以用什么函数来代替s.replace()?@NaveenUmashankar:您需要更好地理解自己的代码。
str
,您在其上执行
替换的字符串,立即超出范围。您确定偏移量,并且从不使用它。整理您的
字符串
定义;您只有三个字符串:当前行、搜索字符串及其替换。这里是这是一个输入错误(offset+s1.size()=>offset+search.size())。我不需要完全创建一个新文件吗?这是一个相当大的文件。
fileInput.open("IO_CAN_0_User.c");
ofstream  fileOutput;
fileOutput.open("output.c");

if(fileInput.is_open() && fileOutput.is_open() ) {

  while(!fileInput.eof()) {

    getline(fileInput, line);

    if ((offset = line.find(search, 0)) != string::npos) {

        cout << "found: " << search << endl;

        line.replace( offset, offset+search.size(), s2 );
    }

    fileOutput << line << '\n';
}