从文件中删除单词每次用户输入要删除的单词并将新数组写入新文件。 C++

Posted

技术标签:

【中文标题】从文件中删除单词每次用户输入要删除的单词并将新数组写入新文件。 C++【英文标题】:Deleting word from file evertime user enters a word to delete and writing the new array to a new file. C++ 【发布时间】:2017-03-07 13:20:02 【问题描述】:

您好,我是一名初级程序员,我需要一些帮助来完成我的部分作业。任务是制作一个菜单驱动程序,用户可以通过以下方式操作他们的文件:

    查找用户输入的单词及其所在的行 使用数组从文件中删除用户输入的单词并将新数组写入新文件。每次用户从文件中删除一个单词时,他们都会将旧文件的内容减去删除的单词写入一个新文件。第一个输出文件是 filename1.txt,然后第二个输出文件是 filename2.txt,依此类推。 最后将用户输入的单词替换为用户输入的另一个单词。

这里有一些信息可以帮助你:

我正在使用 Microsoft Visual Studio

到目前为止课堂上已经涵盖的内容:

-if, else if, else
-while, do-while, and for loops
-switch statements
-arrays, char arrays, and 2d arrays
-functions
-just started pointers

课堂上尚未涉及的内容:

-haven't done vectors yet
-haven't done classes yet

已完成的任务:

-Read from user File into a string array
-searched for word user enters with linear search
-replace a word deleted by the user with new word entered by the user

待办事项:

In case 2 (delete a word from the file):
    -I need to get rid of the empty array element when I delete a word from the array.
    -let the user delete a word again, then save it into a new output file (ie: filname1.txt,fileName2.txt,fileName3.txt. etc)
    -Formatting code

create functions for everything I need, I can do this part i am just coding it without for ease of understanding

我最需要关于 switch case 2 的帮助。 这是我迄今为止针对案例 2 的代码:

case '2':
    
        string d_newDictionaryArray[dictionarySize];

        cout << "Enter a word to delete: ";
        cin >> word;

        ofstream doutFile;
        doutFile.open("dictionary_deleted_word.txt");

        // copying from one one array to another
        for (int i = 0; i < dictionarySize; i++)
        
            d_newDictionaryArray[i] = dictionaryArray[i];
        

        // searching and deleting the word from the new array
        for (int j = 0; j < dictionarySize; j++)
        
            if (word == d_newDictionaryArray[j])
            
                d_newDictionaryArray[j] = "";
                cout << "The word " << word << " has been deleted :)" << endl;
            
        
        // Write to a new file
        for (int k = 0; k < dictionarySize; k++)
        
            doutFile << d_newDictionaryArray[k] << "\r\n";
        

        doutFile.close();


        break;
    

【问题讨论】:

欢迎来到 Stack Overflow。请花时间阅读The Tour 并参考Help Center 中的材料,您可以在这里问什么以及如何问。 【参考方案1】:
string space = " ";
int locate = -1;
for (int j = 0; j < dictionarySize; j++)
    
        if (space == d_newDictionaryArray[j])
       
            locate=j;
            break;
        
    
//to locate where the element with space is stored
if(locate!=-1)
for (int i = locate; i <  dictionarySize-1; i++)
    
        d_newDictionaryArray[i] = d_newDictionaryArray[i+1];
    
//to shift all the elements after the space to adjacent previous position

【讨论】:

非常感谢!它工作得很好,我理解你所做的背后的逻辑。此外,您如何定位数组的特定元素将在未来对我有很大帮助。真正令人大开眼界的部分是初始化 i 以进行定位,我习惯于将 i 初始化为 0 或 1,以至于我忘记了 i 只是一个计数器并且可以从任何地方开始,所以这样定位后的所有元素都会被移动.再次感谢您的帮助!

以上是关于从文件中删除单词每次用户输入要删除的单词并将新数组写入新文件。 C++的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript 数组删除重复的单词或字符(如果只输入字符。不要从 1 个单词中删除所有重复项

基于另一列从一列中删除单词,然后创建并将其放入新列

从字符串中删除重复的单词时会发生字符串标记问题

sed:从文件中删除字母数字单词

删除PHP中两个单词之间的空格[重复]

Leetcode——通过删除字母匹配到字典里最长单词(子序列)