Okay, the title may be a bit vague. I honestly don't even know how to phrase the question.
The program tries to find a string of text inside a file, and then print the entire line that the string is on.
While testing, I created a file with the string I wanted to find as the only contents of the file:
#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
When I use this configuration, the program finds "string"
and outputs
[TEMPORARY]string[TEST]
as expected.
However, this layout was only to be used in testing, so after confirming it worked, I wanted to test renaming the file that I would actually be using in order to find the same string.
I copied the file, and wrote a program to rename it to a text file so it would be easier to read from:
#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.
If changing ldb
to txt
is a stupid idea and won't work, let me know of any workarounds.
After I did this, the program does not find the string. Opening the file with notepad as an ldb
or a txt
shows the string exists.
#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");
The program outputs "I couldn't find it, sorry.\n"
.
Honestly, I think it has something to do with the file conversion as it works with a text file created within the program itself, but i don't see much wiggle room there.
When I open the new text file in a text editor after the program fails, it works just fine.
EDIT:
After further testing, this might be situational. I created a sample ldb
file with a string in it, and it worked. I tried pushing it way down to the bottom and it worked. I tried the original file, and it doesn't. it might actually just be unable to print some of the characters that are in the file. This may require a very complicated workaround, and it may even be impossible; or, it could be simple...I don't know. I will conduct further testing and if I solve the problem I will close the question.
It also doesn't save to the document, so it must be a problem within the if
statement.
EDIT2: updated code, still not working. further testing concludes it might be a problem with the way the source file... well... is. it might be too long, it might be too complex, it might be impossible. i am seriously puzzled with this one. the code works fine in any other situation, so if you need something similar feel free to grab this and use it while i try and make it work for what I need.
oh yeah and thanks for the formatting, casey.