I'm trying to print the text "Ääkkösiä ruutuun." to console with c++. I have windows 7 and am using Code::Blocks editor. Searching on the subject I found that maybe these sort of lines would help
_setmode(_fileno(stdout), _O_U16TEXT);
wstring s{L"Ääkkösiä ruutuun."};
wcout<<s<<endl;
But when I try to compile it, I get the error: _fileno was not declared in this scope.
I have all these includes:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stdexcept>
#include <cmath>
#include <sstream>
#include <fstream>
#include <codecvt>
#include <locale>
#include <fcntl.h>
#include <io.h>
#include <stdio.h>
#include <cstdio>
#include <ostream>
what am I missing?
Also, one other thing I tried was locale, but then locale::empty wasn't found! Why doesn't my c++ have anything in it?
EDIT
Here is a picture of what my program is doing now.
It prints out just the first letter (Ä). What happens to the rest?
Ok, it seems that setmode sets it so that only one letter gets printed. (Even trying to print normal texts with multiple commands, just results in a single letter.) Without it the scandinavian letters don't print correctly, thought. They look like this:
The answer you found is for Visual Studio, not Code::Blocks.
While the C standard specifies what should in in <stdio.h>, it only specifies a minimum. Implementors may add their own functions, and should do so using an _ (underscore prefix). This is why you should NOT use that prefix. You don't know what you'll break. Microsoft clearly signaled their non-standard extensions using the correct prefix.
The answer is tagged C++, but C++ inherits the contents of <stdio.h> from C.
The line
setlocale(LC_CTYPE, ".OCP");
works!
A complete example:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <fstream>
using namespace std;
wstring readFile(const char* filename) {
wifstream wif(filename);
locale myLoc("");
//locale utf8_locale(locale(), new gel::stdx::utf8cvt<true>);
wif.imbue(myLoc);
basic_stringstream<wchar_t> wss;
wss << wif.rdbuf();
return wss.str();
}
int main() {
setlocale(LC_CTYPE, ".OCP");
wstring contents = readFile("test.txt");
wcout<<L"Does anything get printed out at all???"<<endl;
//wcout <<contents<<endl;
wstring s{L"Ääkkösiä ruutuun."};
wcout<<s<<endl;
wcout<<L"Näkyykö äkköset?"<<endl;
return 0;
}
The text read from file (utf-8) still doesn't print correctly, though.
It should be
Hei!
Täällä on kaksi riviä.
ä's go awry there.
Output:
Related
my question is pretty simple but I can't seem to find it out.
I want to know what libary to include when using stoi. I was using atoi and it works fine with
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
but I get "stoi not declared" when I run with stoi. Thanks
You need to #include <string> and use a compiler that understands C++11. Minimal example:
#include <string>
#include <cassert>
int main()
{
std::string example = "1234";
int i = std::stoi(example);
assert(i == 1234);
return 0;
}
Compile, for example, with g++ -std=c++11.
I am doing a GUI program in C++ and I use OpenSuse with KDE Plasma. I would like to copy to clipboard a string. How can I accomplish that?
Here is my code:
#include <wx_pch.h>
#include <wx.msgdlg.h>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>
void SimpleGUIFrame::CopyToClipboard(string str)
{
// copy str to cliboard
}
#include <iostream>
#include <time.h>
#include <string.h>
#include <stdio.h>
int main()
{
string msg;
printf("Enter the message that you wish to display as scroller: ");
getline(cin,msg);
msg=msg+". ";
int x=0;
while(1)
{
Scroll(msg);
wait(100);
system("cls");
x++;
}
cin.get();
return 0;
}
I Have this C code and all strings in the file say 'identifier "string" is undefined'. I tried including <string> instead of <string.h> but it didn't work. Why is it not working?
Add
using namespace std;
After includes (but before main). Or, better, use notion of:
std::string // instead of string
Update: I missed the point of this being C-question. I will leave this answer, but for the sake of formality, use it if you came from Google and you are working with C++.
This is C++ code, not C.
The compiler is probably getting confused because it cannot parse it, so then it finds C-like code and all identifiers do not exist.
The includes should be:
#include <iostream>
#include <ctime>
#include <string>
#include <cstdio>
You are also missing a:
using namespace std;
Plus the definitions for Scroll and wait etc.
My problem is pretty much what the title says:
When I use STRG-ALT-F to format my C++ files VSC also messes with the order of #includes.
And of cause this leads to errors that were not there before… :-(
For example this
#include "soc/soc.h"
#include "soc/gpio_sig_map.h"
#include "soc/i2s_reg.h"
#include "soc/i2s_struct.h"
#include "soc/io_mux_reg.h"
#include "driver/gpio.h"
#include "driver/periph_ctrl.h"
#include "rom/lldesc.h"
#include "XClk.h"
#include "DMABuffer.h"
becomes this
#include "DMABuffer.h"
#include "XClk.h"
#include "driver/gpio.h"
#include "driver/periph_ctrl.h"
#include "rom/lldesc.h"
#include "soc/gpio_sig_map.h"
#include "soc/i2s_reg.h"
#include "soc/i2s_struct.h"
#include "soc/io_mux_reg.h"
#include "soc/soc.h"
… and subsequently fails compile.
Is there a way to forbid the formatter reordering and stick to formatting stuff?
Using PlatformIO in VSC on OSX.
I am using jni and want to read file from path, i used:
while (std::getline(file, str)) {
...
}
but it get an error : Function 'getline' could not be resolved, i have added:
#include <vector>
#include <string.h>
#include <jni.h>
#include <fstream>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <iostream>
and they are Ok. How do i resolved this problem? Please help me.
use:
#include <string>
instead of:
#include <string.h>
The latter one is the C variant and will not have the std namespace. Also see:
Difference between <string> and <string.h>?