0

我有一些问题。如果我手动输入文件路径,函数 fopen_s 总是失败,但如果我通过代码设置相同的路径,文件打开没有问题。我的第二个问题是“delete[] bytes”和“delete[] locatie”无法编译,我明白了

错误 C2065:“删除”未声明的标识符

C2059: 语法错误 ']'

我不知道为什么。ASM 部分是一个优化的循环顺便说一句。

char* getline()
{
    char line[50];  
    char *eof;
    line[0] = '\0'; 
    line[sizeof(line)-1] = ~'\0';  
    eof = fgets(line, sizeof(line), stdin);
}

int _tmain(int argc, _TCHAR* argv[])
{
    char* locatie, *bytes, c;
    FILE* bestand;
    int i, size;
    struct stat st;

    printf_s("Voer het pad van het bestand in en druk op enter.\n");
    locatie = getline();
    locatie[strlen(locatie)-1] ='\0';
    locatie = "C:\\Users\\xxx\\Documents\\haha.txt"; // <-- this line

    if ((i = fopen_s(&bestand, locatie, "r" )) != 0)
    {
        printf_s("Het bestand bestaat niet, of kon niet worden geopend!");
        getchar();
        return -1;
    }

    stat(locatie, &st);
    size = st.st_size;
    bytes = (char*)malloc(i+1);
    i = 0;

    loop:
    c = fgetc(bestand);
    __asm
    {
        movsx eax, byte ptr [c]
        cmp eax, 0x0FFFFFFFF
        je Break
        mov eax, dword ptr [bytes]
        add eax, dword ptr [i]
        mov cl, byte ptr [c]
        xor cl, 32
        mov byte ptr [eax], cl  
        mov eax, dword ptr [i]  
        add eax, 1
        mov dword ptr [i], eax
        jmp loop
    }

    Break:
    fclose(bestand);
    bytes[i] = '\0';
    printf(bytes);

    locatie = "C:\\Users\\xxx\\Documents\\haha.cpt";
    fopen_s(&bestand, locatie, "w");
    fprintf_s(bestand, "%c", bytes);
    fclose(bestand);
    delete[] bytes;
    delete[] locatie;
    return 0;
}
4

2 回答 2

0

delete[]是 C++,free()在 C中使用。

该程序具有未定义的行为,因为该函数getline()没有return任何作用。不要试图通过返回line(或者eofNULL或者指向line)来解决这个问题,但是你需要malloc()一个新的缓冲区并返回它或者传递一个缓冲区来getline()填充。

这是个错误:

fprintf_s(bestand, "%c", bytes);

格式说明符"%c"需要一个类型的参数,charbytes它是一个char*. 改用"%s"

fprintf_s(bestand, "%s", bytes);
于 2012-10-26T14:45:25.267 回答
0

但是getline()什么都不退!如果是这样,看起来好像您正在返回一个指向本地缓冲区的指针 -line一旦函数返回,它就会超出范围。

于 2012-10-26T14:47:39.850 回答