2013-07-29 83 views
-11

我想提出一个vsual C++程序,它是如下外的局部变量:如何访问循环

 char* StartPosition; 
     size_t skip=0; //inside the do loop i am incrementing this "skip" but when i come 
// out of the loop it again becomes Zero and the same thing repeats again and again. I now it 
// expects me to do some thing like skip=skip+ some_Increment(increment by 512+distance) and 
// the problem is this"distance" i am getting dynamically.which i am not able to do above. 
     char contents [100]; 
     do 
     { 

       int SizeOfFile = CreateOctalToInteger(&buffer[skip+124],11); //this is a function call. here i am getting the size of some file . 
       size_t distance= ((SizeOfFile%512) ? SizeOfFile + 512 - (SizeOfFile%512) : SizeOfFile); //here i done some manupulation on the size i received. what manupilation ?? 
//doesn't matter for now. 
       skip= distance +512; 
       memcpy(contents,&buffer[skip],100); 
       if (StartPosition=strstr(contents,".c")) 
       { 
        MessageBox(m_hwndPreview,L"finally string is copied",L"BTN WND6",MB_ICONINFORMATION); 
        break; 
       } 
       return true; 

     } 
     while(strstr(contents,".html") != NULL); 

请告诉我,如果这个问题不明确。我想要做的是对于每次执行,skip的值应该增加(根据size_t skip = distance + 512;),但是在这里,发生的是每次控制从循环中出来时,它会再次初始化用零跳过,然后重复相同的事情。请给我解决这个问题的方法。

+5

你好再次,是否真的需要这[一个新的问题[this]之后(http://stackoverflow.com/questions/17920081/how-to-skip-a-file-inside-the -tar-file-to-get-a-particular-file/17924563#17924563) –

+0

这是你重复调用的例程吗?你想大小保持其价值? – Jiminion

+0

有两个变量名为跳过不帮助的东西..... – Jiminion

回答

0

哎终于我所做的解决方案 -

char* StartPosition; 
    //char *check; 
    size_t skip= 0; 
    char contents [100]; 
    do 
    { 

      int SizeOfFile = CreateOctalToInteger(&buffer[skip+124],11); 
      size_t distance= ((SizeOfFile%512) ? SizeOfFile + 512 - (SizeOfFile%512) : SizeOfFile); 
      skip += distance + 512; 
      memcpy(contents,&buffer[skip],100); 
      if (StartPosition=strstr(contents,".html")) 
      { 
       MessageBox(m_hwndPreview,L"finally string is copied",L"BTN WND6",MB_ICONINFORMATION); 
       break; 
      } 


    } 
    while(strcmp(contents,".html") != NULL); 

- 稠STRCMP代替的strstr。现在它的工作

6
size_t skip= distance +512; 

声明了一个新变量skip,它掩盖了你在循环之外声明的实例。如果您想更新原始实例,只是改变这一行指现有变量

skip= distance +512; 

如果我误解,你想要的skip值调用之间持续存在的功能,您可能需要使外部的skipstatic(如Jim所建议的)或将函数更改为接受初始值skip作为参数并返回更新的值。

编辑:这是你的原代码与两个skip变量的行为评论

char* StartPosition; 
int skip=0; // declare variable named skip 
char contents [100]; 
do { 
    int SizeOfFile = CreateOctalToInteger(&buffer[skip+124],11); 
    size_t distance= ((SizeOfFile%512) ? SizeOfFile + 512 - (SizeOfFile%512) : SizeOfFile); 
    size_t skip= distance +512; // declare second variable called skip 
           // this masks the above variable 
           // operations in this loop will affect this skip instance 
           // code outside the loop refers to the earlier skip instance 
           // the value of the earlier skip instance never changes from 0 
    memcpy(contents,&buffer[skip],100); 
    if (StartPosition=strstr(contents,".c")) { 
     MessageBox(m_hwndPreview,L"finally string is copied",L"BTN WND6",MB_ICONINFORMATION); 
     break; 
    } 
    return true; 
} 
while(strstr(contents,".html") != NULL); 
+0

我不能说,你可以请示例解释?? – Sss

+0

如果我把size_t skip =距离+512;低于int skip = 0; (我的意思是在这个循环之上,然后它会给距离未定义的错误) – Sss

+3

你有两个同名的变量。一个在循环内部,一个在循环外部,这就是问题所在。 – Borgleader

2
size_t skip= distance +512; 

您创建一个新的跳跃,而这个新的跳跃的生命结束时,循环结束,而跳过了循环逗留0。如果你想给的价值,int skip只使用skip = distance +512;

您也可以只使用size_t skip = 0;代替int skip = 0;

+1

@ShekharSinghSHEKHAWAT在循环的上面声明值skip之后,skip在循环中可用,所以您不需要再次声明它。 –

+3

@ShekharSinghSHEKHAWAT你还没有明白答案 – doctorlove

+0

@ShekharSinghSHEKHAWAT而且,如果你再次声明,你创建一个新的跳过,我们可以称它为(skip1)。 ,现在在循环上面声​​明的跳过的名字被覆盖。您只需给skip1赋予正确的值。在循环之后,跳过return并跳过skip1。所以跳过是零 –

3

没有无关的细节,你这样做是

int skip = 0; 
do 
{ 
    size_t skip = distance + 512; 
    //.... 
} 
while(something != NULL); 

您已经声明 不同东西叫做跳跃在那里。如果你只是想叫跳过一个变量,不decalre的第二个,使用第一种:

int skip = 0; 
do 
{ 
    skip = distance + 512; 
    //... 
} 
while(something != NULL); 
1

必须定义跳过外循环,并增加它的循环内

char contents [100]; 
    size_t skip = 0; // or whatever is approriate 
    do 
    { 

      int SizeOfFile = CreateOctalToInteger(&buffer[skip+124],11); 
      size_t distance= ((SizeOfFile%512) ? SizeOfFile + 512 - (SizeOfFile%512) : SizeOfFile); 
      skip += distance+512; 
      memcpy(contents,&buffer[skip],100); 
      if (StartPosition=strstr(contents,".c")) 
      { 
       MessageBox(m_hwndPreview,L"finally string is copied",L"BTN WND6",MB_ICONINFORMATION); 
       break; 
      } 
      return true; 

    } 
    while(strstr(contents,".html") != NULL); 
+0

你是否做出了解决方案,但我不知道为什么这个循环不去第二次执行。但它正确评估第一次执行(我已调试的值是正确的),但不要重复,直到while(strstr(contents,“。html”)!= NULL);是否实现..你猜猜它为什么这样做?? – Sss

+1

@ShekharSinghSHEKHAWAT循环结束时无条件的'return true'保证你最多可以完成一次迭代。您应该删除该行。 – simonc

0

您目前在代码中有两次跳过定义,一次是int,另一次是size_t。在循环内部,size_t skip正在递增,但是在循环外部调用int size_t。你需要在循环外部创建一个跳过变量,在循环内部增加它,然后你可以在循环之后调用它。

char* StartPosition; 
    size_t skip=0; //inside the do loop i am incrementing this "skip" but when i come 
// out of the loop it again becomes Zero and the same thing repeats again and again. I now it 
// expects me to do some thing like skip=skip+ some_Increment(increment by 512+distance) and 
// the problem is this"distance" i am getting dynamically.which i am not able to do above. 
    char contents [100]; 
    do 
    { 

      int SizeOfFile = CreateOctalToInteger(&buffer[skip+124],11); //this is a function call. here i am getting the size of some file . 
      size_t distance= ((SizeOfFile%512) ? SizeOfFile + 512 - (SizeOfFile%512) : SizeOfFile); //here i done some manupulation on the size i received. what manupilation ?? 
//doesn't matter for now. 
      skip= distance +512; 
      memcpy(contents,&buffer[skip],100); 
      if (StartPosition=strstr(contents,".c")) 
      { 
       MessageBox(m_hwndPreview,L"finally string is copied",L"BTN WND6",MB_ICONINFORMATION); 
       break; 
      } 
      return true; 

    } 
    while(strstr(contents,".html") != NULL);