跳转到内容
View in the app

A better way to browse. Learn more.

彼岸论坛

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.
欢迎抵达彼岸 彼岸花开 此处谁在 -彼岸论坛

[OpenAI] 最近感觉 ChatGPT 经常出错,就连最拿手的代码也有问题,十分误导人

发表于

最近 GPT-4 给人的回复特别奇怪,有时候牛头不对马嘴,有时候出现错别字,甚至连比较擅长的代码也出错了,各位帮忙看看,它是不是出智障问题了。

我想让它用 C 语言实现 shell 中 mkdir- p 效果的代码,它的回复如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <errno.h>
#include <unistd.h>

// 创建目录,如果需要则递归创建父目录
int mkdir_p(const char *path) {
    char *copypath = strdup(path);
    char *sp;  // 用于 strtok_r 的保存指针
    char *p = NULL;
    int status = 0;

    errno = 0;

    // strtok_r 需要一个可修改的字符串
    for (p = strtok_r(copypath, "/", &sp); p != NULL; p = strtok_r(NULL, "/", &sp)) {
        // 在每一个中间步骤上,逐步构建路径
        if (p == copypath) { // 特殊处理以斜杠开头的路径
            continue;
        }

        // 逐层路径累加
        *(p + (p - copypath) + strlen(p)) = '\0';

        // 检查目录是否存在
        struct stat st;
        if (stat(copypath, &st) != 0) {
            // 目录不存在,创建之
            if (mkdir(copypath, 0777) != 0 && errno != EEXIST) {
                status = -1;
                break;
            }
        } else if (!S_ISDIR(st.st_mode)) {
            // 不是目录,抛出错误
            status = -1;
            break;
        }

        *(p + (p - copypath) + strlen(p)) = '/';  // 恢复 '/'
    }

    free(copypath);
    return status;
}

int main(int argc, char *argv[]) {
    if (argc < 2) {
        fprintf(stderr, "Usage: %s <path>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    if (mkdir_p(argv[1]) != 0) {
        perror("mkdir_p failed");
        exit(EXIT_FAILURE);
    }

    printf("Directories created successfully\n");
    return 0;
}

其中的 *(p + (p - copypath) + strlen(p)) = '\0'; 应该是有问题,甚至会造成数组越界吧。

V 友们看看是它变智障了,还是我加班加傻了。

Featured Replies

No posts to show

创建帐户或登录来提出意见

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.