9.4 文件IO基本操作

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(fgets( buf , sizeof(buf) , fp1) != NULL)
  17. {
  18. len = strlen(buf);
  19. for( i = 0 ; i < len ; ++i)
  20. buf[i] += S_KEY;
  21. fputs( buf , fp2);
  22. }
  23. }
  24. void decode(const char * src , const char * dest)
  25. {
  26. FILE * fp1 = fopen( src , "r" );
  27. FILE * fp2 = fopen( dest , "w" );
  28. if( !fp1 || !fp2)
  29. {
  30. printf("打开文件失败\n");
  31. exit( -1 );
  32. }
  33. char buf[1024];
  34. int len = 0 , i = 0;
  35. while(fgets( buf , sizeof(buf) , fp1) != NULL)
  36. {
  37. len = strlen(buf);
  38. for( i = 0 ; i < len ; ++i)
  39. buf[i] -= S_KEY;
  40. fputs( buf , fp2);
  41. }
  42. }
  43. int main(int argc , char * argv[])
  44. {
  45. if( argc < 4)
  46. {
  47. printf("参数一 是加密(1)或解密(2)\n");
  48. printf("参数二 是源文件\n参数三 是目标文件\n");
  49. exit(-1);
  50. }
  51. if( atoi(argv[1]) == 1)
  52. code(argv[2],argv[3]);
  53. else if(atoi(argv[1]) == 2)
  54. decode(argv[2],argv[3]);
  55. return 0;
  56. }
102248052156928.png


其他文件读写函数:

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. }
102248055598654.png



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

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

fopen的 r+ 模式
文件指针在开头,从文件开头覆盖原先的内容。
 
   
  1. #include <stdio.h>
  2. int main()
  3. {
  4. FILE *fp = fopen("a.txt","r+");
  5. if(!fp)
  6. {
  7. printf("打开文件失败\n");
  8. return -1;
  9. }
  10. char buf[1024] = "刘威";
  11. fputs(buf , fp);
  12. return 0;
  13. }
102248062319283.png


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. }
102248073878596.png

就可以把windows里的a.txt 发送到linux里
102248076063297.png
102248078401226.png



通过fgets和feof获取文件的行数:
 
   
  1. #include <stdio.h>
  2. int main()
  3. {
  4. FILE *fp = fopen("a.txt","r");
  5. char buf[1024];
  6. int count = 0;
  7. while(fgets(buf,sizeof(buf),fp))
  8. {
  9. count++;
  10. }
  11. printf("%d\n",count);
  12. return 0;
  13. }
此方法得到的行数是正确的。
 
   
  1. #include <stdio.h>
  2. int main()
  3. {
  4. char buf[1024];
  5. int count = 0;
  6. FILE *fp = fopen("a.txt","r");
  7. while(!feof(fp))
  8. {
  9. fgets(buf,sizeof(buf),fp);
  10. count++;
  11. }
  12. printf("%d\n",count);
  13. return 0;
  14. }
上面这种方法linux得到的行数比实际大 1,windows正常



读取一个文件的所有数字,进行排序,并且写入另外一个文件
 
   
  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. }
102248083561668.png
102248085745367.png




转载于:https://www.cnblogs.com/l6241425/p/3965306.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值