长不大的小Tom 2021-12-28 22:54 采纳率: 100%
浏览 29
已结题

strcpy用法问题


void my_fputc(char *path)
{
    if (path == NULL)
    {
        return;
    }

    FILE* fp;

    fp = fopen(path, "w+");

    if (fp == NULL)
    {
        perror("fopen");
        system("pause");
        return;
    }

    char buf[] = "this is test";
    char tmp1[20] = { 0 };
    char tmp2[20] = "lyyyw";
    int i = 0;
    int n = strlen(buf);

    for (i = 0; i < n; i++)
    {
        int ret = fputc(buf[i],fp);
        sprintf(tmp1,"%c",ret);
        strcpy(tmp2[i],tmp1[0]);
        //printf("%c", ret);
    }

    if (fp != NULL)
    {
        fclose(fp);
        fp = NULL;
    }
}

void my_fgetc(char* path)
{
    FILE* fp;

    fp = fopen(path, "r+");

    char ch;

    if (fp == NULL)
    {
        perror("fopen");
        system("pause");
        return;
    }

    //while ((ch = fgetc(fp)) != EOF)
    while(!feof(fp))
    {
        ch = fgetc(fp);
        printf("%c",ch);
    }

    if (fp != NULL)
    {
        fclose(fp);
        fp = NULL;
    }
}

int main(void)
{
    my_fputc("../test.txt");

    //my_fgetc("../test.txt");

    printf("\n");
    system("pause");
}

为什么这种情况下strcpy会出现错误,两个变量都是有内存的

  • 写回答

3条回答 默认 最新

  • _术士_ 2021-12-28 23:22
    关注

    strcpy(tmp2[i],tmp1[0]);

    strcpy拷贝的是字符串,不是字符

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
    1人已打赏
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 1月6日
  • 已采纳回答 12月29日
  • 创建了问题 12月28日

悬赏问题

  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题