关于类实现和写入创建的文本文件的C++帮助
C++ help regarding class implementation and writing into a created text file
我已经困惑了几个小时,不知道该如何解决这个问题。。我的类的实现文件中有两个函数。无论如何,我一直在尝试让我的程序对我电脑上的某个文件中的单词进行加扰(这是一个包含许多单词的词典列表),到目前为止,这是成功的。
但是,问题是,我正试图将所有的单词加扰版本放到一个新创建的存储它们的文本文档中。。。到目前为止,它还没有起作用,我真的很困惑。我大胆地指出了代码中的问题所在。
请注意:我是一名目前正在学习C++的学生,在我完成程序的一般核心之前,代码有点杂乱无章。我的教练说我不允许使用字符串类,ONLY cstring
需要帮助的领域:
"cout<<tmpword<<endl;//我想要写入新文本文件的内容***帮助"和"新建分区<
提前感谢!
void Scrambler::scrambleletters_in_file()
{
char * tmpword = nullptr; //NULL
// compare with every single word in the dictionary
for (int i = 0; i < dictionarySize; i++)
{
// clean the space holding the previous word if any
if (tmpword != nullptr)
delete[] tmpword;
//prepare space
tmpword = new char[strlen(dictionary[i]) + 1];
strcpy_s(tmpword, strlen(dictionary[i]) + 1, dictionary[i]);
// do the unscrambling stuff here
// 1 2 3 4 5 6 7
int first = 1; // H I C K E N '/0'
int last = strlen(tmpword) - 2; // = C H I C K E
// We are showing the difference here, where the first/last letters are
// not scrambled.
for (int i = first; i < last; i++)
{
int j = rand() % last + 1; // Chicken[1-5]'s letters will be randomized
// For instance: "hicke" will be scrambled.
char result = tmpword[i];
tmpword[i] = tmpword[j];
tmpword[j] = result;
}
cout << tmpword << endl; //What I want written into my new text file **** HELP
}
}
void Scrambler::createNew_dictionary()
{
// ofstream is used for writing files
// We'll make a file called scrambled_words.txt
char newfilename[25];
char choice3;
cout << "nHow do you want to create the new scrambled file? (Input 1 or 2) " << endl;
cout << " - PRESET: ("unscrambled_file.txt"): 1 " << endl;
cout << " - CUSTOM:ttt 2 " << endl;
cout << "nChoice ===> ";
cin >> choice3;
if (choice3 == '1')
{
strcpy_s(newfilename, 25, "unscrambled_file.txt"); // copies the preset filename into "newfilename"
}
if (choice3 == '2')
{
cout << "nWhat would you like the new file name to be? (EXAMPLE: "test.txt")" << endl;
cout << "~ Note: if you type in an already existing file name, it will be overwritten " << endl;
cout << "* WARNING: USE underscores INSTEAD of spaces in the text file name! " << endl; // Since I'm supposed to use cstring
// instead of string, this won't work with spaces
cout << "nFile Name ====> ";
cin >> newfilename; // ^ Practing by adding user input customization ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
//*TO DO (and practice) - SHOW A LIST OF CURRENTLY EXISTING .txt FILES, AND SELECT A FILE TO DELETE//
}
ofstream new_dictionary(newfilename); // Opens and/or creates the specified file if it hasn't already.
// If we couldn't open the output file stream for writing
if (!new_dictionary)
{
// Print an error and exit
cout << "Uh oh, the text file could not be opened for writing!" << endl;
system("PAUSE"); // this is just included so you have time to read what the error message
// says before the program is exited by the next statement.
exit(1); // equivilent to "return 0;" in main function
}
else
cout << "File successfully created and opened" << endl;
new_dictionary<<scrambleletters_in_file(); // <--- MY PROBLEM ********
/* We'll write two lines into this file
new_dictionary << "This is line 1" << endl;
new_dictionary << "This is line 2" << endl;
*/
cout << "File successfully overwritten" << endl;
new_dictionary.close();
cout << "File closed. " << endl;
}
在scrambleletters_in_file()
中,您将字符串输出到cout
,这是标准输出(默认情况下为控制台)。此行:
new_dictionary<<scrambleletters_in_file();
表示"计算函数scrambleletters_in_file
的结果并将其写入new_dictionary
"。它的结果是void
,所以代码甚至没有编译。
您需要的是将流传递给函数:
void Scrambler::scrambleletters_in_file(ostream& out) {
...
out << tmpword << endl;
}
并这样称呼它:
scrambleletters_in_file(new_dictionary);
相关文章:
- 文本文件中的单词链表
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 如何将内容数组写入文本文件?
- 无法通过空白将文本文件行分隔为矢量
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- C++将文本文件中的数据读取到结构数组中
- 在指针的帮助下,文本文件中单词的频率
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 将值从二维数组输出到文本文件
- 如何在c++中从文本文件中逐行读取整数
- 从文本文件中读取时钟时间和事件时间并进行处理
- 如何从文本文件中读取值和数组
- 如何在C++中确定文本文件中的元素是字符还是数字
- C++试图读取一个文件并输出到另一个文本文件
- 如何通过套接字将文本文件的内容从服务器发送到客户端
- 已修改的LinkedList未在文本文件本身中更新
- C++文本文件的获取线
- 为什么C++的文件 I/O 在读取文本文件时忽略初始空行?我怎样才能让它不这样做?
- 打印逐行存储的文本文件
- C++文本文件输入