1

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

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

在测试时,我创建了一个文件,其中包含我想要查找的字符串作为文件的唯一内容:

#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.

如果更改ldbtxt是一个愚蠢的想法并且行不通,请告诉我任何解决方法。

在我这样做之后,程序找不到字符串。用记事本打开文件作为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:更新的代码,仍然无法正常工作。进一步的测试得出结论,源文件的方式可能有问题......嗯......是。它可能太长,可能太复杂,可能不可能。我对此感到非常困惑。该代码在任何其他情况下都可以正常工作,因此,如果您需要类似的东西,请随时抓住并使用它,同时我尝试使其满足我的需要。

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

4

0 回答 0