#include <iostream>
#include <cctype>
#include <string>
#include <fstream>
char* functionC(char* destination, const char* source)
{
int k = 0;
for (int i = 0; source[i] != '\0'; i++)
{
if (!isdigit(source[i])) //isDigit проверяет число ли
{
destination[k] = source[i];
k++;
}
}
destination[k] = '\0';
return destination;
}
std::string functionString(const std::string& source)
{
std::string destination = ""; //строка для вывода输出线,输出线
for (int i = 0; i < source.size(); i++)
{
if (!isdigit(source[i]))
{
destination += source[i];
}
}
return destination;
}
int main()
{
std::ifstream infile;
std::ofstream outfile;
infile.open("");
outfile.open("");
if (infile.peek() == std::ifstream::traits_type::eof()) //проверка на пустой файл空文件检验
{
outfile << "File is emrty!\n";
return 1;
}
int length; //длина строки长字符串
infile >> length;
while (!infile.eof())
{
length++; // учитываем символ \0考虑到符号
if (infile.fail() || length <= 0)
{
outfile << "Invalid string length\n";
return 1;
}
char* arrayC1 = new char[length];
char* arrayC2 = new char[length];
arrayC2[0] = '\0';
infile.ignore(length, '\n');
infile.get(arrayC1, length);
for (int i = 0; i < length - 1; i++)
{
if (arrayC1[i] == '\0')
{
outfile << "Invalid string length\n";
return 1;
}
}
std::string str = arrayC1;
outfile << "Source string\t\t\t\t" << str << '\n';
outfile << "With C-style strings:\t" << functionC(arrayC2, arrayC1) << '\n';
outfile << "Using the string class:\t" << functionString(str) << '\n';
delete[] arrayC1;
delete[] arrayC2;
infile >> length;
}
infile.close();
outfile.close();
return 0;
}
这段代码无法运行,原来以为是路径的问题,把文件删了也进不去
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- 泡视界 2021-03-23 09:55关注
一般用
if (infile.fail()) { cout << "Failed to open file!"<<endl; return 1; }
解决 1无用
悬赏问题
- ¥15 Arduino的wifi连接,如何关闭低功耗模式?
- ¥15 Android studio 无法定位adb是什么问题?
- ¥15 C#连接不上服务器,
- ¥20 需要帮我远程操控一下,运行一下我的那个代码,我觉得我无能为力了
- ¥20 有偿:在ubuntu上安装arduino以及其常用库文件。
- ¥15 请问用arcgis处理一些数据和图形,通常里面有一个根据点划泰森多边形的命令,直接划的弊端是只能执行一个完整的边界,但是我们有时候会用到需要在有很多边界内利用点来执行划泰森多边形的命令
- ¥30 在wave2foam中执行setWaveField时遇到了如下的浮点异常问题,请问该如何解决呢?
- ¥750 关于一道数论方面的问题,求解答!(关键词-数学方法)
- ¥200 csgo2的viewmatrix值是否还有别的获取方式
- ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊