C++ readline and write to file (line sanitation) -
i seem running odd issues portion of code involves reading line existing file , writing portion file:
here example of seeing (i want vowels).
here 3 functions involved this. got little .c_str()
happy while trying figure out cause:
void do_file_magic(string file){ fstream source; string outfile, line, vowels, temp; // name outfile outfile = "vowels_" + file; source.open(file); if (!source.is_open()) { die_a_clean_death("unable open source file:", file); }; ofstream destination(outfile); if (!destination.is_open()) { source.close(); die_a_clean_death("unable open destination file:", file); }; // read source file , output vowels destination file. { (int = 1; !source.eof(); i++ ){ getline(source, line); temp = vowels_on_a_line(line) + "\n"; destination << temp.c_str(); }; } while (!source.eof()); // close files source.close(); destination.close(); }; string vowels_on_a_line(string input){ int input_size = input.size(), vowel_counter = 0; // not larger input_size char vowelarray[input_size]; // find vowels vowel test (int = 0; < input_size; i++){ if (vowel_test(input[i])){ vowelarray[vowel_counter] = input[i]; vowel_counter++; }; }; // convert array string string vowels(vowelarray); // return 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; }; };
does see doing wrong here?
here terminal output while running this. note there ?u?
in lieu of text editor output:
mac shell: homework2/>$ ./homework2 enter 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/>$
there multiple bugs in shown code.
(int = 1; !source.eof(); i++ ){ getline(source, line);
checking eof()
in loop condition is bug.
char vowelarray[input_size]; // put vowelarray. string vowels(vowelarray);
the shown code instantiates vowelarray
. vowelarray
not initialized. subsequent code proceeds fill initial contents of vowealarray.
passing vowelarray
std::string
's constructor decays char
array const char *
.
the const char *
parameter std::string
constructor must `\0'-terminated string.
the shown code fails terminate string \0
byte. such resulting string typically contain random garbage, you're seeing.
you need increase maximum size of array 1, in order account '\0' character:
char vowelarray[input_size+1];
and add terminating '\0' char
array, before constructing std::string
it.
Comments
Post a Comment