|
Apologies for the shouting but this is important.
When answering a question please:
- Read the question carefully
- Understand that English isn't everyone's first language so be lenient of bad spelling and grammar
- If a question is poorly phrased then either ask for clarification, ignore it, or mark it down. Insults are not welcome
- If the question is inappropriate then click the 'vote to remove message' button
Insults, slap-downs and sarcasm aren't welcome. Let's work to help developers, not make them feel stupid.
cheers,
Chris Maunder
The Code Project Co-founder
Microsoft C++ MVP
|
|
|
|
|
For those new to message boards please try to follow a few simple rules when posting your question.- Choose the correct forum for your message. Posting a VB.NET question in the C++ forum will end in tears.
- Be specific! Don't ask "can someone send me the code to create an application that does 'X'. Pinpoint exactly what it is you need help with.
- Keep the subject line brief, but descriptive. eg "File Serialization problem"
- Keep the question as brief as possible. If you have to include code, include the smallest snippet of code you can.
- Be careful when including code that you haven't made a typo. Typing mistakes can become the focal point instead of the actual question you asked.
- Do not remove or empty a message if others have replied. Keep the thread intact and available for others to search and read. If your problem was answered then edit your message and add "[Solved]" to the subject line of the original post, and cast an approval vote to the one or several answers that really helped you.
- If you are posting source code with your question, place it inside <pre></pre> tags. We advise you also check the "Encode HTML tags when pasting" checkbox before pasting anything inside the PRE block, and make sure "Ignore HTML tags in this message" check box is unchecked.
- Be courteous and DON'T SHOUT. Everyone here helps because they enjoy helping others, not because it's their job.
- Please do not post links to your question in one forum from another, unrelated forum (such as the lounge). It will be deleted.
- Do not be abusive, offensive, inappropriate or harass anyone on the boards. Doing so will get you kicked off and banned. Play nice.
- If you have a school or university assignment, assume that your teacher or lecturer is also reading these forums.
- No advertising or soliciting.
- We reserve the right to move your posts to a more appropriate forum or to delete anything deemed inappropriate or illegal.
cheers,
Chris Maunder
The Code Project Co-founder
Microsoft C++ MVP
|
|
|
|
|
I need to do the following request within a C++ app (response is json):
curl --header "x-api-key:ABCD" -s https:
1. Can I replace curl with another protocol ? (This is not mandatory, could be as it is now)
2. Is there a header only c++ lib that can complete this request ? Of course, I know there is REST SDK library, but it is too much trouble for that simple request.
Thank you.
modified 17hrs ago.
|
|
|
|
|
curl isn't a protocol, as such, but a CLI tool for data transfer using urls. The underpinnings are based on libcurl, a C library, that implements the data trasfer. As such, you can make API calls directly from your C++ program. Another option would be to look into popen() e.g.
#include <cstdio>
int main()
{
FILE *pipe = popen("curl --header \"x-api-key:ABCD\" -s https://api.test.se/api/mydata", "r");
while( {
}
fclose(pipe);
}
Keep Calm and Carry On
|
|
|
|
|
Hmm ... I tried:
FILE* pipe = _popen("curl --header \"x-api-key:ABCD\" -s https://api.test.se/api/mydata", "r");
while (true)
{
pipe->_Placeholder;
break;
}
fclose(pipe);
but I cannot retrieved any data from pipe ...
|
|
|
|
|
using a C style FILE * you have to use C stdio functions. So for example you could do
#include <stdio>
#include <cstdlib>
FILE *pipe = popen( ... );
char *buff = NULL;
size_t blen = 0;
while( getline(&buff, &blen, pipe) > 0) {
}
free(buff);
I generally prefer getline() to fgets() when using cstdio because it allocates and grows the input buffer as needed.
If you wish to stick with a more C++ style interface, then maybe look into Boost.Process
I think you've mentioned QT as part of your framework, so there's QProcess as well.
Keep Calm and Carry On
|
|
|
|
|
The C++ app is MFC app ...
|
|
|
|
|
For some reason, I was assuming you were using linux. If you're on windows, there's no getline in the C stdio library, so you'll have to use fgets() or another C stdio FILE i/o function.
It might be a better choice to use the libcurl API directly. Or another C/C++ client library. A quick google search turned up Windows HTTP Services - Win32 apps | Microsoft Docs , but that appears to be a Win32 API, so may not be applicable to your situation. There's also GitHub - embeddedmz/httpclient-cpp: C++ client for making simple HTTP requests which might work for you too. I have not tried this package, so cannot speak to its quality or suitability for any purpose, what so ever. Caveat usor.
Keep Calm and Carry On
|
|
|
|
|
|
std::getline() isn't part of C stdio, its part of C++. The getline with the signature int getline(char **, size_t *, FILE *) (i.e C stdio, thus the FILE * parameter) is not provided by MS in the windows C/C++ environment. Or at least it wasn't with VS 2017. It might have been added since then, but I don't think so.
Keep Calm and Carry On
|
|
|
|
|
I have tried:
std::ifstream ifs("curl --header \"x-api-key:ABCD\" -s https://api.test.se/api/mydata");
while (ifs.good())
{
std::string line;
while (std::getline(ifs, line))
{
std::cout << line.c_str() << std::endl;
}
}
Nothing retrieved.
|
|
|
|
|
std::ifstream reads files, and does not have a constructor which executes an external program. You should go back to pure C and use the _popen function.
[edit]
This all you need:
char buffer[132];
FILE* pipette = _popen("curl --header \"x-api-key:ABCD\" -s https://api.test.se/api/mydata", "r");
while (fgets(buffer, 132, pipette))
{
printf("%s", buffer);
}
[/edit]
modified 11hrs ago.
|
|
|
|
|
Yes, that seems to work. Thank you all of you !
|
|
|
|
|
Of course not, unless you actually have a file in your current directory called curl --header "x-api-key:ABCD" -s https://api.test.se/api/mydata.
Keep Calm and Carry On
|
|
|
|
|
I tried to do a basic calculation for testing the performance of different programming languages:
C/C++: (***3.05 s***)
int main()
{
int start = clock();
double result;
for (size_t i = 1; i < 100000000; i++)
{
result = sin((i * 25) / i * i);
}
int end = clock();
std::cout << "it took " << end - start << "ticks std::endl;
}
C#: (***2.452 s***)
using System.Diagnostics;
class TestClass
{
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
double result;
sw.Start();
{
for (int i = 1; i < 100000000; i++)
{
result = Math.Sin((i * 25)/i * i);
}
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds.ToString());
}
}
And the same calculation in Python took 22.14 s to complete
Why is C# faster than C++? 
|
|
|
|
|
I'd expect all "intermediate" calculations in C# to be performed as "int", since you have not supplied a single float or double on the right of the equation.
And you neglected to display / verify "result".
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
Note that Math.Sin is implemented in native code, see the Reference Source (not for the implementation, but for the fact that there's no implementation there). So this isn't really a benchmark of C# vs C++, but one sine written in C++ vs another sine written in C++.
|
|
|
|
|
Both of the previous answers are correct.
On my machine, with VS2019, this code
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
double result=0.0;
sw.Start();
{
for (int i = 1; i < 100000000; i++)
{
result += Math.Sin((i * 25.0) / i * i);
}
}
sw.Stop();
Console.WriteLine(sw.ElapsedMilliseconds.ToString());
Console.WriteLine(string.Format("{0}",result));
} produces
4739
-7,98150964297362
While the following
int main()
{
int start = clock();
double result = 0.0;
for (size_t i = 1; i < 100000000; i++)
{
result += sin((i * 25.0) / i * i);
}
int end = clock(); std::cout << (1000.0 * (end - start) / CLOCKS_PER_SEC) << "\n";
std::cout << result << "\n";
} outputs
4153
-7.98151
"In testa che avete, Signor di Ceprano?"
-- Rigoletto
|
|
|
|
|
how to create
image background datagrid
|
|
|
|
|
|
Anything not in the foreground, is considered as being in the background.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
could you guys recommend me a file IO library. I need not just the library itself but also some explanations to it, like an article.
|
|
|
|
|
Not sure what you're looking for. C and C++ have file I/O.
For C++, start here[^]. Directory-level stuff was added in C++17 as <filesystem>[^].
For C, look here[^].
|
|
|
|
|
thanks Greg, I find that useful feedback
|
|
|
|
|
Is there something wrong with standard libraries ifstream/ofstream? If that's not to your liking, for some reason, maybe boost io or iostreams?
Without a bit more context, it's not clear what you're looking for.
Keep Calm and Carry On
|
|
|
|