C语言读取文本文件



C代码   收藏代码
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3.   
  4. char* ReadFile(char *filename)  
  5. {  
  6.    char *buffer = NULL;  
  7.    int string_size,read_size;  
  8.    FILE *handler = fopen(filename,"r");  
  9.   
  10.    if (handler)  
  11.    {  
  12.        //seek the last byte of the file  
  13.        fseek(handler,0,SEEK_END);  
  14.        //offset from the first to the last byte, or in other words, filesize  
  15.        string_size = ftell (handler);  
  16.        //go back to the start of the file  
  17.        rewind(handler);  
  18.   
  19.        //allocate a string that can hold it all  
  20.        buffer = (char*) malloc (sizeof(char) * (string_size + 1) );  
  21.        //read it all in one operation  
  22.        read_size = fread(buffer,sizeof(char),string_size,handler);  
  23.        //fread doesnt set it so put a \0 in the last position  
  24.        //and buffer is now officialy a string  
  25.        buffer[string_size+1] = '\0';  
  26.   
  27.        if (string_size != read_size) {  
  28.            //something went wrong, throw away the memory and set  
  29.            //the buffer to NULL  
  30.            free(buffer);  
  31.            buffer = NULL;  
  32.         }  
  33.     }  
  34.   
  35.     return buffer;  
  36. }  
  37.   
  38. int main() {  
  39.     char *string = ReadFile("yourfile.txt");  
  40.     if (string) {  
  41.         puts(string);  
  42.         free(string);  
  43.     }  
  44.   
  45.     return 0;  
  46. }  
C代码   收藏代码
  1. #include   <stdio.h>    
  2.   
  3. long   filesize(FILE   *stream);    
  4.   
  5. int   main(void)    
  6. {    
  7.       FILE   *fptr;    
  8.   
  9.       stream   =   fopen( "MYFILE.TXT ",   "w+ ");    
  10.       fprintf(fptr,   "This   is   a   test ");    
  11.       printf( "Filesize   of   MYFILE.TXT   is   %ld   bytes\n ",   filesize(fptr));    
  12.       fclose(fptr);    
  13.       return   0;    
  14. }    
  15.   
  16. long   filesize(FILE   *fptr)    
  17. {    
  18.       long   curpos,   length;    
  19.   
  20.       curpos   =   ftell(fptr);    
  21.       fseek(fptr,   0L,   SEEK_END);    
  22.       length   =   ftell(fptr);    
  23.       fseek(fptr,   curpos,   SEEK_SET);    
  24.       return   length;    
  25. }     

 

C代码   收藏代码
  1. #include "stdio.h"  
  2.   
  3. int main()  
  4. {  
  5.  FILE *pf=NULL;   //文件指针  
  6.   
  7.  int filelen=0;  
  8.  int i=0;  
  9.  char *buf;  
  10.  pf=fopen("D:\\test.txt","r");   //以只读方式打开文件  
  11.  if(pf==NULL)  
  12.  {  
  13.   return 0;  
  14.  }  
  15.  else  
  16.  {  
  17.   //获得文件长度  
  18.   
  19.   fseek(pf,0,SEEK_END);   //文件指针移到末尾  
  20.   filelen=ftell(pf);   //获得文件当前指针位置,即为文件长度  
  21.   rewind(pf);   //将文件指针移到开头,准备读取  
  22.   
  23.   buf=malloc(filelen+1);    //新建缓冲区,存储独处的数据  
  24.   //将缓冲区的数据设置为0  
  25.   for(i=0;i<filelen+1;i++)  
  26.    buf[i]=0;  
  27.   
  28.   //读取文件  
  29.   fread(buf,filelen,1,pf);  
  30.   //关闭文件  
  31.   fclose(pf);  
  32.   //buf中即为要读出的数据  
  33.   
  34.   printf("%s\n",buf);    //输出一下数据,你可以随便怎么用  
  35.   free(buf);    //最后记得要释放  
  36.  }  
  37.  return 1;  
  38. }  

 
#include "stdafx.h"
#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;
//}
  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值