#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("infile.txt");
outfile.open("outfile1.txt");
if (infile.peek() == std::ifstream::traits_type::eof()) //проверка на пустой файл
{
outfile << "File is empty!\n";
return 1;
}
int length; //длина строки长字符串
infile >> length;
while (!infile.eof())
{
length++; // учитываем символ \0考虑到符号
if (infile.fail() || length <= 0)
{
outfile << "\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;
}
if (!isdigit(source[i])) 这里触发了断点不知道为什么