Fstream reading by char with while - c++

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.

Related

File Streams of Programming 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

The program doesn't seem to be saving the input data correctly (c++)

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.

C++ - Missing end of line characters in file read

I am using the C++ streams to read in a bunch of files in a directory and then write them to another directory. Since these files may be of different types, I am using a the generic ios::binary flag when reading/writing these files.
Example code below:
std::fstream inf( "ex.txt", std::ios::in | std::ios::binary);
char c;
while( inf >> c ) {
// writing to another file in binary format
}
The issue I have is that in the case of files containing text, the end of line characters in these text files are not being written to the output file.
Edit: Or at least they do not appear to be as when the newly written file is opened, there is only a single continuous line of characters.
Edit again: The problem (of the continuous string) appears to persist even when the read / write is made in text mode.
Thus, I was wondering if there was a way to check if a file has text or binary and then read/write it appropriately. Else, is there any way to preserve the end of line characters even when opening the file in binary format?
Edit: I am using the g++ 4.8.2 compiler
When you want to manipulate bytes, you need to use read and write methods, not >> << operators.
You can get the intended behavior with inp.flags(inp.flags() & ~std::ios_base::skipws);, though.

Deleting a specific line in a txt file using istream/ofstream in c++

Here is the code I'm having a trouble with, I have a .txt file that contains a list of users and their passwords using this format: user;password.
I need to search for a user in the file and then delete the line which contains this user.
void deleteauser()
{
string user;
cout<<"Which user do you wish to delete?";
cin>>user;
string line;
string delimiter=";";
string token,token1;
ifstream infile;
infile.open("users.txt",ios::in);
while (getline(infile,line,'\n'))
{
token = line.substr(0, line.find(delimiter));
token1=line.substr(token.length(), line.find('\n'));
if(token==user)
{
//here i need to delete the line of the user that has been found
}
}
infile.close();
}
Read the input file, line by line, writing to a temporary file. When you find lines you don't want then just don't write them to the temporary file. When done rename the temporary file as the real file.
To edit a file you have 2 options:
Read in every line and write out those you want to keep
Seek to the part of the file you want deleted and replace the text with spaces (or similar)
You have the first half pretty much done - just write out what you read to a temporary file and delete/rename to make it the original.
For the second option, you can write to the input file at that point if you use an iofstream (be aware of buffering issues). The better option is to use seekp or seekg to get to the right point before overwriting the file.

ifstream file opening issues

I am having a hell of a time trying to open files on a network drive with ifstream.
I can successfully open the file if... I explicitly declare the filename, such as ifstream f("filename.txt").
However, that is the only way I can get the file open, and I need be able to dynamically find the name of that file and open it. Right now I have a string vector of the filenames in a given folder.
I have tried the following as input arguments to the ifstream constructor, to no success.
converting the string using c_str().
declaring a char* and assigning the string.cstr() to it.
same as above, but const char*
changed the system directory to the folder where the file is, and inputted the filename itself (relative path)
I print out the filename and change to its directory each time before trying to open it, so I know for a fact the filename is 100% correct.