[問題] 拷貝文件檔案成功,印在螢幕上卻失敗
遇到的問題: (題意請描述清楚)
有個題目希望我把文件一的內容拷貝到文件二上,
並且在螢幕上顯示出文件裡的內容。
純粹就拷貝的部分來說是成功了,但是印在螢幕上會卻有錯。
開發平台: (例: VC++ or gcc/g++ or Dev-C++, Windows or Linux)
Dev-C++,Windows Vista
有問題的code: (請善用置底文標色功能)
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <io.h>
#include <sys/stat.h>
#define SIZE 20
int main(int argc, char *argv[])
{
char buffer[SIZE];
int f1,f2,bytes;
f1=open("C:\\Users\\acer\\Desktop\\C練習
\\12.3.10\\welcome.txt",O_RDONLY|O_TEXT);
f2=creat("C:\\Users\\acer\\Desktop\\C練習
\\12.3.10\\output2.txt",S_IWRITE);
if((f1!=-1)&&(f2!=-1))
{
while(!eof(f1))
{
bytes=read(f1,buffer,SIZE);
write(f2,buffer,bytes);
if(bytes<SIZE)
buffer[SIZE]='\0';
printf("%s",buffer);
}
close(f1);
close(f2);
putchar('\n');
printf("檔案拷貝完成!!\n");
}
else
printf("檔案開啟失敗!!\n");
system("PAUSE");
return 0;
}
補充說明:
文件的內容是:
「Welcome to the
world of C language.」
可以順利拷貝,但是在螢幕上卻會顯示錯誤,
而且當SIZE的值不同,會顯示不同的東西。
像是當SIZE=2,會顯示出:
「Wex "lcx "omx "e x "tox " tx "hex "
world of C language.」
請問是哪裡有問題呢?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.165.172.237
推
02/06 15:20, , 1F
02/06 15:20, 1F
→
02/06 15:21, , 2F
02/06 15:21, 2F
→
02/06 15:26, , 3F
02/06 15:26, 3F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章