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
}
}
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:
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?
So, I want my program to read data from a file, and save it into different quarter1, quarter2,quarter3, quarter4 depending of it's date, but it doesn't seem to work properly and still don't know why, I've been trying to debug and I'm pretty sure it fails when saving at saveQuarters or existeix which is basically a dichothomic search which returns if the code exists and if it exists, it returns the position. This is the code:
I just skimmed through some of the stuff you had so this suggestion may not work, but you can try declaring your file as input or output. Perhaps that could be the problem.
Some thing like:
string fileName = "data.txt";
ifstream dataFile;
dataFile.open(fileName, ios::in);
Doing this:
fitxerCens >> taulaCens[i].stateName;
Will grab an entire line of the data file until it sees a space is correct.
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.
I'm using "getline" to read some lines in a text file. It works as it should, but I'm calling the method multiple times.
while(getline(file, line))
{
//Do something
}
//More code in between
while(getline(file, line))
{
//Do something else
}
The problem is that when I call "getline" the second time it starts reading from where it previously finished (e.g. If the first while loop ends at the second line then the next loop starts at the third line). How can I ensure that my program reads the file from the first line every time?
If you need that same first line multiple times I think you should reconsider your strategy.
Just read the line once.
Save it in a variable (or just keep it in the variable "line" you already have).
Close the file.
You would avoid a lot of not necessary I/O operations...
Nonetheless as other people suggested if by any reason you want to procede with this approach you need to insert:
myinputstream.clear(); //clear the buffer
myinputstream.seekg(0, ios::beg); //reset the reading position to beginning
between each attempt to read the same file.
And do not forget to close it eventually.
myinputstream.close();
There's a seekg() function that should help
http://www.cplusplus.com/reference/istream/istream/seekg/
iostream::seekg (0, iostream::beg);
will move you at the beggining of the stream