#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
int file_size(char* filename)//獲取文件名為filename的文件大小。
{
FILE *fp = fopen(filename, "rb");//打開文件。
int size;
if(fp == NULL) // 打開文件失敗
return -1;
fseek(fp, 0, SEEK_END);//定位文件指針到文件尾。
size=ftell(fp);//獲取文件指針偏移量,即文件大小。
fclose(fp);//關閉文件。
return size;
}
int main ()
{
int size=0;
size=file_size("qw");
printf("%d\n",size);
FILE * pFile,*qw;
char *buffer=(char*)malloc(sizeof(char)*size);
qw =fopen("qw","r");
pFile = fopen ( "qwe" , "wb" );
printf("%d==\n",pFile);
printf("%d\n",size);
fread(buffer,1,size,qw);
fwrite (buffer , sizeof(byte), size , pFile );
fclose (pFile);
rename("qwe","Groot.jpg");
return 0;
}
擴展資料:
c語言讀取TXT文件:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE 1024
int main()
{
char buf[MAX_LINE];? /*緩沖區*/ FILE *fp; /*文件指針*/ int len; ?/*行字符個數*/ if((fp = fopen("test.txt","r")) == NULL) { perror("fail to read"); exit (1) ; } while(fgets(buf,MAX_LINE,fp) != NULL) { len = strlen(buf); buf[len-1] = '\0';? /*去掉換行符*/ printf("%s %d \n",buf,len - 1); }return 0;
}