c++ wcout stops outputting, also wofstream stops outputting after some calls - c++

I tried to output contents to a file
std::locale::global(std::locale());
std::wofstream file(outfilename , std::wofstream::binary);
for (const auto & j : grid[0]) {
try {
std::wcout << L"String in WideString " << decoder->decode(j) << std::endl;
file << decoder->decode(j) << std::endl;
}
catch (std::exception& e) {
std::cout << e.what() << std::endl;
}
}
wcout stops outputting anything ( even "String in WideString" is not outputted ) after some amount of calls,
(I debugged it and it executes wcout like supposed to, after it stopped outputting text)
wofstream also stops outputting after the same amount of calls.
This is the first time I used the widestrings, streams and couts.
Thanks for looking into this.

it is the € sign, that stops wcout and wofstream from working, removing that from the input file, I get the data from, makes everything work like expected, very strange

Related

C++ programs not working correctly in VS Code [duplicate]

#include <bits/stdc++.h>
using namespace std;
void scan_a_line_indefinitely()
{
// scan line indefinitely
string input_line;
while(getline(cin,input_line))
{
cout << input_line ; **// doesn't print if i use this line**
//cout << input_line << endl; **// if i use this line, it works fine**
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
scan_a_line_indefinitely();
return 0;
}
someone please help me understand the problem with this code.
i think the problem is with cout.tie() and cout.tie(), when i remove these, program works fine.
std::cout will flush under these conditions:
An input-stream which is tied to std::cout tries to read input.
You removed the ties.
iostreams are synchronized with stdio, thus effectively unbuffered.
You disabled the synchronization.
The buffer is full.
That takes a bit longer.
The program ends normally.
That comes too late for you.
There is a manual flush (stream.flush() which is called when streaming std::flush; stream << std::endl is equivalent to stream << stream.widen('\n') << std::flush).
You have none of those.
So, fix any of them and you will see your output earlier.
If only iostreams are used you can add a manual flush to the output :
std::cout.flush();
Or
std::cout << /* the output */<< std::flush;
Also:
std::cout << std::endl is equivalent to std::cout << '\n' << std::flush

When i use cout.tie(NULL), program doesn't print anything for my code, but if i print endl, program works fine

#include <bits/stdc++.h>
using namespace std;
void scan_a_line_indefinitely()
{
// scan line indefinitely
string input_line;
while(getline(cin,input_line))
{
cout << input_line ; **// doesn't print if i use this line**
//cout << input_line << endl; **// if i use this line, it works fine**
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
scan_a_line_indefinitely();
return 0;
}
someone please help me understand the problem with this code.
i think the problem is with cout.tie() and cout.tie(), when i remove these, program works fine.
std::cout will flush under these conditions:
An input-stream which is tied to std::cout tries to read input.
You removed the ties.
iostreams are synchronized with stdio, thus effectively unbuffered.
You disabled the synchronization.
The buffer is full.
That takes a bit longer.
The program ends normally.
That comes too late for you.
There is a manual flush (stream.flush() which is called when streaming std::flush; stream << std::endl is equivalent to stream << stream.widen('\n') << std::flush).
You have none of those.
So, fix any of them and you will see your output earlier.
If only iostreams are used you can add a manual flush to the output :
std::cout.flush();
Or
std::cout << /* the output */<< std::flush;
Also:
std::cout << std::endl is equivalent to std::cout << '\n' << std::flush

tellg() returns -1 only for small files

I ran across a peculiar issue. Let's say I'm reading a file like this:
std::ifstream in("file.txt", std::ios::binary);
std::string text;
in.seekg(0, std::ios::end);
text.resize(in.tellg());
in.seekg(0, std::ios::beg);
in.read(&text[0], text.size());
The problem arises when the file contains less than 4 characters, i.e. "ab" or "abc", but works in other cases as intended, i.e. "abcd" or larger.
Why is tellg returning -1 for such a situation (ultimately causing my string to throw a std::length_error)?
Additional info:
I'm working with MSVC 15.5.3 (if not the latest, one of the more contemporary). Reproduced with GCC 5.1 as well.
This error doesn't occur with the equivalent C-style:
FILE* f = fopen("text.txt", "rb");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
EDIT:
failbit is set right before the first call to seekg, meaning opening the file failed? Why would that be the case for a file of less than 3 bytes...
After a few comments, it's clear that the ifstream constructor itself is failing in some way, as failbit is set even before the seekg call.
Since pretty much all I/O operation first construct a sentry object before proceeding, that will be why your operations are failing.
So I have a few suggestions.
First, use the full path name to your file just to ensure there's no possibility you're running it in a directory other than where the input file is.
Second, try the following complete program which works under g++ 5.4(a) to see if it exhibits the same problem (your code, while indicative, was not really complete).
#include <iostream>
#include <fstream>
int main() {
std::ifstream in("/full/path/to/file.txt", std::ios::binary);
std::cout << "after open, good=" << in.good() << ", bad=" << in.bad()
<< ", fail=" << in.fail() << ", eof=" << in.eof() << std::endl;
std::cout << "seekg returns " << in.seekg(0, std::ios::end) << std::endl;
std::cout << "after seek, good=" << in.good() << ", bad=" << in.bad()
<< ", fail=" << in.fail() << ", eof=" << in.eof() << std::endl;
std::cout << "tellg returns " << in.tellg() << std::endl;
std::cout << "after tell, good=" << in.good() << ", bad=" << in.bad()
<< ", fail=" << in.fail() << ", eof=" << in.eof() << std::endl;
}
Try this both with a two-byte and ten-byte file.
If none of that gives you any joy, Microsoft and/or GNU should be made aware of the issue. The former can be done here, the latter here.
Just for completeness, the only possibility that originally came to my mind was that the file, although three bytes long, is invalid in some way. This depends on the actual content so, if it is just abc, you can safely ignore this.
What I was thinking is something along the lines of a Unicode file with two byte BOM and the first byte of a multi-byte Unicode code point (e.g., UTF-16), or the first three bytes of of UTF-8 four-byte code point.
However, that seems incredibly unlikely if you're opening it in binary mode, so you can probably safely ignore it.
(a) For what it's worth, the only way I could get this to have failbit set after the open was to delete the file. Even using an empty file did not exhibit the problem you're describing.

Eclipse C++ console print order linux

I know there many subjects about this but none of them helps me.
I use in my C/C++ project std::cout and std::cerr to print info (cout) or error (cerr).
But when executing it they don't print in the right order, they seems to "group print". Sometime all cerr then all cout and sometime all cout first then all cerr.
I tried to flush() after every line, don't work. (luckily, it would be awful having to use it every time ...).
Also tried setvbuf(stdout, NULL, _IONBF, 0); same issue...
If run program directly in linux's console, order is good but eclipse console more useful due to colors.
Here code sample
#include <iostream>
int main(int argc, char** argv)
{
std::cerr << __LINE__ << std::endl;
std::cerr << __LINE__ << std::endl;
std::cout << __LINE__ << std::endl;
std::cerr << __LINE__ << std::endl;
std::cerr << __LINE__ << std::endl;
std::cout << __LINE__ << std::endl;
}
And console print
11
12
14
15
13
16
==> Wrong order ...
In this example cerr comes out before cout
Ok the situation is as follows, std::cout is added into a buffor, and std::cerr does not. std::cerr is faster and it does not need to be flushed. Also std::cerr and std::cout uses different streams.
The problem here is that std::cerr shows right away and std:cout needs to be flushed before it's showed.

File not saving, or not

I cannot seem to figure out why, during the while loop at the bottom,
std::cout << line;
does not print anything.
I believe that the test.txt file is not actually being written to because when I open test.txt in my folder, its empty. Any thoughts?
void Ticket::WriteTicket()
{
std::string ticketInput;
std::ofstream ticketFile("test.txt");
ticketFile.open("test.txt");
std::cout << "Please Enter Ticket Information: " << std::endl;
getline(std::cin, ticketInput);
std::cout << ticketInput << std::endl; //does print out the line
ticketFile << ticketInput;
ticketFile.close();
//here for testing only
std::string line;
std::ifstream ticketRead("test.txt");
while(getline(ticketRead, line));
{
std::cout << "something here?: " << line; // there is nothing here when it outputs
}
}
EDIT (SOLUTION):
After using some of the information that was given above, mainly from Basile Starynkevitch (I put this here because I cannot upvote yet), I was able to get the code to work!
I also did some research in my book and copied a similar program's style. Aka where to put what part of the code, and then the input worked. I continued on with the output and the key part was the std::ifstream::in in the opening of the file for output.
void Ticket::WriteTicket()
{
std::string ticketInput;
std::cout << "Please Enter Ticket Information: " << std::endl;
getline(std::cin, ticketInput);
std::ofstream ticketFile("Ticket.txt");
ticketFile << ticketInput << std::endl;
ticketFile.close();
//here for testing
std::ifstream ticketRead;
ticketRead.open("Ticket.txt", std::ifstream::in);
std::string line;
while(getline(ticketRead, line))
{
std::cout << line << std::endl;
}
}
Thank you for the help everyone!
You need to flush the output buffer.
ticketFile << ticketInput;
should be
ticketFile << ticketInput << std::endl;
std::endl flushes the output buffer.See std::flush if you don't want the new line.
C++ I/O is buffered. At least code
std::cout << "something here?: " << line << std::flush;
but in your case
std::cout << "something here?: " << line << std::endl;
would be better.
Also
std::ofstream ticketFile("test.txt")
should probably be
std::ofstream ticketFile("test.txt", std::ios_base::out);
I strongly suggest taking some hours to read more about C++ libraries before coding. Check every function or class that you are using. Of course, you also need to std::flush on ticketFile.
Maybe the file need to be openned in write mode.
Try this
std::ofstream ticketFile("test.txt","w");