qq_39914289
扫地浮尘
2017-12-10 10:27

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

  • c
  • linux

#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条回答