c语言中fread问题,在linux与vs中的问题

#include
#include
#include
#pragma warning(disable:4996)
#define SIZE 1024*8

void menu()
{
printf("*************************\n");
printf("1.拷贝文件:\n");
printf("2.清屏:\n");
printf("3.返回:\n");
printf("*************************\n");

}

void mycopy()
{
char rpath[256];
char wpath[256];
FILE *rfp = NULL;
FILE *wfp = NULL;
int n = 0;

printf("请输入:\n");
scanf("%s", rpath);

printf("请输出:\n");
scanf("%s", wpath);

if (strcmp(rpath, wpath) == 0)
{
    printf("no same\n");
    return;
}

rfp = fopen(rpath, "rb+");
if (rfp == NULL)
{
    perror("fopen rfp");
    fclose(rfp);
}
wfp = fopen(wpath, "wb+");
if (wfp == NULL)
{
    perror("fopen wfp");
    fclose(wfp);
}

char buf[1000] = { 0 };
do{
    //n = 0;
    n = fread(buf, sizeof(char), SIZE, rfp);
    printf("%d\n", n);
    fwrite(buf, sizeof(char), n, wfp);

} while (n > 0);

fclose(wfp);
fclose(rfp);

}

int main(void)
{
int cmd = 0;
while (1)
{
menu();
printf("cmd:");
scanf("%d", &cmd);

    switch (cmd)
    {
    case 1:
        mycopy();
        break;
    case 2:
        system("cls");
        break;
    default:
        exit(0);
        break;
    }
}

system("pause");
return 0;

}

//#define SIZE 1024*8,linux可以执行成功,vs显示拷贝失败
//换成1024*2,linux与vs都拷贝成功

1个回答

fread调用中的buf创建太小

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!