码迷,mamicode.com
首页 > 其他好文 > 详细

9.4 文件IO基本操作

时间:2014-09-05 15:50:31      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   color   os   io   ar   for   

fopen后必须判断 FILE *p  是否返回NULL
fopen打开文件后,一定要fclose


feof  判断文件是否到达最后
对一个文件进行简单的加密解密操作
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define S_KEY 10
  5. void code(const char * src , const char * dest)
  6. {
  7. FILE * fp1 = fopen( src , "r" );
  8. FILE * fp2 = fopen( dest , "w" );
  9. if( !fp1 || !fp2)
  10. {
  11. printf("打开文件失败\n");
  12. exit( -1 );
  13. }
  14. char buf[1024];
  15. int len = 0 , i = 0;
  16. while(!feof(fp1))
  17. {
  18. fgets( buf , sizeof(buf) , fp1);
  19. len = strlen(buf);
  20. for( i = 0 ; i < len ; ++i)
  21. buf[i] += S_KEY;
  22. fputs( buf , fp2);
  23. }
  24. }
  25. void decode(const char * src , const char * dest)
  26. {
  27. FILE * fp1 = fopen( src , "r" );
  28. FILE * fp2 = fopen( dest , "w" );
  29. if( !fp1 || !fp2)
  30. {
  31. printf("打开文件失败\n");
  32. exit( -1 );
  33. }
  34. char buf[1024];
  35. int len = 0 , i = 0;
  36. while(!feof(fp1))
  37. {
  38. fgets( buf , sizeof(buf) , fp1);
  39. len = strlen(buf);
  40. for( i = 0 ; i < len ; ++i)
  41. buf[i] -= S_KEY;
  42. fputs( buf , fp2);
  43. }
  44. }
  45. int main(int argc , char * argv[])
  46. {
  47. if( argc < 4)
  48. {
  49. printf("参数一 是加密(1)或解密(2)\n");
  50. printf("参数二 是源文件\n参数三 是目标文件\n");
  51. exit(-1);
  52. }
  53. if( atoi(argv[1]) == 1)
  54. code(argv[2],argv[3]);
  55. else if(atoi(argv[1]) == 2)
  56. decode(argv[2],argv[3]);
  57. return 0;
  58. }
bubuko.com,布布扣
bubuko.com,布布扣



其他文件读写函数:

fprintf    是向一个文件写入东西 , 感觉类似 php 的 file_put_contents();?????

fscanf   可以从一个文件读入东西。不过遇到空格就会自动终止。
  1. #include <stdio.h>
  2. int main()
  3. {
  4. FILE *fp = fopen( "test.txt" , "r" ); //这个文件是有hello world 因为有空格 只能读出来hello
  5. char buf[100] ;
  6. fscanf( fp , "%s" , buf);
  7. printf("%s\n" , buf );
  8. fclose( fp);
  9. return 0;
  10. }
bubuko.com,布布扣



fopen的 b 模式
  1. FILE *p = fopen( "a.txt" , "wb");
b的意思是按照二进制的方式写文件
windows这个选项才有作用,linux可写可不写

例:
在windows下  没有b模式写入文件一个   \n 的时候  会自动写入 \r\n
如果有b的话,完全按照二进制方式写 只会写入 \n


linux和windows通过网络传输文件(借助myudp)
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include "myudp.h"
  6. #pragma comment (lib,"myudp.lib")
  7. int main(int argc , char *argv[])
  8. {
  9. if(argc < 3)
  10. {
  11. printf("[./app] [FILE_NAME] [SEND:1] [IP]\n");
  12. printf("[./app] [FILE_NAME] [RECV:2]\n");
  13. return -1;
  14. }
  15. if( atoi(argv[2]) == 1)
  16. {
  17. FILE *fp = fopen( argv[1] , "r");
  18. if( !fp )
  19. {
  20. printf("打开文件失败\n");
  21. return -1;
  22. }
  23. if(argv[3] == NULL)
  24. {
  25. printf("请输入你需要发送的IP\n");
  26. return -1;
  27. }
  28. char buf[1024] , ch;
  29. while( !feof( fp ) )
  30. {
  31. memset(buf , 0 , sizeof(buf));
  32. fgets( buf , sizeof(buf) , fp );
  33. send_socket( argv[3], 8080, buf, sizeof(buf));
  34. }
  35. ch = EOF;
  36. send_socket( argv[3], 8080, &ch, sizeof(ch));
  37. fclose(fp);
  38. }
  39. else if( atoi(argv[2]) == 2) //接收的
  40. {
  41. FILE *fp = fopen( argv[1] , "w");
  42. if( !fp )
  43. {
  44. printf("打开文件失败\n");
  45. return -1;
  46. }
  47. char buf[1024];
  48. char IP[1024];
  49. bind_socket( 8080 );
  50. while( 1 )
  51. {
  52. memset( buf , 0 , sizeof(buf));
  53. memset( IP , 0 , sizeof(IP));
  54. recv_socket(buf, sizeof(buf), IP);
  55. if( buf[0] == EOF)
  56. break;
  57. fputs( buf , fp);
  58. }
  59. fclose(fp);
  60. }
  61. return 0;
  62. }
bubuko.com,布布扣


就可以把windows里的a.txt 发送到linux里
bubuko.com,布布扣
bubuko.com,布布扣


通过fgets获取文件的行数:
  1. int file_count(FILE *fp)
  2. {
  3. char buf[1024] , len = 0;
  4. while( fgets( buf , sizeof(buf) , fp) != NULL )
  5. {
  6. len++;
  7. }
  8. return len;
  9. }
此方法得到的行数是正确的。


  1. int file_count(FILE *fp)
  2. {
  3. char buf[1024] , len = 0;
  4. while( !feof(fp))
  5. {
  6. fgets( buf , sizeof(buf) , fp);
  7. len++;
  8. }
  9. return len;
  10. }
上面这种方法得到的行数比实际大 1,不正确



读取一个文件的所有数字,进行排序,并且写入另外一个文件
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. int file_count(FILE *fp)
  5. {
  6. char buf[1024] , len = 0;
  7. while( fgets( buf , sizeof(buf) , fp) != NULL )
  8. {
  9. len++;
  10. }
  11. rewind(fp);
  12. return len;
  13. }
  14. void show_array(int *p , int len)
  15. {
  16. int i;
  17. for( i = 0 ; i < len ; i++)
  18. printf("%d\n",p[i]);
  19. }
  20. //选择排序
  21. void sort_array(int *p , int len)
  22. {
  23. int i, j , min , tmp;
  24. for(i = 0 ; i < len ; ++i)
  25. {
  26. min = i;
  27. for(j = i+1 ; j < len ; ++j)
  28. if(p[min] > p[j])
  29. min = j;
  30. if(min != i)
  31. {
  32. tmp = p[i];
  33. p[i] = p[min];
  34. p[min] = tmp;
  35. }
  36. }
  37. }
  38. int main()
  39. {
  40. FILE *fp = fopen("t.txt" , "r");
  41. if(!fp)
  42. {
  43. printf("打开文件失败\n");
  44. return -1;
  45. }
  46. int len = file_count(fp) , i = 0;
  47. int *p = (int *)malloc(sizeof(int) * len);
  48. memset(p , 0 , sizeof(int) * len);
  49. char buf[1024] = {0};
  50. for( i = 0 ; i < len ; i++)
  51. {
  52. fgets( buf , sizeof(buf) , fp);
  53. p[i] = atoi(buf);
  54. }
  55. //show_array( p , len);
  56. sort_array( p , len);
  57. //show_array( p , len);
  58. FILE *fp2 = fopen("sort.txt" , "w");
  59. for( i = 0 ; i < len ; i++)
  60. fprintf(fp2 , "%d\n" , p[i]);
  61. free(p);
  62. fclose(fp);
  63. fclose(fp2);
  64. return 0;
  65. }
bubuko.com,布布扣

bubuko.com,布布扣




9.4 文件IO基本操作

标签:des   style   blog   http   color   os   io   ar   for   

原文地址:http://www.cnblogs.com/l6241425/p/3957990.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!