【问题标题】:Overwriting and Appending lines in text files (C++)覆盖和追加文本文件中的行 (C++)
【发布时间】:2012-09-21 14:56:29
【问题描述】:

我正在尝试在没有太多 I/O 或使用文本文件经验的情况下完成 CSE 实验。该程序的目标是根据用户输入的单词和定义创建字典。输出文本文件的格式应该是

条目数(作为数字)

单词

定义

单词

定义

等等

此时,如果您的所有单词都是“Alberta”且定义为“Threesome”,则输出显示为:

1

阿尔伯塔

三人组

2阿尔伯塔

三人组

2阿尔伯塔

三人组

显然有些地方很不对劲。这是生成的代码:如果它很混乱并且输出语句被奇怪地分开,我很抱歉,我已经尝试了一段时间。

#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>

using namespace std;
int main() 
{
    string name; // Name of writing/reading document

cout << "Please enter the path (including file name) for the dictionary you want to create: " ;
getline(cin,name); // Inputs name

fstream dicFile;

int addcount = 0; // number of times file has been added to.

int choice = 0; // Controls while loop

while (choice != 3) 
{
    cout << "- - - - - - - - - -" << endl;
    cout << "1) Add a word to the dictionary" << endl;
    cout << "2) Print Dictionary" << endl;
    cout << "3) Quit" << endl;
    cout << "Choose an option from the menu: ";

    string choiceS;
    getline(cin,choiceS);

    choice = atoi(choiceS.c_str());

    string wordAdd; // Stores word to be added
    string defAdd; // Stores definition to be added

    int size = 1; // Number of entries after the first entry

    if(choice > 0 && choice < 4)
    {
        cout << "- - - - - - - - - -" << endl;

        if(choice == 1) 
        {
                            
            if(addcount == 0) 
            {
                dicFile.open(name.c_str(),ios::in|ios::out|ios::app);
                cout << "Enter a word or phrase to add to the dictionary: ";
                getline(cin,wordAdd);
                dicFile << size;
                dicFile << endl;
                dicFile << wordAdd;

                dicFile << endl;

                cout << "Enter the definition for \"" << wordAdd << "\": ";
                getline(cin,defAdd);
                dicFile << defAdd << endl;
    
                dicFile.close();    
                addcount++;
            }
            
            else 
            {
                dicFile.open(name.c_str(),ios::in|ios::out|ios::app);
                size = size + 1;
                dicFile << size;
                dicFile.close();

                dicFile.open(name.c_str(),ios::in|ios::out|ios::app);
                cout << "Enter a word or phrase to add to the dictionary: ";
                getline(cin,wordAdd);
                dicFile << wordAdd;

                dicFile << endl;

                cout << "Enter the definition for \"" << wordAdd << "\": ";
                getline(cin,defAdd);
                dicFile << defAdd << endl;
    
                dicFile.close();
            }

            
        }   

        if(choice == 2)
        {
            if(true)
            {
                dicFile.open(name.c_str(),ios::in);
                
                string readSize;
                int readSizei;
                
                getline(dicFile,readSize);
                readSizei = atoi(readSize.c_str());

                for(int i = 0; i < readSizei*2 + 1; i++)
                {
                    string word;
                    string def;
                    getline(dicFile,word);
                    cout << word << endl;
                    getline(dicFile,def);
                    cout << def << endl;
                }
                dicFile.close();    
            }

            else 
            {   cout << "- - - - - - - - - -" << endl;
                cout << "The dictionary is empty!" << endl;
            }
        }
        
    }

    else 
    {   cout << "- - - - - - - - - -" << endl; 
        cout << "Invalid menu selection (number should be between 1-3)" << endl; 
    }

}
}

当我选择选项 2 时,我必须做什么才能覆盖文本文件的第一行,但将新定义附加到文件中?对 I/O 来说相当新。感谢您的宝贵时间。

【问题讨论】:

    标签: c++ file text append overwrite


    【解决方案1】:

    您不能“就地”修改现有文件。相反,您必须创建一个新的临时文件,并将所有内容复制到临时文件中,并进行所需的修改。然后将临时文件重命名为原始文件,从而替换它。

    在伪代码中:

    open_old_file();
    create_temporary_file();
    
    while (read_line_from_old_file())
    {
        do_possible_modifications_to_line();
        write_line_to_temporary_file();
    }
    
    close_temporary_file();
    close_old_file();
    
    rename_temporary_file_to_old_file();
    // or if the above doesn't work:
    //   copy_temporary_file_to_old_file();
    //   delete_temporary_file();
    

    【讨论】:

    • 这听起来很简单,但我该怎么做呢?
    • 非常感谢您抽出宝贵时间,我会试试的。
    猜你喜欢
    • 1970-01-01
    • 2010-10-16
    • 2016-01-01
    • 2017-07-21
    • 2017-09-14
    • 2016-06-19
    • 2016-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多