File Streams of Programming C++ - c++

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

Related

Reading multiple files and check for EOF

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?

Data isn't being saved in the text file (C++ Fstream library )

I've been trying to save the score of the player in the game in a text file, but it doesn't do so.
This is the code I'm using:
//some code above
std::fstream TextScore ("Ranking.txt");
// some code above
if (Player->getFinal(Map) == true)
{
TextScore.open("Ranking.txt", ios::out);
TextScore << Player->getPoints();
TextScore.close();
//some code below
}
Then I check the text file and nothing has been saved, the file is empty.
I would like to know what I'm missing or doing wrong.
Thanks in advance.
std::fstream TextScore ("Ranking.txt");
This opens the file, as if TextScore.open("Ranking.txt"), std::ios::in|std::ios::out) was called.
TextScore.open("Ranking.txt", std::ios::out);
This opens it again.
The combination is not going to work if the file already exists. The first open will succeed and the second one will fail. After that, all I/O operations will fail. Open it just once, either in the constructor or in a separate open call. The most idiomatic C++ way would be
{
std::fstream TextScore ("Ranking.txt", std::ios::out);
TextScore << Player->getPoints();
}
No need to close the file explicitly thanks to RAII.
Opening the same file twice is certainly going to cause problems. Move the definition of TextScore into the body of the if statement in place of the call to TextScore.open(). And then you can remove the call to TextScore.close(); the destructor will close the file.

How to leave a file open and continue from where we left last time? C++

Is there is a way to read in from a file until the end of a line, then go to another function do something, then come back afterwards to read in from the same file BUT from where we stopped last time (not from the beginning of the file)?
If yes, please provide a snippets. Code makes more sense to me than words. Thanks
When opening a file for writing using std::fstream::open(), you have the options to set the file openmode as the second argument after the filename...
std::fstream file;
file.open("myfile.txt", ios_base::openmode::ate);
openmode::ate is the flag used to open the file with the input cursor positioned at eof.
http://www.cplusplus.com/reference/ios/ios_base/openmode/
http://www.cplusplus.com/reference/fstream/fstream/open/
http://www.cplusplus.com/reference/fstream/fstream/

Program Almost Runs ,Trouble With File Operation

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.

Fstream reading by char with while

So I have a code like this:
fstream abc;
abc.open(fileName);
while(abc.get(currChar))
{
if(currChar==' ') ++spaces;
}
abc.close();
And I want it to count spaces in the given .txt file reading one char at a moment until EOF. However, the above code always gives me 0s as the spaces. Why?
I just tried your code, and it works. I believe the problem is what sftrabbit said, the path of the file is incorrect.
If your text file is in the same folder as your main.cpp, the path is simply the name of the text file (e.g. textFile.txt).
If it's somewhere else, for example, in your C drive, then the path should be C:/textFile.txt.