gpt4 book ai didi

C++ 读取行和写入文件(行卫生)

转载 作者:行者123 更新时间:2023-11-30 01:09:47 24 4
gpt4 key购买 nike

我的部分代码似乎遇到了一些奇怪的问题,这些问题涉及从现有文件读取一行并将一部分写入另一个文件:

这是我所看到的示例(我只想要元音)。

image

这是我的三个函数。在尝试找出原因时,我有点 .c_str() 高兴:

void do_file_magic(string file){
fstream source;
string outfile,
line,
vowels,
temp;

// Name the outfile
outfile = "vowels_" + file;

source.open(file);
if (!source.is_open()) {
die_a_clean_death("Unable to open source file:", file);
};

ofstream destination(outfile);
if (!destination.is_open()) {
source.close();
die_a_clean_death("Unable to open destination file:", file);
};

// read source file and output vowels to destination file.
do {
for (int i = 1; !source.eof(); i++ ){
getline(source, line);
temp = vowels_on_a_line(line) + "\n";
destination << temp.c_str();
};
} while (!source.eof());

// Close the files
source.close();
destination.close();
};

string vowels_on_a_line(string input){
int input_size = input.size(),
vowel_counter = 0;

// this will not be larger than input_size
char vowelarray[input_size];

// find the vowels with the vowel test
for (int i = 0; i < input_size; i++){
if (vowel_test(input[i])){
vowelarray[vowel_counter] = input[i];
vowel_counter++;
};
};

// convert array to string
string vowels(vowelarray);

// return the stringi
printf("%s\n", vowels.c_str());
return vowels;
};

bool vowel_test(char c){
if(
c=='a'||c=='A'||
c=='e'||c=='E'||
c=='i'||c=='I'||
c=='o'||c=='O'||
c=='u'||c=='U'
) {
return true;
} else {
return false;
};
};

有没有人看到我在这里做错了什么?

这是运行时的终端输出。我注意到有 ?U? 代替文本编辑器输出:

Mac Shell: Homework2/>$ ./Homework2 

Enter a filename:
-----------------
test.txt
oeiuooiaeoeeuaiiieiaeeaiiaoeiea
uueeaoaeaoiouaiueeiiuaeeeueiiueia?U?
uuoeuaeoeiiuoeeauueuiuioiiuieiuuaaoi?
aeueeieoioooeauaieoaiuiaeuaoeuaiiu?U?
oieuuuiiauoeuoiiaeieieiaeuaaiii
UeaooaiuaeuueieieiauiiaIeeoioeieiiue?
aueuuuuaueieuiaaeuaauaeeeaeeeeuee?U?
aauueiuauaiuoeieeoeeooiiuiiaiaueiuuo?
aeieeaiaieouo

auiooiiaeauiaeuuuaeeuouuaaauoua
eueuoeeuiaeeuuaoiiiuoeieuiuaue
iaeeoaoiaieoiauiuauouiaeiaeuuoi
eueeeueeeiueeieuioauieeuaeuaa
?
iaueeiaeeeueuaoeauueuiauiiauiuieuu?U?
eeeueeeoaeuoioiuIauioeieueiua
eeeuiiieaeeoiiuioueiaoaiuaaiiai
aeaiaeiuauaeeauieoiaeeiiuieeeuui??U?
aiuaaaeuaaoooiouiaiaaiueaeuaa
?
aeeeueuouauueauaeeuUeeoieueioioeua?U?
eeaueueiuaaeuuuooeeeiuaeuuiie
auiieuioeuaaaaieieeeeuiaiieieeuIee?U?
aeaieaeiiaeuaoeauieiuaeueieeeeueua?U?
uoueuuuiaai

euaeioaeuieuiaeaiiaiaeoiouaiuaeeeuauue
iauoieuauiuaeauaoeAeeaiiauiuUeaiueoe?
eaiiaeieiuuoueaeeieuuaoeaeeaiuuaea#

iieuoeuaeeaauEiauiieuaoeaiueaaeoi?U?
auiaeaeiaeeuiauioeiieiieaaoeeuioeeu

uieueeeueuieoeuauiieauieeiueaiueuiiU?
eiuuoiuiieooaaiuiauoaeeiuaiaeuio??U?
H?U?
Mac Shell: Homework2/>$

最佳答案

显示的代码中存在多个错误。

   for (int i = 1; !source.eof(); i++ ){
getline(source, line);

在循环条件中检查 eof() is always a bug.

 char vowelarray[input_size];

// You put something into vowelarray.

string vowels(vowelarray);

显示的代码实例化了 vowelarrayvowelarray 未初始化。随后的代码继续填充 vowealarray 的初始内容。

vowelarray 传递给 std::string 的构造函数会将 char 数组衰减为 const char * .

std::string 构造函数的 const char * 参数必须是一个以“\0”结尾的字符串。

显示的代码无法以 \0 字节终止字符串。因此,生成的字符串通常会包含随机垃圾,这就是您所看到的。

您需要将数组的最大大小增加 1,以考虑额外的 '\0' 字符:

char vowelarray[input_size+1];

然后在从中构造 std::string 之前,将终止符 '\0' 正确添加到 char 数组。

关于C++ 读取行和写入文件(行卫生),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39461448/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com