How to explain the strange output by "puts" in c++? [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am reading a piece of code written by others, there is one line like this:
cout << "Data Loaded" << endl;
it seems nothing strange, however, the actually output is:
[18607330327, 18790481919] [19144201237, 19327352831] [20754813973, 20937965567] [21291684885, 21474836479] [21474836482, 21653864362] [22011707392, 22190735274] [23622320128, 23801348010] [24159191040, 24338218922] [27197264917, 27204255743] [27205653525, 27212644351] [27230819349, 27230959453] [27233615872, 27235153757] [30064771072, 30067638186] [30073159680, 30076026794] [30098325504, 30098440106] [30098456576, 30098536200] Data Loaded
where does the extra output come from? if I comment that line, then, nothing is output.
I then include the <cstdio> and replace that line by puts("Data Loaded"), still, the extra info get printed.

cout is a buffered output stream, and endl not only creates a new line, it also flushes the buffer. Without the flushing of the buffer it might happen that you do not see the output of a previous cout.

Related

"string too long" crash, c++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 days ago.
Improve this question
im crashing on xstring with this error, can't figure out why
error:
the piece of code where its crashing on:
std::string response = f_network.request_to_server((xorstr_("a=") + crypt.rsa_enc(params.m_cUsername) + xorstr_("&b=") + crypt.rsa_enc(params.m_cPassword) + xorstr_("&c=") + crypt.rsa_enc(f_main.get_hwid()) + xorstr_("&pc=") + f_main.base64_encode(reinterpret_cast<unsigned char*>(hwid_part.data()), hwid_part.length())));
im using VS 2022, with the latest SDK
I already enabled multi-byte character set, but that didn't change anything, im desperate
this is a request to my API, where it asks for the username and password, i don't know why its crashing tho, i've tried everything i could, and it doesn't seem to work

How to get available space on a network drive in c++? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I tried using std::filesystem::space(dir) but with no luck -> it cannot determine available disk space, sets it to uintmax.
auto info = std::filesystem::space("K:\\Dir");
if(info.available == static_cast<uintmax_t>(-1))
{
std::cout << "Error occurred!\n";
}
K:\Dir does indeed exist. And this snippet prints Error occurred for my drive mounted on K:.
If you want to get the free space you need to try
const std::filesystem::space_info spaceInfo = std::filesystem::space(dir);
cout << static_cast<std::intmax_t>(spaceInfo.free) << endl;
Here dir = "/path/to/dir/";
Refer cppreference

How to supress display of \n after input in c++? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have written a simple FORTH-like interpreter where I get input a line at a time and process it like this:
std::string line;
while (!std::cin.eof()) {
std::getline(std::cin, line);
std::istringstream input(line);
std::copy(std::istream_iterator<std::string>(input),
std::istream_iterator<std::string>(),
std::back_inserter(data.input_));
// some work
}
And if I input 5 5 + . <enter> it displays like this:
5 5 + .
10 ok
But what I would like it show is:
5 5 + . 10 ok
In other words a newline should be accepted to mean end of line input but it should not be echoed to std::cout.
Having done some reading it seems I will not be able to achieve this in std c++ (is this true?) but I can use the functions in the termios library to set up the console this way on a POSIX system without messing with other console functions such as ctl-c handling etc. I haven't figured out exactly how though. Can anyone show me the way?
You can Use VT100 Escape Sequences for customising console output (VT100 ESC SEQUENCES). in this case;
use \033[1A for go 1 line up, \033[nC (n is int value) to Go n letter right

Why file is not created in /etc/ directory [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Please find the code sample
void createFile(const std::string& FileName, const std::string& Content)
{
ofstream of(FileName.c_str());
of<<Content;
of.close();
}
const std::string testFile = "/etc/testFile";
const std::string EmptyContent = "";
createFile(testFile, EmptyContent);
File is not creating at /etc/ directory. I think this is related to permissions. What extra I have to add in the code to work.
There's nothing extra that you can add to this program to "make it work". If an arbitrary program can write to /etc, this would toss the traditional POSIX security model out the window.
In order to be able to write to /etc, your program must be executed as root.
It seems to be a permission issue. Try to run your program using sudo:
sudo yourprogram

My output will not write to a file [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Here is the code i wrote:
#include <stdio.h>
#include<stdlib.h>
int main(void)
{
float x;//the data value
float sum=0.0;//the running total of the values readd
int n=0;//the number of data values read
float cube;
FILE* fin = fopen("sumcubes.in", "r"); /* open for reading */
FILE* fout=fopen("sumcubes.c","w");
while(1){
fscanf(fin,"%f",&x);
if(feof(fin))break;
n++;
cube=x*x*x;
sum=sum+cube;
}
fprintf(fout,"There are %d values in the file:",n);
fprintf(fout,"The sum of cubes of these values is:%f",sum);
fclose(fin);
fclose(fout);
system("sumcubes.in");
system("sumcubes.c");
system("pause");
return 0;
}
I was expecting it would read the values in the file and then cube each value. after cubing each value in file it will get the sum of these cube values. however nothing was printed to the output.
When I ran your code (after Ken Y-N fixed the typo), it worked just fine, except that the three system() calls generated "command not found" error messages. The file sumcubes.c contained the expected results, though why you would give that file a .c extension baffles me. Perhaps the file sumcubes.in is not in your current directory when you are running the executable? When I tried that, I got a segmentation fault, but since you don't check the results of fopen(), if the input file does not exist (or is not in your current directory), anything could happen.