Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
只有第一个缓冲区在C+中实现的双缓冲区中工作+; 我实现了一个双缓冲程序,它读取带有代码的缓冲区的TXT文件,需要在Internet上使用代码和C++处理,然后输出处理结果为Output.txt格式,但不起作用。_C++ - Fatal编程技术网

只有第一个缓冲区在C+中实现的双缓冲区中工作+; 我实现了一个双缓冲程序,它读取带有代码的缓冲区的TXT文件,需要在Internet上使用代码和C++处理,然后输出处理结果为Output.txt格式,但不起作用。

只有第一个缓冲区在C+中实现的双缓冲区中工作+; 我实现了一个双缓冲程序,它读取带有代码的缓冲区的TXT文件,需要在Internet上使用代码和C++处理,然后输出处理结果为Output.txt格式,但不起作用。,c++,C++,以下是我想做的: producer routine buf1 full_flag : 0 , record_count : 1 buf2 full_flag : 0 , record_count : 0 producer routine buf1 full_flag : 1 , record_count : 2 buf2 full_flag : 0 , record_count : 0 Consumer routine buf1 full_flag : 1 , record_count : 3

以下是我想做的:

producer routine
buf1 full_flag : 0 , record_count : 1
buf2 full_flag : 0 , record_count : 0
producer routine
buf1 full_flag : 1 , record_count : 2
buf2 full_flag : 0 , record_count : 0
Consumer routine 
buf1 full_flag : 1 , record_count : 3
buf2 full_flag : 0 , record_count : 0
...
Consumer routine 
buf1 full_flag : 1 , record_count : 10
buf2 full_flag : 0 , record_count : 0
producer routine
buf1 full_flag : 1 , record_count : 1
buf2 full_flag : 1 , record_count : 1
Consumer routine 
buf1 full_flag : 1 , record_count : 1
buf2 full_flag : 1 , record_count : 2
但它实际上是这样工作的:

producer routine
buf1 full_flag : 0 , record_count : 1
buf2 full_flag : 0 , record_count : 0
producer routine
buf1 full_flag : 1 , record_count : 2
buf2 full_flag : 0 , record_count : 0
...
Consumer routine 
buf1 full_flag : 1 , record_count : 10
buf2 full_flag : 0 , record_count : 0
producer routine
buf1 full_flag : 1 , record_count : 1
buf2 full_flag : 0 , record_count : 0
Consumer routine 
buf1 full_flag : 1 , record_count : 2
buf2 full_flag : 0 , record_count : 0

不管我怎么看代码,我都不知道该怎么做。第一个缓冲区工作正常,但在操作后指针似乎没有跨越到第二个缓冲区

可能结构或指针有问题。但我不知道该怎么处理

如果你能帮助我,我将非常感激

代码如下:

#include <stdio.h>
struct buf{
    int buf_n;
    int full_flag;
    int record_counter;
    struct buf* next;
};
int main(){
    struct buf buf1, buf2;
    struct buf *to_fill,*to_empty;
    char str[9];
    int state;
    int i=0;
    int j=0;
    char* Pstate;
    int count=0;
    int n=sizeof(str);
    buf1.buf_n = 1;
    buf2.buf_n = 2;
    buf1.full_flag = 0;
    buf2.full_flag = 0;
    buf1.record_counter = 0;
    buf2.record_counter = 0;
    buf1.next = &buf2;
    buf2.next = &buf1;
    to_fill = &buf1;
    to_empty = &buf1;
    FILE* file=fopen("input.txt","rt");
    FILE* file2=fopen("output.txt","wt");
while(1){
wait: 
  if((*to_fill).full_flag == 1) goto Loop;
   Pstate = fgets(str,sizeof(str),file);
   if(Pstate==NULL)
    break;
   fputs(str, stdout);
  if((*to_fill).record_counter > n){
   (*to_fill).full_flag = 0;
   (*to_fill) = *(*to_fill).next;
  }
  (*to_fill).record_counter=1;
  (*to_fill).full_flag=1;
  (*to_fill) = *(*to_fill).next;
  fprintf(file2,"producer routine\n");
  fprintf(file2,"buf1 full_flag : %d , record_count : %d\n", buf1.full_flag, buf1.record_counter);
  fprintf(file2,"buf2 full_flag : %d , record_count : %d\n", buf2.full_flag, buf2.record_counter);
  i++;
  goto Loop;
Loop: 
  if((*to_empty).full_flag == 0) goto wait;
  (*to_empty).record_counter = (*to_empty).record_counter+1;
  fprintf(file2,"Consumer routine \n");
  fprintf(file2,"buf1 full_flag : %d , record_count : %d\n", buf1.full_flag, buf1.record_counter);
  fprintf(file2,"buf2 full_flag : %d , record_count : %d\n", buf2.full_flag, buf2.record_counter);
  if((*to_empty).record_counter > n){
   (*to_empty).full_flag = 0;
   (*to_empty) = *(*to_empty).next;
  }
  count++;
  if(count>1000){
   break;
  goto wait;
  }
 }
 state=fclose(file);
 state=fclose(file2);
 return 0;
}

对任何人来说,判断出什么是错的都不是一件容易的事——那些GoTo都是1965年的事——但是你把
*分配给填充
*分配给空
,而人们期望把
分配给填充
分配给空

(该分配使您的两个缓冲区完全相同。特别是,
to_fill->next
to_empty->next
是相同的。)

中断
之后还有一个
转到
,没有任何效果

稍微更新您的代码,并修复该分配,使流程更加清晰(为清晰起见,删除了输出):

现在,似乎还剩下不少bug-缓冲区切换看起来很不确定(例如,您可能会将
切换到\u fill
两次,使其回到开始的位置)。

修复作为练习留下的bug。

检查您的标签<代码>双缓冲
用于图像处理。这看起来更像C而不是C++。还有,
goto
-真的吗?我很难理解你的代码。你可以尝试在调试器下一步一步地运行你的代码,“不管我怎么看代码,我都不知道该怎么做”。你需要(学习如何)调试你的程序。很抱歉我问了一个糟糕的问题。。。谢谢你的回复。
1234567890
struct buf{
    int buf_n;
    bool is_full;
    int record_counter;
    buf* next;
};

int main(){
    buf buf1 = {1, false, 0};
    buf buf2 = {2, false, 0};
    buf1.next = &buf2;
    buf2.next = &buf1;

    int i = 0;
    char* Pstate = nullptr;
    int count = 0;
    char str[9];
    int n = sizeof(str);

    buf* to_fill = &buf1;
    buf* to_empty = &buf1;
    FILE* file = fopen("input.txt","rt");
    FILE* file2 = fopen("output.txt","wt");

    while(1){
        if(!to_fill->is_full){
            Pstate = fgets(str,sizeof(str),file);
            if(Pstate == NULL)
                break;
            fputs(str, stdout);
            if(to_fill->record_counter > n){
                to_fill->is_full = false;
                to_fill = to_fill->next;
            }
            to_fill->record_counter = 1;
            to_fill->is_full = true;
            to_fill = to_fill->next;
            i++;
        }
        if(to_empty->is_full) {
            to_empty->record_counter += 1;
            if(to_empty->record_counter > n){
                to_empty->is_full = 0;
                to_empty = to_empty->next;
            }
            count++;
            if(count > 1000){
                break;
            }
        }
    }
    fclose(file);
    fclose(file2);
    return 0;
}