- #include <stdio.h>
- #include <stdlib.h>
- char* ReadFile(char *filename)
- {
- char *buffer = NULL;
- int string_size,read_size;
- FILE *handler = fopen(filename,"r");
- if (handler)
- {
- //seek the last byte of the file
- fseek(handler,0,SEEK_END);
- //offset from the first to the last byte, or in other words, filesize
- string_size = ftell (handler);
- //go back to the start of the file
- rewind(handler);
- //allocate a string that can hold it all
- buffer = (char*) malloc (sizeof(char) * (string_size + 1) );
- //read it all in one operation
- read_size = fread(buffer,sizeof(char),string_size,handler);
- //fread doesnt set it so put a \0 in the last position
- //and buffer is now officialy a string
- buffer[string_size+1] = '\0';
- if (string_size != read_size) {
- //something went wrong, throw away the memory and set
- //the buffer to NULL
- free(buffer);
- buffer = NULL;
- }
- }
- return buffer;
- }
- int main() {
- char *string = ReadFile("yourfile.txt");
- if (string) {
- puts(string);
- free(string);
- }
- return 0;
- }
- #include <stdio.h>
- long filesize(FILE *stream);
- int main(void)
- {
- FILE *fptr;
- stream = fopen( "MYFILE.TXT ", "w+ ");
- fprintf(fptr, "This is a test ");
- printf( "Filesize of MYFILE.TXT is %ld bytes\n ", filesize(fptr));
- fclose(fptr);
- return 0;
- }
- long filesize(FILE *fptr)
- {
- long curpos, length;
- curpos = ftell(fptr);
- fseek(fptr, 0L, SEEK_END);
- length = ftell(fptr);
- fseek(fptr, curpos, SEEK_SET);
- return length;
- }
- #include "stdio.h"
- int main()
- {
- FILE *pf=NULL; //文件指针
- int filelen=0;
- int i=0;
- char *buf;
- pf=fopen("D:\\test.txt","r"); //以只读方式打开文件
- if(pf==NULL)
- {
- return 0;
- }
- else
- {
- //获得文件长度
- fseek(pf,0,SEEK_END); //文件指针移到末尾
- filelen=ftell(pf); //获得文件当前指针位置,即为文件长度
- rewind(pf); //将文件指针移到开头,准备读取
- buf=malloc(filelen+1); //新建缓冲区,存储独处的数据
- //将缓冲区的数据设置为0
- for(i=0;i<filelen+1;i++)
- buf[i]=0;
- //读取文件
- fread(buf,filelen,1,pf);
- //关闭文件
- fclose(pf);
- //buf中即为要读出的数据
- printf("%s\n",buf); //输出一下数据,你可以随便怎么用
- free(buf); //最后记得要释放
- }
- return 1;
- }
#include <windows.h>
//文件操作--读文件
int _tmain(int argc, _TCHAR* argv[])
{
FILE* pFile;
char *pbuffer = NULL; //缓存读取内容
//int string_size = 1024, read_size;
int iFileLen = 0;
int i = 0;
pFile = fopen("G:\\文件操作\\FileOpreate\\Debug\\Read.txt", "rb"); //以只读和二进制方式打开
if (NULL == pFile){
printf("打开文件失败,请退出!\n");
//Sleep(1000);
exit(0);
}
else
{
/*
*(1).读写文件中字符的函数(一次只读写文件中的一个字符):
*int fgetc(FILE *stream);
*int getchar(void);
*int fputc(int ch,FILE *stream);
*int putchar(int ch);
*int getc(FILE *stream);
*int putc(int ch,FILE *stream);
*/
//每次读取一个字符
/*int ch = 0;
while( (ch = fgetc(pFile)) != EOF)
fputc(ch, stdout);
fclose(pFile);*/
/*
(2).读写文件中字符串的函数
char *fgets(char *string,int n,FILE *stream);
char *gets(char *s);
int fprintf(FILE *stream,char *format,variable-list);
int fputs(char *string,FILE *stream);
int fscanf(FILE *stream,char *format,variable-list);
*/
每次读取一行
//char str[128];
//while (!feof(pFile)) {//如果没有到文件尾
// if (fgets(str, 128, pFile) != NULL) //如果没有出差
// printf("%s", str);
//}
//fclose(pFile);
/
//(3)读取整个文件--文件随机读取
//获取文件长度
fseek(pFile, 0, SEEK_END); //文件指针移到文件末尾
iFileLen = ftell(pFile); //获得文件当前指针位置,即为文件长度
rewind(pFile); //将文件指针移到开头,准备读取文件
pbuffer = (char*)malloc(iFileLen + 1); //新建缓冲区,存储的数据
memset(pbuffer, 0, sizeof(char) * iFileLen + 1);//将缓冲区初始化为0
//读取文件
int n = fread(pbuffer, sizeof(char), sizeof(char) * iFileLen, pFile);
//关闭文件
fclose(pFile);
printf("%s\n", pbuffer); //输出数据
free(pbuffer); //释放缓冲区
}
return 0;
}
文件操作--写文件
//int _tmain(int argc, _TCHAR* argv[])
//{
// //char *s = "That's good news";
// //int i = 617;
// //FILE *fp;
// //fp = fopen("test.dat", "w"); /*建立一个文字文件只写*/
// //fputs("Your score of TOEFL is", fp); /*向所建文件写入一串字符*/
// //fputc(':', fp); /*向所建文件写冒号:*/
// //fprintf(fp, "%d/n", i); /*向所建文件写一整型数*/
// //fprintf(fp, "%s", s); /*向所建文件写一字符串*/
// //fclose(fp);
//
// char s[24], m[20];
// int i;
// FILE *fp;
// fp = fopen("test.dat", "r"); /*打开文字文件只读*/
// fgets(s, 24, fp); /*从文件中读取23个字符*/
// printf("%s", s);
// fscanf(fp, "%d", &i); /*读取整型数*/
// printf("%d", i);
// putchar(fgetc(fp)); /*读取一个字符同时输出*/
// fgets(m, 17, fp); /*读取16个字符*/
// puts(m); /*输出所读字符串*/
// fclose(fp);
// return 0;
//}