简体   繁体   English

向量弄乱了std :: cout吗?

[英]vector messing with std::cout?

I'm trying to make a progress bar for a copy of a portion of a disk to another. 我正在尝试制作一个进度条,用于将磁盘的一部分复制到另一部分。 I am using a progress bar class that deal with printing the progress bar that I found on stack exchange ( https://codereview.stackexchange.com/questions/186535/progress-bar-in-c ) that I have slightly modified to allow more precision (showing the decimal). 我正在使用一个进度条类,该类用于打印我在堆栈交换( https://codereview.stackexchange.com/questions/186535/progress-bar-in-c )上找到的进度条,对此我做了一些修改,以允许更高的精度(显示小数)。

However to copy the files i use a vector, and if I put the declaration of the progress bar object after the declaration of my vector I have part of the content of the vector that is showing instead of the progress bar. 但是,要复制文件,我使用了一个向量,如果我在向量的声明之后放置了进度条对象的声明,那么我将显示部分向量的内容,而不是进度条。

here is a code sample that does not work: 这是无效的代码示例:

vector<char> buff(512);

progress_bar progress{std::cout, 70u, "Formating"};

int fd=open("/dev/sdc1",O_RDONLY);
int fd2=open("/dev/sdb1",O_RDWR);
double NbOfBlocks = 11800.0;
if (fd>0)
{
    for (int i = 0.0; i<NbOfBlocks; i++)
    {
        progress.write(i/NbOfBlocks);
        read(fd,&buff[0],512*i);
        write(fd2,&buff[0],512*i);
    }
}

If I do : 如果我做 :

progress_bar progress{std::cout, 70u, "Formating"};

vector<char> buff(512);

int fd=open("/dev/sdc1",O_RDONLY);
int fd2=open("/dev/sdb1",O_RDWR);
double NbOfBlocks = 11800.0;
if (fd>0)
{
    for (int i = 0.0; i<NbOfBlocks; i++)
    {
        progress.write(i/NbOfBlocks);
        read(fd,&buff[0],512*i);
        write(fd2,&buff[0],512*i);
    }
}

Everything works fine. 一切正常。

Any Idea why or how to avoid it ? 任何想法为什么或如何避免呢?

read(fd,&buff[0],512*i);

当i> 0时,缓冲区溢出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM