创建新的字符串以某种方式改变旧字符串的值?|C

prdp8dxp  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(122)

所以我在nand 2 tetris做项目6-你必须做一个HACK汇编程序。
现在,我想做的就是创建一个大字符串,将数据存储在汇编文件中,没有Windows 0x 0 d和重复的新行(所以没有0x 0a后跟0x 0a)。
这是我的头文件-assembler. h

typedef struct string
{
    int length;
    char *the_string;   
} string;

字符串
这是我的代码

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <iso646.h>
#include <string.h>
#include "assembler.h"

string *put_the_entire_file_into_one_big_string(FILE *the_entire_file);
string *take_out_the_double_lines(string *the_string);
int main(int argc, char *argv[])
{
    // Check the number of arguements
    if (argc != 2)
    {
        printf("Provide only one arguement: the name of the assembly file\n");
        return -1;
    }

    // Check if the arguement is an .asm file
    int a = sizeof(argv[1]);
    if (!((argv[1][a - 5] == '.') and (argv[1][a - 4] == 'a') and (argv[1][a - 3] == 's') and (argv[1][a - 2] == 'm')))
    {
        printf("not an .asm file\n");
        return -1;
    }

    // Check if the file exists
    FILE *the_file = fopen(argv[1], "r");
    if (the_file == NULL)
    {
        printf("File doesn't exist. Maybe you mistyped it?");
        return -1;
    }
    string *the_big_string_thats_the_entire_file = put_the_entire_file_into_one_big_string(the_file);

    string *the_big_string_but_without_mulitple_line_breaks_in_a_row = take_out_the_double_lines(the_big_string_thats_the_entire_file);

    free(the_big_string_thats_the_entire_file->the_string);

    printf("%s", the_big_string_but_without_mulitple_line_breaks_in_a_row->the_string);

    free(the_big_string_but_without_mulitple_line_breaks_in_a_row->the_string);
    
    fclose(the_file);
}

string *put_the_entire_file_into_one_big_string(FILE *the_entire_file)
{
    string *one_big_string;
    one_big_string->the_string = malloc(sizeof(char));
    one_big_string->length = 0;
    char c;
    while (1)
    {
        c = fgetc(the_entire_file);
        if (c == EOF)
        {
            break;
        }
        if (c == 0x0d)
        {
            continue;
        }
        one_big_string->the_string[one_big_string->length] = c;
        one_big_string->length++;
        one_big_string->the_string = realloc(one_big_string->the_string, sizeof(char) * (one_big_string->length + 1));
    }
    return one_big_string;
}

string *take_out_the_double_lines(string *the_old_string)
{
    string *new_string;
    new_string->the_string = malloc(sizeof(char));
    new_string->length = 1;
    new_string->the_string[0] = the_old_string->the_string[0];
    new_string->length = 2;
    new_string->the_string = realloc(new_string->the_string, sizeof(char) * 2);

    for (int i = 1; i < the_old_string->length; i++)
    {
        printf("%i\t%i\n", i, the_old_string->length);
        if (not(the_old_string->the_string[i] == 0x0a and the_old_string->the_string[i - 1] == 0x0a))
        {
            new_string->the_string[i] = the_old_string->the_string[i];
            new_string->length++;
            new_string->the_string = realloc(new_string->the_string, sizeof(char) * new_string->length);
        }
    }
    return new_string;
}


根据gdb的数据,

new_string->length = 1;


以某种方式改变了_old_string?
我正在测试的Add.asm文件在这里:
https://drive.google.com/file/d/1xZzcMIUETv3u3sdpM_oTJSTetpVee3KZ/view
在projects/06/Add.asm下

zf9nrax1

zf9nrax11#

int a = sizeof(argv[1]);

字符串
指针上的sizeof返回的是指针的大小,而不是指向的数据。这里需要strlen()来计算argv[1]的长度。请注意,在访问之前,需要先确定argv[1][a-5]是否有效。
或者,您可以使用strcchr()strcmp()来检查argv[1]是否以".asm"结尾:

const char *const point = strcchr(argv[1], '.');

if (point && !strcmp(point, ".asm")) {
    /* The file ends with .asm. */
} else {
   /* Handle error here. */
}
string *one_big_string;
one_big_string->the_string = malloc(sizeof(char));  // Aside: You can leave out sizeof(char). It is defined by the standard to be 1
one_big_string->length = 0;

的数据
one_big_string的内容在这里是不确定的。它没有指向任何有意义的东西。你需要首先为one_big_string分配空间,然后为其成员分配内存。new_string也有类似的问题。
然后,您需要按照分配内存的相反顺序free()该内存。

相关问题