I am working on a project in C++. I have to read different files. And somehow, only the first file works and others don't. My code is below
void scanner::readfile(string input)
{
infile.open(input);
while (!infile.eof())
{ .......
......
}
}
After I read my first file,It works perfectly fine. So when I try to read the second file, it won't even go into the while loop. So I use infile.peek(), it returns -1 for every file after first file. I assume that maybe infile is stuck at EOF from last file. Is there anywhere I can fix the problem?
Related
I'm making a program that compares a string entered by the user to another one taken from an ifstream Gfile, and if the comparison is True, i want to save that ID in an ofstream RFile, and here is my code:
void Dropbox::SafetyVerifByString(string GivenID, string GivenFile, string ReturnFile)
{
ifstream GFile(GivenFile.c_str());
ofstream RFile("Results.txt");
string Res="";
if(!GFile.is_open() || !RFile.is_open())
{
cout<<"Error: One Of The Files Could Not Open!";
}
else
{
while(getline(GFile,Email,':') && getline(GFile,ID)) // GFile contains a 100 lines like this mickeykats#gmail.com:fa289bcf529dc73c1e0376652af1299332946b59
{
if(GivenID==ID) //this is where my problem resides
{
Res=Email+":"+ID;
cout<<Res<<endl;
RFile<<Res<<endl;
}
}
}
GFile.close();
RFile.close();
}
i tried putting the RFile<<Res<<endl; above and outside that if statement and it worked, it literally works anywhere but inside that if statement, and i want it nowhere but inside that statement, i've been stuck here from 2 AM till 5 AM and it is driving me crazy, there is literally no reason for it to do that .. thanks for your time.
PS: while i'm at it, if there is any better way to read from a file containing hundreds of lines each line has the following format -> Email:Code in a way that stores Email in a string variable and code in another string variable, please let me know, if what i came up with (through internet research) is good, let me know too, thank you.
Edit1: i added a Cout<<ID<<endl; right after if(ID=GivenID) just to show you that ID is correctly getting the desired value, and i did a bunch of trie to show you the result:
Edit2: i did some more testing and it seems like some of the 4 text files i have work okay and the function does print to Results and in some others it doesn't, i'm even more confused ... it my have something to do with the encrypted pass in some of the files and the character that constitute it, here is a preview of the files:
When I copying the content of file Source.txt, which include only the word "Life", to another file Target.txt.
It only copy the "EI" not "Life".why?
The following
Blockquote
is the code that i tried. is another way to copy one file content to another file. and also explain the following why it's happen?
Thanks in advance.
Great Confusion.
Source File include the following text:
Life
Copied Files from Source File is:
EI
char ch;
ifstream source("Source.txt");
ofstream target("Target.txt");
while(source.eof()==false)
{
source.get(ch);
target<<ch
The correct code is
char ch;
ifstream source("Source.txt");
ofstream target("Target.txt");
while(source.get(ch))
{
target<<ch;
}
eof is only true after you read and it fails (because of eof). It's not generally true when you are at the end of file, i.e. if the next read will fail because of end of file. Because of this reason it's almost never correct to use eof in a while loop condition.
More detail
The program almost runs but i am not sure how to make the .txt file for this , its not giving me an error.
the project asks me to:
" File encryption is the science of writing the contents of a file in a secret code. Your encryption program should work like a filter, reading the contents of one file, modifying
the data into a code, and then writing the coded contents out to a second file.
The second file will be a version of the first file, but written in a secret code. Although there are complex encryption techniques, you should come up with a simple one of your own. For example, you could read the first file one character at a time, and add 10 to the ASCII code of each character before it is written to the second file. "
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char ch;
fstream fin, fout;
fin.open("testone.txt", ios::in);
fout.open("encrypted.txt", ios::out);
while (!fin.eof())
{
fin.get(ch);
fout.put(ch + 10);
}
fin.close();
fout.close();
system("pause");
return 0;
}
Read this -
Error LNK1561: entry point must be defined
https://social.msdn.microsoft.com/Forums/vstudio/en-US/e1200aa9-34c7-487c-a87e-0d0368fb3bff/error-lnk1561-entry-point-must-be-definedproblem-in-c?forum=vclanguage
Not up on my Visual C, but you may need #include <cstdlib> to get system
LNK1561 means your main function can't be found. Clearly the main function is present, so this should compile. Follow Beta's suggestion and ensure you can compile and run a trivial program.
Putting Compiling issues aside, This code won't work.
Overarching Problem: You are not checking for any errors along the way, so there is no way for your program to tell if anything has gone wrong.
For example, what if the file didn't open? The while (!fin.eof()) becomes an infinite loop. If the file is not open, you can never read EOF. Trying to use EOF as a loop condition is a bad idea anyway. Definitely read the link in #Steephen's comment.
If you fail to read a character with fin.get(ch); then what? The current code tries to use the character anyway. Bad idea.
Testing a stream is pretty simple. if (!fin) does the job. Read up on how streams work to learn why. Thius simple test doesn't tell you what went wrong, but at least you know something went wrong.
To make things easier, most stream functions return the stream. This lets you chain stream operations together and makes if (!fin.get(ch)) an easy way to tell if get worked.
So your IO loop can be as simple as
while (fin.get(ch) && fout.put(ch + 10))
{
}
If get couldn't get ch for any reason--unopened file, end of file, unreadable file--the while loop exits. Afterwards you can query fin to find out why. If EOF, awesome. If not EOF, the output file's probably wrong.
The same applies to put. If put failed, the loop ends. Test for why and decide if you want to keep the file.
I also recommend dropping a quick test at the end of main to print out a check.
fin.open("encrypted.txt", ios::in);
while (fin.get(ch) && std::cout.put(ch - 10))
{
}
A better test would be to read the character, undo the encryption, and compare against the original input.
Okay, I've seen answers on here but I still don't understand exactly what I need to do. I am trying to read in stuff from a file but I'm getting unexpected results with .eof() and I'm becoming frustrated with it! I cut out the stuff I'm actually doing for a project I'm doing but hopefully the code I have is enough for help. I have a bunch of similar while loops in my functions as I'm trying to parse data from a file and assign the data to the correct data structures. It seems to work when it wants too as one file I was testing works, while another says it reaches the end of the file when in reality it has one more piece of data to read. I don't know what's going on. Thank you ahead of time for the help!
void ReadStuff(ifstream &theFile)
{
while (!theFile.eof())
{
string line;
getline(theFile, line);
istringstream read(line);
string lineSect;
while (!read.eof())
{
read >> lineSect;
if (read.eof())
{
break;
}
//Reading stuff for this line
}
//Reading more stuff from a file until all data for all labels is read
}
}
void bot_manager_item::create_games()
{
games.clear();
std::ifstream paths_in("C:\\Users\\bill hank\\Documents\\bot_plugins\\directory_listing.txt", std::ios::in);
while (paths_in.good())
{
send_message("The path was good.");
char q[5000];
paths_in.getline(q, 5000);
send_message(q);
games.push_back(qanda(q));
}
paths_in.close();
}
The file I'm loading exists, what else might be wrong? paths_in.good keeps failing.
Edit: I figured it out. Wow am I annoyed by the answer to this. Basically Windows lets you say whether you want to show file extensions or not. This windows installation is set to say that the extension shouldn't be shown. So when I'm checking the file again and again I'm seeing: directory.txt and thinking that this means that everything is fine with the directory when in reality the filename was directory.txt.txt
If paths_in.good() keeps failing then it means that some of the stream error flags are set (badbit, eofbit or failbit).
eofbit - end of file was reached
badbit - error with the stream buffer such as memory shortage or an exception inside the stream buffer is cast
failbit - some other error beside eof was reached
In order to find out what happened, you need to check which errorbit is set first, and then find out more about the specific error, and what can cause it.
Out of curiosity, does this code output the contents of the file correctly? If this code works, then the problem is something else. If this code doesn't work, then that likely means that the file either isn't where you specified, or you don't have read permissions on it.
void bot_manager_item::create_games() {
std::ifstream paths_in("C:\\Users\\bill hank\\Documents\\bot_plugins\\directory_listing.txt");
char q[5000];
while (paths_in.getline(q, 5000)) {
std::cout << q << std::endl;
}
}
This code does a few minor things differently.
std::ios::in doesn't need to be explicitly specified for std::ifstream.
it doesn't use is_good, while that should be fine, you can just treat the std::ifstream as a bool which will be true when it is in a good state.
getline() returns a reference to the stream it operated on, so you can just put that whole line in the condition.
cosmetic, but no need to explicitly close the ifstream if it is about to go out of scope.