Disabling Ctrl-Z behaviour in `ifstream::getline` - c++

I want to use ifstream::getline to read lines from a file, but the file contains the byte Ctrl-Z (0x1A), which makes getline think that it's reached the end of file (this is in Windows -- in *nix it would be Ctrl-D, if I'm not wrong). Can I disable this behaviour somehow, and tell getline to ignore Ctrl-Z?
Just for info: the file is comments.wast in the WebAssembly testsuite, which tests the ability of a WebAssembly parser to accept any and all characters inside a block comment.

This shouldn't happen when your ifstream object is using binary mode.
Just create it like this:
std::ifstream fin(<path>, std::ios::binary)
This of course prevents you from using other features of the stream, but if your main goal is to be able to read all sort of values while ignoring special characters, this is probably the way to go.

Related

How to get raw bytes written to ostream by an external library without creating a file

(My previous questions was closed as a duplicate of Are there binary memory streams in C++ which is ridiculous, since i can't change the implementation of the library I'm using)
I'm using a library (Poco) to create zip files. It takes ostream as an input and writes the data of the zip file into it. Something like:
std::ofstream ofs("file.zip", std::ios::binary);
Compress compress(ofs);
// add data to compress ...
compress.close();
// now file.zip contains added file
This works. But I want to be able to create a zip in memory without creating a file. I tried using stringstream instead of ofstream, i get additionl newline characters in the data in the zip file is corrupted. Is there any other stream i can use?
(If someone still thinks it's a duplicate, I'm gonna need an explanation, since I don't see how this other question is helpful for me)
Use a std::stringstream -- that will create an in-memory string that you can write to as an ostream, and WILL NOT add extra newlines. If you later copy the string to an fstream that was opened in text mode (such as std::cout), then that process may add extra CR characters that are not in the string (nor in the original output).
If you are seeing extra characters corrupting your stream, they are coming from somewhere else -- something besides you compress call/lib is writing to the stream, or something with how you are looking at your stream is doing something.
If you're on linux, how about creating an anonymous file using memfd_create? You can then open /proc/self/fd/<fd> and do your stuff. Some implementations of std::ofstream may even provide a constructor that takes a FILE*, you can check if that's the case on your system.

Is there a character that can be used as a delimiter for EOF?

I wrote a function that spellchecks a line read from a file which takes in a file stream and a delimiter as parameters. My problem is that the function requires a delimiter, but when reading in the last line, I haven't got one. I would use the last character of the file, but I need that last character for spellcheck purposes.
Is there any way to use the EOF macro as a delimiter?
Typically, you would let the stream tell you when it has received an EOF signal in whatever platform-dependent way is appropriate (be that the end of a file, or Ctrl+D on a Linux terminal emulator).
So, stop reading when you hit your custom delimiter, or when an attempt to read from the stream sets the stream's EOF bit. You ought to be checking the stream's state anyway — what if there's an error? You'll be looping forever at the moment.
That's how std::getline and co do it, anyway.

preferred c++ i/o stream method: fstream or ifstream/ofstream or something else entirely?

I have created a roster program that accepts user input to create/write/delete information into and out of a specified text file. My issue now becomes wanting to create a lasting text file that isn't overwritten every time I re-run the program and am not sure if using fstream or a combination of of/ifstream is better practice, or if there is maybe a third option I missed when checking the reference docs.
Right now I am simply using: std::ofstream outfile("roster.txt"); which works, until I kill and re-run the program to which my text file is now wiped clean.
check out the append flag. it writes to the end of an existing file.
http://www.cplusplus.com/doc/tutorial/files/
example here.
std::ofstream outfile("roster.txt" , ios::app)

when would failbit be set while executing a getline function call in c++

when would getline in c++ fail?
I have a big snippet of code which I am unable to paste in its entirety for multifarious reasons. I am trying to read from a file , which I know exists and contains data, using getline in C++. But getline fails returning error 123-invalid name(output of getlasterror). I looked up the error code which baffles me even more.
I do error check while opening the file. So I am positive that I have the handle to the file.
Please bear with me for not pasting the code. I am new to c++ and especially in windows. Any suggestions or insights about getline would help. I am trying to read a file which is dumped by a compiler.
This is an extract from the code
ifstream inFile("C:\...\ash.txt",ios::in);
string singleLine;
getline(inFile,singleLine);
singleLine is empty ! I am doing something silly..pls point that out to me! appreciate it
IMPORTANT EDIT:
I checked for the ios members and found that fail bit is set. Why would the fail bit be set? the file does exist and also I was wondering if how windows exposes file extensions could cause a problem . That doesnt seem to the problem. What am i missing?
GetLastError only tells you about Win32 API calls, which std::getline is not.
Check the members of the iostream that failed, e.g. rdstate().
Calling ios::exceptions(eofbit | failbit | badbit) before getline and catching the resulting exception might or might not get you a more descriptive error message.
If the fail bit is set, it is probably because you didn't successfully open the file. Check whether ifFile.is_open() returns true; if not, then then probably indicates that the file is not open correctly. You might not have permissions, or you may need to escape the string properly, or the file may be locked.
You should also check if fail is set both before and after the call to getline. If it's before, that probably means that the file isn't open. If it's after, it could mean that the file is empty.
It may be the case that Windows is hiding the true file extension from you. The file name might actually be named ash.txt.txt, for example, if you have Explorer configured to hide file extensions. That might be worth investigating if the file isn't open.
Do you need to escape the backslashes in the file path?
It failed to open the file.
This is becuase you did not specify the correct path.
This is because you used the ancient windows convention of \ as a path separator.
Which also happens to be the escape character in C.
Which is why Windows lets you use / as a path separator (and has done for over a decade) because the use of '/' is so error prone.
Your path should be:
ifstream inFile("C:\\...\\ash.txt");
// Or my preference
ifstream inFile("C:/.../ash.txt");
Or even better use boost.
Getline IIRC is meant for c_strings and the string is the first argument, not the second.
http://www.cplusplus.com/reference/iostream/istream/getline/
You're attempting to use a normal string, which (I could be wrong) doesn't work with getline.

How to Determine if STDIN is Empty?

I'm writing an emulator for my Operating Systems course. The problem I have is that we need to get all our .job files (they are like application programs being fed to the emulator) from STDIN and read them in.
Call:
./RMMIX < aJob.job
I just slurp it with
while(getline(std::cin, line))
line by line. The problem is, if I do not put anything to STDIN, then cin will wait for user input- NOT what I want. I need the program to recognize a lack of text on STDIN and terminate, not wait for user input instead.
I have determined that I can query the length like so:
size_t beg = std::cin.tellg();
std::cin.seekg(0, std::ios_base::end);
size_t end = std::cin.tellg();
std::cin.seekg(0, std::ios_base::beg);
and terminate if std::cin has a length of 0.
Are there any other solutions to this? Is this a portable solution?
I don't think there's a platform independent way of doing this, but on Unix-based systems you should be able to do:
#include <unistd.h>
...
int main() {
if (!isatty(0)) {
// stdin is being streamed from a file or something else that's not a TTY.
}
...
}
However, I think doing it via a command line option is the preferred approach.
You need to redesign your program. Instead of reading from standard input, read from a named file, who's name you provide on the command line. Then instead of:
./RMMIX < aJob.job
you say:
./RMMIX aJob.job
This is much easier and more portable than trying to determine if there is anything in standard input.
You might also look at this http://www.programmersheaven.com/mb/CandCPP/232821/232821/non-blocking-reads-on-stdin/ for an idea that comes at the problem from another direction -- don't check the number of bytes on the stream, but instead just make the read succeed immediately and then check to see if anything was read.
You can press Ctrl+D on the command line to signal end-of-file for standard input on the running program.
This is desired behavior. Otherwise, if programs exited immediately when no input remained, pipelines could randomly be broken by commands that were waiting on another command that had not been scheduled to run (and that had not produced any additional output), or that buffered output and emitted it all at once, like sort does.
When using io redirection to pull stdin from a file via something like ./RMMIX < file.txt, this end-of-file condition is signaled automatically when there is no more data left in the file. For input read from a terminal, waiting is probably the desired behavior.