Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.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语言中将字符串切碎为512字节的块_C_C Strings - Fatal编程技术网

在c语言中将字符串切碎为512字节的块

在c语言中将字符串切碎为512字节的块,c,c-strings,C,C Strings,我正在尝试编写一个函数,它接受一个字符串,并将其分割成512字节的块。它获取字符串中的前512字节,并将其存储在切碎的[0]中,然后将下一个512字节存储在切碎的[1]中……以此类推。当我在函数中打印切碎时,它似乎起作用了,但当我返回切碎时[0]会给出整个字符串,而不仅仅是前512个字符串。你知道这是怎么回事吗 char** chopString(){ char string[]="1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

我正在尝试编写一个函数,它接受一个字符串,并将其分割成512字节的块。它获取字符串中的前512字节,并将其存储在切碎的[0]中,然后将下一个512字节存储在切碎的[1]中……以此类推。当我在函数中打印切碎时,它似乎起作用了,但当我返回切碎时[0]会给出整个字符串,而不仅仅是前512个字符串。你知道这是怎么回事吗

char** chopString(){
    char string[]="1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200";

    int len=strlen(string)+1;
    int i,j,bytes,blocks;
    int blockSize=512;
    bytes=len*sizeof(char);
    blocks=(int)ceil((double)bytes/blockSize);  //determine number of blocks rounded up

    char* chunk=malloc(blockSize+1);
    char* newChunk=malloc(blockSize+1);
    char** chopped=malloc(sizeof(char*)*(blocks+1));  //blockSize+1 for  null character


    for(j=0; j<blocks; j++){
        len=strlen(string)+1;              //get length of string
        if(len<blockSize){                    //if the string can fit in one block
            bytes=len;                        //the number of bytes to write is the length
        }else{                                //if it doesn't fit in one block
            bytes=blockSize;                  //then bytes to write is one block
        }
        strcpy(chunk,string);        //copy newString into chunk
        newChunk=chunk;                 //keep pointer to begining of chunk

        for(i=0; i<bytes; i++){
            chunk++;
        }
        strcpy(string,chunk);        //set new string to remaining portion            
        *chunk='\0';                    //set end of chunk to null
        chopped[j]=newChunk;            //put chunk into array
        printf("Chopped[%d]: %s\n",j,chopped[j]);
        printf("length: %d",(int)strlen(chopped[j]));
    }
    chopped[j]=NULL;
    printf("\n"); 
    return chopped;
}
strcpy复制整个字符串。试试strncpy。注意:您需要在字符串的末尾添加一个空终止符

strcpy复制整个字符串。试试strncpy。注意:您需要在字符串的末尾添加一个空终止符


代码有很多问题。这里有一些

每次调用malloc都需要检查返回值!=NULL以确保操作成功

此行:“newChunk=chunk;”覆盖malloc返回的指针,导致内存泄漏

strcpy不关注目标1参数中的可用字节数。 因此,字节数需要限制为 第一个参数-1的长度 然后附加一个NUL字节 最简单的修复方法是使用strncpy

设置字符**的正确方法是:

4a分配数组

  char** myArray = malloc( count* sizeof(char*) )
  if (NULL == myArray)
     //handle error and exit
  else
     // following to make cleanup on error easy
     memset(myArray, 0x00, count*sizeof(char*) )
4b分配每个字符串

  for( int i=0; i<count; i++ )
      myArray[i] = malloc( blockSize+1 ) //+1 allows for termination byte
      if( NULL == myArray[i] )
          // cleanup and exit
4c清理将是

  for( int i=0; i<count; i++ )
      free(myArray[i])
  free(myArray)

代码有很多问题。这里有一些

每次调用malloc都需要检查返回值!=NULL以确保操作成功

此行:“newChunk=chunk;”覆盖malloc返回的指针,导致内存泄漏

strcpy不关注目标1参数中的可用字节数。 因此,字节数需要限制为 第一个参数-1的长度 然后附加一个NUL字节 最简单的修复方法是使用strncpy

设置字符**的正确方法是:

4a分配数组

  char** myArray = malloc( count* sizeof(char*) )
  if (NULL == myArray)
     //handle error and exit
  else
     // following to make cleanup on error easy
     memset(myArray, 0x00, count*sizeof(char*) )
4b分配每个字符串

  for( int i=0; i<count; i++ )
      myArray[i] = malloc( blockSize+1 ) //+1 allows for termination byte
      if( NULL == myArray[i] )
          // cleanup and exit
4c清理将是

  for( int i=0; i<count; i++ )
      free(myArray[i])
  free(myArray)