C++ 调用函数c+;后,用另一个字符替换空白+;
我需要帮助获取声明的字符串函数,以将输入文件的空白更改为特定字符C++ 调用函数c+;后,用另一个字符替换空白+;,c++,arrays,string,function,file-io,C++,Arrays,String,Function,File Io,我需要帮助获取声明的字符串函数,以将输入文件的空白更改为特定字符 if (infile.fail()) { cout << "The file doesn't exist"; exit(-1); } else { numBooks = readFile (infile, magSub, 260); for (i=0; i<numBooks; i++) { cout << "Last Name: " <&
if (infile.fail())
{
cout << "The file doesn't exist";
exit(-1);
}
else
{
numBooks = readFile (infile, magSub, 260);
for (i=0; i<numBooks; i++)
{
cout << "Last Name: " << magSub[i].lastName << endl;
cout << "First Name: " << magSub[i].firstName << endl;
cout << "Street Address: " << magSub[i].address << endl;
cout << "City: " << magSub[i].city << endl;
cout << "State or Province: " << magSub[i].state << endl;
cout << "Country: " << magSub[i].country << endl << endl;
cout << "Zip or Postal Code: " << magSub[i].zip << endl;
cout << "Expiration Date: " << magSub[i].expDate << endl;
cout << "Subscriber Number: " << magSub[i].subNum << endl << endl;
}
writeFile(outfile, magSub, numBooks);
}
}
void fillSpace (string &expDate)
{
for (int i=0; expDate.length(); i++)
{
if (isspace(expDate[i]))
expDate[i] = '0';
}
}
if(infle.fail())
{
您可能错过了fillSpace
函数的for
循环中的检查条件
for(int i=0;i
以及调用函数
您必须声明一个字符串,该字符串将存储来自magSub[i].expDate
的字符串
然后将该字符串传递给函数fillSpace
之后,您将得到字符串,其中的字符空间替换为“0”
cout在您的fillSpace
代码中,您没有检查字符串结束条件。您应该使用I您没有调用空格更改函数。我不知道如何调用该函数以使其工作。我尝试过调用它,但它没有更改文件。因此,您认为不调用它会有帮助吗?对于(int i=0;expDate.length();i++)
应该是(int i=0;i
@OP能否请您显示magSub
是什么类的定义。变量是string
s还是char数组都很重要。不幸的是,它没有帮助…当我将那行代码放入时它崩溃。@user3546210expDate
的数据类型是什么?我有一个结构库(string expDate);@user3546210您能告诉我错误消息吗?@user3546210请检查答案。我现在已经编辑好了…希望这可以正常工作…请尝试一下
string temp = magSub[i].expDate; // copy the string to the temp string/char array
fillSpace (temp); // Missing Line for function call
cout << "Expiration Date: " << temp << endl; // replace line with