When I try to Find a Keyword from the GetLine() Function It only Returns a Empty String. But when I try to print the Line with std::cout, it prints everything, so i debugged the solution I found that the String is empty but std::cout somehow manages to print the lines in file.
std::ifstream file(fileToSearch);
std::string line;
cout << endl << keyWords[i];
while (std::getline(file, line)) {
cout << endl << keyWords[i]<<" ";
cout << line;
if (auto pos = line.find(keyWords[i]) != std::string::npos) {
// std::stringstream iss(line.substr(pos + keyWords[i].size()));
cout << endl << "found again in String Table" << endl;
//iss >> j;
break;
}
}
This is the data in the File
%Checking Windows(R) Installer VersionConfiguring Windows InstallerConfiguring %sĀ½Setup has completed configuring the Windows Installer on your system. The system needs to be restarted in order to continue with the installation. Please click Restart to reboot the system.%s
When I checked the Encoding of the file in Notepad++. I found that the file is not encoded at all , but when I changed the file to UTF-8 I can get the Value and Search It Successfully. Is there anyway to do this by code rather than changing it Manually??
Related
I'm trying to read multiple files in a folder so I can parse through their data.
I first try to fill the list using a text document with all the file names in it, then based on that vector of string, continuously call ifstream so I can read every file and process the word data.
The problem I'm running into is that ifstream is failing to open all of the files, except one in the middle of the list?
Heres the output, its failing to read the dbfiles but they all have the right names?
These files aren't more than 8GB a piece so it should be able to handle it but it's not?
maybe theres a problem with the file paths?
std::ifstream dbfiles(argv[1]);
if (!dbfiles)
{
std::cerr << "Failed to open database " << argv[1] << " for reading." << std::endl;
}
std::string word;
std::vector<std::string> dbfile_names;
std::string file_name;
while (getline(dbfiles, file_name))
{ //reading in the file names
dbfile_names.push_back(file_name);
}//populate list of dbs
dbfiles.close();
for (unsigned int j = 0; j < dbfile_names.size(); j++)
{ //for every single file
std::ifstream dbfile(dbfile_names[j].c_str());
if (!dbfile)
{
std::cout << "Failed to open database file" << dbfile_names[j] << " for reading. READ FAILURE" << std::endl;
}else{
std::cout << "currently reading " << dbfile_names[j] << std::endl;
}
while (dbfile >> word)
{
//do stuff with the segments of data
//here I gather the data word by word and process it
}
dbfile.close();
}
I went into my debugger and found that due to getline, all the file names had a /r at the back of them.
The post over here Getting std :: ifstream to handle LF, CR, and CRLF?, helped describe the problem and how to easily fix it.
My files are now reading accordingly
I have the endl, but its not going into my file, so when I enter in more than 1 line, its all on the same line in the notepad.
I've tried:
codeFile << codeLine;
codeFile << endl;
I've also tried adding a "\n" to the string by adding a constant string to it but it doesn't work.
//Writing Coded Lines to File:
if(codeFile)
{
//Relaying Feedback to User:
cout << "File has been successfully opened/created" << endl;
cout << "\nWriting to file..." << endl;
for(int i = 0; i < lines; i++)
{
//Getting Non-Coded Line from User:
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cin.getline(line, length);
//Creating Copy of Line:
strcpy(cline, line);
//Gathering Line Length:
length = strlen(line);
//Coding Line:
codedLine = code(length, line, cline, sAlphabet);
//Coded Line Test
cout << codedLine;
//Writing to File:
codeFile << codedLine << endl;
}
}
//Closing File:
codeFile.close();
cout << "\nFile has now been closed";
}
Cygwin mocks a POSIX system and uses Unix line endings, not the Windows line endings understood by NotePad.
Replacing endl with '\n' won't help. endl is a '\n' followed by a stream flush.
The best option is to use a different file reader, WordPad for example, that understands Unix line endings. The alternatives are to
Change your compiler toolchain to one that is not emulating a POSIX operating system, or
Brute force a Windows line ending with \r\n, but this means your code will have a similar wrong line-ending problem on a Unix -based system.
Note: I am using the C++11 standard, so I don't see why this isn't working with or without c_str() appended.
I have the following code:
// open streams
ifstream in(input);
ofstream out(output);
// get which file to open
in.ignore(INT_MAX, ':'); // we don't need the beginning part
in.ignore(); // remove trailing whitespace
string fileLocation;
getline(in, fileLocation);
out << "Loading: " << fileLocation << endl;
cout << "Loading: " << fileLocation << endl;
// now that we know where the file is, load it:
ifstream file(fileLocation);
which reads from a file that looks vaguely like this
File: file.txt
(Subcommands below)
I know that I am pulling the correct filename because of the terminal output.
Anyway, I noticed that the stream wasn't opening properly, so I added this conditional to check:
if ( !file )
{
cout << "File wasn't loaded properly." << endl;
}
And sure enough, I see that message when running the program.
My question is this: how come, when I hard-code the file location, e.g. ifstream file("file.txt") it opens up no problem? How do I get this working properly?
my partner wrote a bunch of code for one of my projects in a text editor, when i run the code it works perfectly.....
now i have copy and pasted all the code into qt creator, and im having an issue
stringstream ss;
string line;
ifstream myfile;
myfile.open("Instructors.txt");
if (myfile.is_open()){
while (getline(myfile,line)){
ss << line << ", ";
}
myfile.close();
}
else cout << "bad open" << endl;
above is the part fo my code that is having the issue, i can assure you all Instructors.txt is indeed in in the correct file, but everytime our code reaches this point imstead of opening the file i get thrown to the else "bad open" why would this be?
It's hard to say what it may be without any error code, what you can do is to improve your error message with something more meaningful (for you and for your customers):
else cout << "Error opening file: " << strerror(errno) << endl;
strerror (see reference) function returns a string for a given error code captured in by errno macro.
Otherwise you can do it much more C++ish using exceptions: first enable them for your stream:
myfile.exceptions(ifstream::failbit | ifstream::badbit);
Then catch them, all together is:
try
{
ifstream myfile("Instructors.txt");
myfile.exceptions(ifstream::failbit | ifstream::badbit);
while (getline(myfile, line))
{
ss << line << ", ";
}
myfile.close();
}
catch (ifstream::failure e)
{
cout << e.what() << endl;
}
Try to rewrite file name, may be it contains characters from different encodings.
double check the working directory, chances are it is in the build folder (where the executable gets dropped)
in QtCreator you can fix this by going to projects and selecting run; there you will be able to set the working directory
I am trying to read from file:
The file is multiline and basically i need to go over each "word". Word being anything non space.
Sample input file would be:
Sample file:
test 2d
word 3.5
input
{
test 13.5 12.3
another {
testing 145.4
}
}
So I tried something like this:
ifstream inFile(fajl.c_str(), ifstream::in);
if(!inFile)
{
cout << "Cannot open " << fajl << endl;
exit(0);
}
string curr_str;
char curr_ch;
int curr_int;
float curr_float;
cout << "HERE\n";
inFile >> curr_str;
cout << "Read " << curr_str << endl;
The problem is when it reads new line it just hangs. I read everything before test 13.5
but once it reaches that line it doesnt do anything.
Anyone can tell me what I am doing wrong?
Any better suggestion on how to do this???
I essentially need to go through file and go one "word" (non white char) at the time.
I
Thanks
You open a file 'inFile' but are reading from the 'std::cin' any particular reason?
/*
* Open the file.
*/
std::ifstream inFile(fajl.c_str()); // use input file stream don't.
// Then you don't need explicitly specify
// that input flag in second parameter
if (!inFile) // Test for error.
{
std::cerr << "Error opening file:\n";
exit(1);
}
std::string word;
while(inFile >> word) // while reading a word succeeds. Note >> operator with string
{ // Will read 1 space separated word.
std::cout << "Word(" << word << ")\n";
}
Not sure how "in the spirit" of the iostream library this is, but you could do it with unformatted input. Something like:
char tempCharacter;
std::string currentWord;
while (file.get(tempCharacter))
{
if (tempCharacter == '\t' || tempCharacter == '\n' || tempCharacter == '\r' || tempCharacter == ' ')
{
std::cout << "Current Word: " << currentWord << std::endl;
currentWord.clear();
continue;
}
currentWord.push_back(tempCharacter);
}
Does that work?