微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

添加使用重命名文件的函数后,程序将不再找到文本字符串

如何解决添加使用重命名文件的函数后,程序将不再找到文本字符串

好吧,标题可能有点含糊。老实说,我什至不知道如何表达这个问题。

程序尝试在文件中查找一串文本,然后 打印字符串所在的整行。

在测试时,我创建了一个文件,其中的字符串是我想查找的唯一内容

#include <Windows.h>
#include <iostream>
#include <fstream>
#include <string>
#include <iostream>
#include <filesystem>
#include <Wininet.h>
#include <stdio.h>

//...

std::cout << "Creating a document...\n"; //this is test code that creates a sample document with "string" inside it.
std::ofstream write;
write.open ("test.txt");                 //actual file creation
write << "[TEMPORARY]string[TEST]\n";    //print into the document
write.close();
std::cout << "Document created.\n";      //end message

当我使用这个配置时,程序找到"string"输出 [TEMPORARY]string[TEST] 符合预期。

然而,这个布局只用于测试,所以在确认它有效后,我想测试重命名我实际使用的文件以找到相同的字符串。

我复制了该文件,并编写了一个程序将其重命名为文本文件,以便于阅读:

#include <Windows.h>
#include <iostream>
#include <fstream>
#include <string>
#include <iostream>
#include <filesystem>
#include <Wininet.h>
#include <stdio.h>

//...

std::cout << "Changing extension to txt...\n";
//int result; //making variable to store result of following code. potential warning fix.
char oldname[] = "data.ldb"; //set up rename variables
char newname[] = "string.txt"; //same as ^
rename(oldname,newname); //change the ldb to txt so we can read from it.
std::cout << "Extension changed.\n"; //this code block produces a warning. please fix. low priority.

如果将 ldb 更改为 txt一个愚蠢的想法并且不起作用,请告诉我任何解决方法

执行此操作后,程序找不到该字符串。使用记事本打开文件作为 ldbtxt 显示字符串存在。

#include <Windows.h>
#include <iostream>
#include <fstream>
#include <string>
#include <iostream>
#include <filesystem>
#include <Wininet.h>
#include <stdio.h>

//...

std::cout << "Attempting to get line with word \"string...\"\n";
    std::ifstream input;                                //"input" is what we are using to read the file. 
    size_t pos;                                         //size_t has something to do with finding text.
    std::string line;                                   //this is what we save the line to.
    input.open("string.txt");                           //open the document with the string.
    if (input.is_open()) {                              //if we are in the file...
        while (!input.eof()) {                          //get a line from it..."
            getline(input,line);
            pos = line.find("string");                  //that says "string."
            if (pos != std::string::npos) {             //if we find it...
                std::cout << "Line found!"              //print "Line found!"
                "                   \n"                 //(this is to remove the "i Couldn't find it" message.)
                "The line is...\n\n";               
                std::cout << line;                      //and then print the line we found.
                std::cout << "\n\nSaving to file...\n"; 
                std::ofstream grab;                     //Now open a new file...
                grab.open("grabbedstring.txt");         //called "grabbedstring,"
                grab << line;                           //print the line to it,grab.close();                           //and close the file.
                std::cout << "Saved to file \"grabbedstring.txt";         //We did it! :D
            }
            else {
                std::cout << "I Couldn't find it,sorry.\n"             //we didn't do it... D:
                "\x1b[A";
            }
            
        }
        
    }
    else {
        std::cout << "I Couldn't open the file,sorry.\n";               //we didn't even come close... ;-;
    }
    input.close(); 
    std::cout << "\n\nEnd of code so far.\n"
        "Completed successfully!\n";
    system("pause");
    

程序输出"I Couldn't find it,sorry.\n"

老实说,我认为这与文件转换有关,因为它适用于在程序本身中创建的文本文件,但我看不到那里有多少回旋余地。

当我在程序失败后在文本编辑器中打开新的文本文件时,它工作得很好。


编辑:

经过进一步测试,这可能是特定情况。我创建了一个包含字符串的示例 ldb 文件,并且它工作正常。我试着把它推到底,它奏效了。我试过原始文件,它没有。它实际上可能只是无法打印文件中的某些字符。这可能需要非常复杂的解决方法,甚至可能是不可能的;或者,这可能很简单……我不知道。我将进行进一步的测试,如果我解决了问题,我将关闭问题。

它也没有保存到文档中,所以它一定是 if 语句中的一个问题。


EDIT2:更新的代码,仍然无法正常工作。进一步的测试得出结论,源文件的方式可能有问题......好吧......是。它可能太长,可能太复杂,可能是不可能的。我对这个很困惑。该代码在任何其他情况下都可以正常工作,因此如果您需要类似的东西,请随时抓住它并使用它,同时我尝试使其适合我需要的东西。

哦,是的,谢谢你的格式化,凯西。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。