在 C 中扩展 RLE 字符串时出现奇怪的输出

问题描述 投票:0回答:1

这几天我一直在为这件事苦恼。我必须为大学制作一个程序,使用运行长度编码来压缩或解压缩 ASCII 艺术文件,具体取决于字符串中的第一个字符“C”或“E”。压缩部分的工作就像一个魅力。但扩展在 2/60 行上略有偏差。我就是不明白为什么。 Belo 是我的 Expand() 函数和 main() 的代码,我没有包含我的 compress() 因为它工作正常

我尝试检查内存泄漏/缓冲区溢出但无济于事,据我所知我正确释放内存。并确保新行中的字符串不会像前一行一样被读取。

ASCII 艺术输入: (E 表示扩展)

E
   20###05
   12###07   06#**#!!###03
   11#**#!!!04##   03#***04#!!!04#
   10#***04###03!!!03#  #***05#!!!04#
   10#***07#!!!03# #***06#!!!04#
   10#***09#!###03!*!*!*#!!!05#   08--
   10#!*!*!*!*!*!#!###0A!!!04#   06/_
   10###0B!##!!!0A#!!!03#   05//__
   0D###03!!!0B#!!!0D#!!!03###04///03  \
   03\   07##!#!!!0B#!!!0F#!!!07#
   03_\   04##!!#!!!0B#!!!0B###06!!!07*#
   04\\  ##!!#!!!0C#!!!04###07   05#!!!06***03#
  ___03\\#!!!03###13***05   07#...03!!***05#
 /   03\#!!!03.#   07***05 #   05***03   08#...04***07#
   05#*...04#   08***03   03#   0E#...07***05#
   04#**...05##   0A***05   0A##...08!!***04#
   04#!...08##   07***07###09...06#...03!!!05*#
   03#!...0B###07.***05...0F#.#..!!!04**#
  #*...05##...0D#..#...0F#...03#.!!***04#
  #*...04#.#...0C#...04#...0C##...06!***05#
  #*...07##...07###03...06###03...08#...07!!!03***04#
  #*...09###07...06!!...04###08...07!!!05***03#
   03#!!!03...11!!!08...0D!!!03***07#
   04#!!!04...0C!!!1B***06#
   05#***07!!!1C***03!!!04***05#
   06#***06!!!19***08!!***04#
   07##***05!!!15#***0D###03
   09##***04!!!15###03***06###04
   0B###04!!!18###06!#
   0F#!!!1E*#
   0F#!!!1D***03##
   0E#!!!1D***06#
   0D#!!!1D***07#
   0C#!!!20***05#
   0B#!!!23**#
   0A#!!!27##
   09#!!!29*##
   08#!!!2B***03#
   08#!!!21###04!!!06***04##
   07#!!!1F###03***04##!!!04***06##
   07#!!!1D##**!!***05#!!!03***08#
   07#!!!08#!!!0B#!!!07#***03!!!04***03!!!04***0A#
   06#!!!0A#!!!09#!!!07#***04!!!05*!!!0A***05#
   06#!!!0B#!!!07#!!!07#*!!***03!!!11***03#
   06#!!!0C#!!!06#!!!06#*!!!05*!!!0F***05#
   06#!!!0C#!!!06#!!!05#***03!!!11***08#
   05#!!!0D#!!!06#!!!05#***04!!!0E***0A#
   05#!!!0D#!!!06#!!!05#***05!!!0E***09#
   05#!!!0D#!!!06#!!!05#***03!!!11***08##
   04##!!!0D#!!!06#!!!05#*!!!15***05#!*##
   03#!#!!!08###06!!!06#!!!05#**!!!0A###09!!!04*#!!**##
  #!#!#!!!06#!!!0C#!!!05#***03!!!05###04***06!!!03###07!!**#
 #!#!!##!!!04#!!!0B###0C*!!!03#***08!!!0F**#
 #!#!!#!#!!#!!!03#!!!07#!!!0F#***0B!!!0F#
 #!#!!!03#!#!#!!#!!!07#!!!04#!!!04#!!!05#***0A!!!0F**#
 #!!#!!!03#!##!!#!!!06#!!!04#!!!04#!!!06#***0C!!!0A***04#
  ###09 ###0A!!!04#!!!04#!!!06#***0A!!!0C***03#
   16###11***0C!!!0A**#
   26#***0A!!!05###09
   27###0F

预期输出 ASCII 艺术:

                                #####
                  #######      #**#!!###
                 #**#!!!!##   #****#!!!!#
                #****###!!!#  #*****#!!!!#
                #*******#!!!# #******#!!!!#
                #*********#!###!*!*!*#!!!!!#        --
                #!*!*!*!*!*!#!##########!!!!#      /_
                ###########!##!!!!!!!!!!#!!!#     //__
             ###!!!!!!!!!!!#!!!!!!!!!!!!!#!!!####///  \
   \       ##!#!!!!!!!!!!!#!!!!!!!!!!!!!!!#!!!!!!!#
   _\    ##!!#!!!!!!!!!!!#!!!!!!!!!!!######!!!!!!!*#
    \\  ##!!#!!!!!!!!!!!!#!!!!#######     #!!!!!!***#
  ___\\#!!!###################*****       #...!!*****#
 /   \#!!!.#       ***** #     ***        #....*******#
     #*....#        ***   #              #.......*****#
    #**.....##          *****          ##........!!****#
    #!........##       *******#########......#...!!!!!*#
   #!...........#######.*****...............#.#..!!!!**#
  #*.....##.............#..#...............#...#.!!****#
  #*....#.#............#....#............##......!*****#
  #*.......##.......###......###........#.......!!!****#
  #*.........#######......!!....########.......!!!!!***#
   #!!!.................!!!!!!!!.............!!!*******#
    #!!!!............!!!!!!!!!!!!!!!!!!!!!!!!!!!******#
     #*******!!!!!!!!!!!!!!!!!!!!!!!!!!!!***!!!!*****#
      #******!!!!!!!!!!!!!!!!!!!!!!!!!********!!****#
       ##*****!!!!!!!!!!!!!!!!!!!!!#*************###
         ##****!!!!!!!!!!!!!!!!!!!!!###******####
           ####!!!!!!!!!!!!!!!!!!!!!!!!######!#
               #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*#
               #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!***##
              #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!******#
             #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*******#
            #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*****#
           #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!**#
          #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
         #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*##
        #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!***#
        #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!####!!!!!!****##
       #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!###****##!!!!******##
       #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##**!!*****#!!!********#
       #!!!!!!!!#!!!!!!!!!!!#!!!!!!!#***!!!!***!!!!**********#
      #!!!!!!!!!!#!!!!!!!!!#!!!!!!!#****!!!!!*!!!!!!!!!!*****#
      #!!!!!!!!!!!#!!!!!!!#!!!!!!!#*!!***!!!!!!!!!!!!!!!!!***#
      #!!!!!!!!!!!!#!!!!!!#!!!!!!#*!!!!!*!!!!!!!!!!!!!!!*****#
      #!!!!!!!!!!!!#!!!!!!#!!!!!#***!!!!!!!!!!!!!!!!!********#
     #!!!!!!!!!!!!!#!!!!!!#!!!!!#****!!!!!!!!!!!!!!**********#
     #!!!!!!!!!!!!!#!!!!!!#!!!!!#*****!!!!!!!!!!!!!!*********#
     #!!!!!!!!!!!!!#!!!!!!#!!!!!#***!!!!!!!!!!!!!!!!!********##
    ##!!!!!!!!!!!!!#!!!!!!#!!!!!#*!!!!!!!!!!!!!!!!!!!!!*****#!*##
   #!#!!!!!!!!######!!!!!!#!!!!!#**!!!!!!!!!!#########!!!!*#!!**##
  #!#!#!!!!!!#!!!!!!!!!!!!#!!!!!#***!!!!!####******!!!#######!!**#
 #!#!!##!!!!#!!!!!!!!!!!############*!!!#********!!!!!!!!!!!!!!!**#
 #!#!!#!#!!#!!!#!!!!!!!#!!!!!!!!!!!!!!!#***********!!!!!!!!!!!!!!!#
 #!#!!!#!#!#!!#!!!!!!!#!!!!#!!!!#!!!!!#**********!!!!!!!!!!!!!!!**#
 #!!#!!!#!##!!#!!!!!!#!!!!#!!!!#!!!!!!#************!!!!!!!!!!****#
  ######### ##########!!!!#!!!!#!!!!!!#**********!!!!!!!!!!!!***#
                      #################************!!!!!!!!!!**#
                                      #**********!!!!!#########
                                       ###############

实际输出(差异被((eg))包围

       (((###############¶ ¶¶¶¶¶¶                         #####)))
                  #######      #**#!!###
                 #**#!!!!##   #****#!!!!#
                #****###!!!#  #*****#!!!!#
                #*******#!!!# #******#!!!!#
                #*********#!###!*!*!*#!!!!!#        --
                #!*!*!*!*!*!#!##########!!!!#      /_
                ###########!##!!!!!!!!!!#!!!#     //__
             ###!!!!!!!!!!!#!!!!!!!!!!!!!#!!!####///  \
   \       ##!#!!!!!!!!!!!#!!!!!!!!!!!!!!!#!!!!!!!#
   _\    ##!!#!!!!!!!!!!!#!!!!!!!!!!!######!!!!!!!*#
    \\  ##!!#!!!!!!!!!!!!#!!!!#######     #!!!!!!***#
  ___\\#!!!###################*****       #...!!*****#
 /   \#!!!.#       ***** #     ***        #....*******#
     #*....#        ***   #              #.......*****#
    #**.....##          *****          ##........!!****#
    #!........##       *******#########......#...!!!!!*#
   #!...........#######.*****...............#.#..!!!!**#
  #*.....##.............#..#...............#...#.!!****#
  #*....#.#............#....#............##......!*****#
  #*.......##.......###......###........#.......!!!****#
  #*.........#######......!!....########.......!!!!!***#
   #!!!.................!!!!!!!!.............!!!*******#
    #!!!!............!!!!!!!!!!!!!!!!!!!!!!!!!!!******#
     #*******!!!!!!!!!!!!!!!!!!!!!!!!!!!!***!!!!*****#
      #******!!!!!!!!!!!!!!!!!!!!!!!!!********!!****#
       ##*****!!!!!!!!!!!!!!!!!!!!!#*************###
         ##****!!!!!!!!!!!!!!!!!!!!!###******####
           ####!!!!!!!!!!!!!!!!!!!!!!!!######!#
               #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*#
               #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!***##
              #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!******#
             #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*******#
            #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*****#
           #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!**#
          #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##
         #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*##
        #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!***#
        #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!####!!!!!!****##
       #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!###****##!!!!******##
       #!!!!!!!!!!!!!!!!!!!!!!!!!!!!!##**!!*****#!!!********#
       #!!!!!!!!#!!!!!!!!!!!#!!!!!!!#***!!!!***!!!!**********#
      #!!!!!!!!!!#!!!!!!!!!#!!!!!!!#****!!!!!*!!!!!!!!!!*****#
      #!!!!!!!!!!!#!!!!!!!#!!!!!!!#*!!***!!!!!!!!!!!!!!!!!***#
      #!!!!!!!!!!!!#!!!!!!#!!!!!!#*!!!!!*!!!!!!!!!!!!!!!*****#
      #!!!!!!!!!!!!#!!!!!!#!!!!!#***!!!!!!!!!!!!!!!!!********#
     #!!!!!!!!!!!!!#!!!!!!#!!!!!#****!!!!!!!!!!!!!!**********#
     #!!!!!!!!!!!!!#!!!!!!#!!!!!#*****!!!!!!!!!!!!!!*********#
     #!!!!!!!!!!!!!#!!!!!!#!!!!!#***!!!!!!!!!!!!!!!!!********##
    ##!!!!!!!!!!!!!#!!!!!!#!!!!!#*!!!!!!!!!!!!!!!!!!!!!*****#!*##
   #!#!!!!!!!!######!!!!!!#!!!!!#**!!!!!!!!!!#########!!!!*#!!**##
((!!!!***#)) #!#!#!!!!!!#!!!!!!!!!!!!#!!!!!#***!!!!!####******!!!#######!!**#
 #!#!!##!!!!#!!!!!!!!!!!############*!!!#********!!!!!!!!!!!!!!!**#
 #!#!!#!#!!#!!!#!!!!!!!#!!!!!!!!!!!!!!!#***********!!!!!!!!!!!!!!!#
 #!#!!!#!#!#!!#!!!!!!!#!!!!#!!!!#!!!!!#**********!!!!!!!!!!!!!!!**#
 #!!#!!!#!##!!#!!!!!!#!!!!#!!!!#!!!!!!#************!!!!!!!!!!****#
  ######### ##########!!!!#!!!!#!!!!!!#**********!!!!!!!!!!!!***#
                      #################************!!!!!!!!!!**#
                                      #**********!!!!!#########
                                       ###############((¶))

这是我的 Expand() 和 main() 方法:

// Function to expand a compressed string
char* expand(const char *input) {
    // Allocate memory for the output string
    char *output = (char *)malloc(2 * strlen(input) + 1);
    char *result = output; // Pointer to store the output
    char currentChar;
    int count;
    char hexCount[3];

    // Iterate through the input string
    for (size_t i = 0; i < strlen(input); i++) {
        currentChar = input[i];
        // Check if a character of the input has a hex digit one and 2 places in front of it -- meaning it's been compressed
        if (i + 4 < strlen(input) && input[i + 1] == currentChar && input[i + 2] == currentChar && isxdigit(input[i + 3]) && isxdigit(input[i + 4])) {
            // Copy the hex number from compressed string into the hexCount buffer
            strncpy(hexCount, &input[i + 3], 2);
            hexCount[2] = '\0';

            // Convert the hex count to decimal
            count = strtol(hexCount, NULL, 16);

            // Append the character 'count' times to the output
            for (int j = 0; j < count; j++) {
                strncat(output, &currentChar, 1);
            }
            i += 4;
        } else {
            // Append the character to the output
            strncat(output, &currentChar, 1);
        }
    }
    return result;
}

int main() {
    // Read each line from stdin, apply the compress/expand function
    char line[MAX_LINE_LEN];
    fgets(line, sizeof(line), stdin);  // Consume the newline after reading excomp
    char excomp = line[0];
    int lineNum = 0;
    if (excomp == 'C') {
        while (fgets(line, sizeof(line), stdin) != NULL) {
            // Remove the newline character
            line[strcspn(line, "\n")] = '\0';

            char *modifiedLine = compress(line);
            char *finalLine = modifiedLine;

            // Print the modified line to the command line
            printf("%s\n", finalLine);

            // Free the memory allocated by FUNCTION
            free(modifiedLine);
        }

        
    } else if (excomp == 'E') {
        while (fgets(line, sizeof(line), stdin) != NULL) {
            // Remove the newline character
            line[strcspn(line, "\n")] = '\0';

            char *modifiedLine = expand(line);
            char *finalLine = modifiedLine;

            // Print the modified line to the command line
            printf("%s\n", finalLine);

            // Free the memory allocated by FUNCTION
            free(modifiedLine);
        }

    }


    return 0;
}

非常感谢您的帮助,干杯!!

c memory
1个回答
0
投票

这些语句会导致未定义的行为

strncat(output, &currentChar, 1);

strncat
函数要求源和目标都是正确的空终止字符串。

output
不是一个以 null 结尾的字符串,因为
malloc
不会初始化它分配的内存。其内容将是不确定。使用
calloc
“清除”分配的内存(将所有字节设置为零)。

© www.soinside.com 2019 - 2024. All rights reserved.