C 文件读写
r 文件只读操作打开一个文件
#include<stdio.h>#include<stdlib.h>#include<string.h>intmain(){FILE*p=fopen("./test.txt","r");if(p==NULL){printf("openfail!\n");}else{printf("opensucess!\n");}return0;}chunli@ubuntu:~/pointer$lltotal20K-rwxrwxr-x1chunlichunli8.4KMay2716:09a.out-rw-rw-r--1chunlichunli215May2716:09main.c-rw-rw-r--1chunlichunli209May2716:04test.txtchunli@ubuntu:~/pointer$gcc-std=c99main.c&&./a.outopensucess!
w 文件只写操作如果文件不存在就创建文件如果文件存在就清空文件
#include<stdio.h>#include<stdlib.h>#include<string.h>intmain(){FILE*p=fopen("write_file.txt","w");if(p==NULL){printf("openfail!\n");}else{printf("opensucess!\n");}return0;}chunli@ubuntu:~/pointer$gcc-std=c99main.c&&./a.outopensucess!chunli@ubuntu:~/pointer$lltotal20K-rwxrwxr-x1chunlichunli8.4KMay2716:09a.out-rw-rw-r--1chunlichunli215May2716:09main.c-rw-rw-r--1chunlichunli209May2716:04test.txt-rw-rw-r--1chunlichunli0May2716:09write_file.txt
从文件读几个字符
chunli@ubuntu:~/pointer$lltotal4.0K-rw-rw-r--1chunlichunli479May2716:18main.cchunli@ubuntu:~/pointer$catmain.c#include<stdio.h>#include<stdlib.h>#include<string.h>intmain(){FILE*p=fopen("main.c","r");//打开当前目录下的文件if(p==NULL){printf("openfail!\n");}else{printf("opensucess!\n");charc=getc(p);printf("%c",c);c=getc(p);printf("%c",c);c=getc(p);printf("%c",c);c=getc(p);printf("%c",c);c=getc(p);printf("%c",c);c=getc(p);printf("%c",c);c=getc(p);printf("%c\n",c);}return0;}chunli@ubuntu:~/pointer$gcc-std=c99main.c&&./a.outopensucess!
读取文件所有字符
#include<stdio.h>#include<stdlib.h>#include<string.h>intmain(){FILE*p=fopen("main.c","r");//打开当前目录下的文件if(p==NULL){printf("openfail!\n");}else{printf("opensucess!\n");charc=0;while((c=getc(p))!=EOF){printf("%c",c);}}return0;}编译运行chunli@ubuntu:~/pointer$gcc-std=c99main.c&&./a.outopensucess!#include<stdio.h>#include<stdlib.h>#include<string.h>intmain(){FILE*p=fopen("main.c","r");//打开当前目录下的文件if(p==NULL){printf("openfail!\n");}else{printf("opensucess!\n");charc=0;while((c=getc(p))!=EOF)//读取文件所有字符{printf("%c",c);}}return0;}
文本文件复制
chunli@ubuntu:~/pointer$lltotal4.0K-rw-rw-r--1chunlichunli428May2716:32main.cmain程序chunli@ubuntu:~/pointer$catmain.c#include<stdio.h>#include<stdlib.h>#include<string.h>intmain(){FILE*p1=fopen("main.c","r");//打开当前目录下的文件FILE*p2=fopen("text.c","w");//创建文件if((p1==NULL)||(p2==NULL)){printf("openfail!\n");}else{printf("opensucess!\n");charc=0;while((c=getc(p1))!=EOF){putc(c,p2);}}fclose(p1);//关闭文件fclose(p2);//关闭文件return0;}chunli@ubuntu:~/pointer$gcc-std=c99main.c&&./a.outopensucess!可以看到生成了一个新的文件chunli@ubuntu:~/pointer$lltotal20K-rwxrwxr-x1chunlichunli8.6KMay2716:33a.out-rw-rw-r--1chunlichunli428May2716:32main.c-rw-rw-r--1chunlichunli428May2716:33text.c查看这个文件chunli@ubuntu:~/pointer$cattext.c#include<stdio.h>#include<stdlib.h>#include<string.h>intmain(){FILE*p1=fopen("main.c","r");//打开当前目录下的文件FILE*p2=fopen("text.c","w");//创建文件if((p1==NULL)||(p2==NULL)){printf("openfail!\n");}else{printf("opensucess!\n");charc=0;while((c=getc(p1))!=EOF){putc(c,p2);}}fclose(p1);//关闭文件fclose(p2);//关闭文件return0;}chunli@ubuntu:~/pointer$
文件加密
chunli@ubuntu:~/pointer$lltotal4.0K-rw-rw-r--1chunlichunli495May2716:40main.c主程序chunli@ubuntu:~/pointer$catmain.c#include<stdio.h>#include<stdlib.h>#include<string.h>voidcode(constchar*src,constchar*dest){FILE*p1=fopen(src,"r");//打开当前目录下的文件FILE*p2=fopen(dest,"w");//创建文件if((p1==NULL)||(p2==NULL)){printf("openfail!\n");}else{printf("opensucess!\n");charc=0;while((c=getc(p1))!=EOF){c+=10;putc(c,p2);}}fclose(p1);//关闭文件fclose(p2);//关闭文件}intmain(){code("main.c","code.txt");}编译并运行chunli@ubuntu:~/pointer$gcc-std=c99main.c&&./a.outopensucess!看看产生的文件chunli@ubuntu:~/pointer$lltotal20K-rwxrwxr-x1chunlichunli8.6KMay2716:40a.out-rw-rw-r--1chunlichunli495May2716:40code.txt-rw-rw-r--1chunlichunli495May2716:40main.c已经完成加密的文件chunli@ubuntu:~/pointer$catcode.txt-sxmno*F}~nsy8rH-sxmno*F}~nvsl8rH-sxmno*F}~|sxq8rHysn*myno2myx}~*mrk|*4}|m6myx}~*mrk|*4no}~3*PSVO*4z;*G*pyzox2}|m6*,|,3E99訷濴锁PSVO*4z<*G*pyzox2no}~6,,3E99蓴耄sp22z;*GG*X_VV3*2z<*GG*X_VV3*3z|sx~p2,yzox*pksv+*fx,3Eov}oz|sx~p2,yzox*mo}}+fx,3Emrk|*m*G*:Ersvo22m*G*qo~m2z;33*+G*OYP3m5G;:E~m2m6z<3Epmvy}o2z;3E99ンpmvy}o2z<3E99ンsx~*wksx23myno2,wksx8m,6,myno8~~,3Echunli@ubuntu:~/pointer$
文件解密
chunli@ubuntu:~/pointer$lltotal4.0K-rw-rw-r--1chunlichunli923May2716:43main.cchunli@ubuntu:~/pointer$catmain.c#include<stdio.h>#include<stdlib.h>#include<string.h>voiddecode(constchar*src,constchar*dest){FILE*p1=fopen(src,"r");//打开当前目录下的文件FILE*p2=fopen(dest,"w");//创建文件if((p1==NULL)||(p2==NULL)){printf("openfail!\n");}else{printf("opensucess!\n");charc=0;while((c=getc(p1))!=EOF){c-=10;putc(c,p2);}}fclose(p1);//关闭文件fclose(p2);//关闭文件}voidcode(constchar*src,constchar*dest){FILE*p1=fopen(src,"r");//打开当前目录下的文件FILE*p2=fopen(dest,"w");//创建文件if((p1==NULL)||(p2==NULL)){printf("openfail!\n");}else{printf("opensucess!\n");charc=0;while((c=getc(p1))!=EOF){c+=10;putc(c,p2);}}fclose(p1);//关闭文件fclose(p2);//关闭文件}intmain(){code("main.c","code.txt");decode("code.txt","decode.txt");}chunli@ubuntu:~/pointer$gcc-std=c99main.c&&./a.outopensucess!opensucess!chunli@ubuntu:~/pointer$catdecode.txt#include<stdio.h>#include<stdlib.h>#include<string.h>voiddecode(constchar*src,constchar*dest){FILE*p1=fopen(src,"r");//打开当前目录下的文件FILE*p2=fopen(dest,"w");//创建文件if((p1==NULL)||(p2==NULL)){printf("openfail!\n");}else{printf("opensucess!\n");charc=0;while((c=getc(p1))!=EOF){c-=10;putc(c,p2);}}fclose(p1);//关闭文件fclose(p2);//关闭文件}voidcode(constchar*src,constchar*dest){FILE*p1=fopen(src,"r");//打开当前目录下的文件FILE*p2=fopen(dest,"w");//创建文件if((p1==NULL)||(p2==NULL)){printf("openfail!\n");}else{printf("opensucess!\n");charc=0;while((c=getc(p1))!=EOF){c+=10;putc(c,p2);}}fclose(p1);//关闭文件fclose(p2);//关闭文件}intmain(){code("main.c","code.txt");decode("code.txt","decode.txt");}chunli@ubuntu:~/pointer$
主函数传参数加密解密
主程序 main.c
#include<stdio.h>#include<stdlib.h>#include<string.h>voiddecode(constchar*src,constchar*dest){FILE*p1=fopen(src,"r");//打开当前目录下的文件FILE*p2=fopen(dest,"w");//创建文件if((p1==NULL)||(p2==NULL)){printf("openfail!\n");}else{printf("opensucess!\n");charc=0;while((c=getc(p1))!=EOF){c-=10;putc(c,p2);}}fclose(p1);//关闭文件fclose(p2);//关闭文件}voidcode(constchar*src,constchar*dest){FILE*p1=fopen(src,"r");//打开当前目录下的文件FILE*p2=fopen(dest,"w");//创建文件if((p1==NULL)||(p2==NULL)){printf("openfail!\n");}else{printf("opensucess!\n");charc=0;while((c=getc(p1))!=EOF){c+=10;putc(c,p2);}}fclose(p1);//关闭文件fclose(p2);//关闭文件}intmain(intargc,char*args[]){if(argc<4){printf("参数1:1加密1:2解密\n");printf("参数2:1输入文件2:2输出文件\n");}else{if(atoi(args[1])==1){code(args[2],args[3]);}if(atoi(args[1])==2){decode(args[2],args[3]);}}}编译chunli@ubuntu:~/pointer$gcc-std=c99main.c执行chunli@ubuntu:~/pointer$./a.out参数1:1加密1:2解密参数2:1输入文件2:2输出文件执行加密chunli@ubuntu:~/pointer$./a.out1main.c加密了.txtopensucess!执行解密chunli@ubuntu:~/pointer$./a.out2加密了.txt解密了.txtopensucess!查看加密的文件chunli@ubuntu:~/pointer$cat加密了.txt-sxmno*F}~nsy8rH-sxmno*F}~nvsl8rH-sxmno*F}~|sxq8rHysn*nomyno2myx}~*mrk|*4}|m6myx}~*mrk|*4no}~3*PSVO*4z;*G*pyzox2}|m6*,|,3E99訷濴锁PSVO*4z<*G*pyzox2no}~6,,3E99蓴耄sp22z;*GG*X_VV3*2z<*GG*X_VV3*3z|sx~p2,yzox*pksv+*fx,3Eov}oz|sx~p2,yzox*mo}}+fx,3Emrk|*m*G*:Ersvo22m*G*qo~m2z;33*+G*OYP3m7G;:E~m2m6z<3Epmvy}o2z;3E99ンpmvy}o2z<3E99ンysn*myno2myx}~*mrk|*4}|m6myx}~*mrk|*4no}~3*PSVO*4z;*G*pyzox2}|m6*,|,3E99訷濴锁PSVO*4z<*G*pyzox2no}~6,,3E99蓴耄sp22z;*GG*X_VV3*2z<*GG*X_VV3*3z|sx~p2,yzox*pksv+*fx,3Eov}oz|sx~p2,yzox*mo}}+fx,3Emrk|*m*G*:Ersvo22m*G*qo~m2z;33*+G*OYP3m5G;:E~m2m6z<3Epmvy}o2z;3E99ンpmvy}o2z<3E99ンsx~*wksx2sx~*k|qm6*mrk|*4k|q}eg3sp2k|qm*F*>3z|sx~p2,D;僵蓾<昀砀,3Ez|sx~p2,D;螯耄仄<螯曱砀,3Eov}osp2k~ys2k|q}e;g3*GG*;3myno2k|q}e<g6k|q}e=g3Esp2k~ys2k|q}e;g3*GG*<3nomyno2k|q}e<g6k|q}e=g3E查看界面的文件chunli@ubuntu:~/pointer$cat解密了.txt#include<stdio.h>#include<stdlib.h>#include<string.h>voiddecode(constchar*src,constchar*dest){FILE*p1=fopen(src,"r");//打开当前目录下的文件FILE*p2=fopen(dest,"w");//创建文件if((p1==NULL)||(p2==NULL)){printf("openfail!\n");}else{printf("opensucess!\n");charc=0;while((c=getc(p1))!=EOF){c-=10;putc(c,p2);}}fclose(p1);//关闭文件fclose(p2);//关闭文件}voidcode(constchar*src,constchar*dest){FILE*p1=fopen(src,"r");//打开当前目录下的文件FILE*p2=fopen(dest,"w");//创建文件if((p1==NULL)||(p2==NULL)){printf("openfail!\n");}else{printf("opensucess!\n");charc=0;while((c=getc(p1))!=EOF){c+=10;putc(c,p2);}}fclose(p1);//关闭文件fclose(p2);//关闭文件}intmain(intargc,char*args[]){if(argc<4){printf("参数1:1加密1:2解密\n");printf("参数2:1输入文件2:2输出文件\n");}else{if(atoi(args[1])==1){code(args[2],args[3]);}if(atoi(args[1])==2){decode(args[2],args[3]);}}}chunli@ubuntu:~/pointer$
一行一行的读取文件
#include<stdio.h>#include<stdlib.h>#include<string.h>intmain(intargc,char*args[]){FILE*p=fopen("main.c","r");charbuf[1024];while(!feof(p))//判断是不是到了文件结束{fgets(buf,sizeof(buf),p);//从文件读取一行printf("%s",buf);}fclose(p);return0;}chunli@ubuntu:~/pointer$gcc-std=c99main.c&&./a.out#include<stdio.h>#include<stdlib.h>#include<string.h>intmain(intargc,char*args[]){FILE*p=fopen("main.c","r");charbuf[1024];while(!feof(p))//判断是不是到了文件结束{fgets(buf,sizeof(buf),p);//从文件读取一行printf("%s",buf);}fclose(p);return0;}}chunli@ubuntu:~/pointer$
文件复制 一行一行的复制
#include<stdio.h>#include<stdlib.h>#include<string.h>intmain(intargc,char*args[]){FILE*p1=fopen("main.c","r");FILE*p2=fopen("cpoy.txt","w");charbuf[1024];while(!feof(p1))//判断是不是到了文件结束{fgets(buf,sizeof(buf),p1);//从文件中读取fputs(buf,p2);//写入到另一个文件}fclose(p1);fclose(p2);return0;}chunli@ubuntu:~/pointer$gcc-std=c99main.c&&./a.outchunli@ubuntu:~/pointer$lltotal20K-rwxrwxr-x1chunlichunli8.7KMay2718:51a.out-rw-rw-r--1chunlichunli325May2718:51cpoy.txt-rw-rw-r--1chunlichunli323May2718:51main.cchunli@ubuntu:~/pointer$catcpoy.txt#include<stdio.h>#include<stdlib.h>#include<string.h>intmain(intargc,char*args[]){FILE*p1=fopen("main.c","r");FILE*p2=fopen("cpoy.txt","w");charbuf[1024];while(!feof(p1))//判断是不是到了文件结束{fgets(buf,sizeof(buf),p1);//从文件中读取fputs(buf,p2);//写入到另一个文件}fclose(p1);fclose(p2);return0;}
利用一行一行的读取加密文件
main.c#include<stdio.h>#include<stdlib.h>#include<string.h>voiddecode(char*s){intlen=strlen(s);while(len--)*s++-=10;}voidcode(char*s){intlen=strlen(s);while(len--)*s+++=10;}intmain(intargc,char*args[]){FILE*p1=fopen("main.c","r");FILE*p2=fopen("cpoy1.txt","w");FILE*p3=fopen("cpoy2.txt","w");charbuf[1024];while(!feof(p1))//判断是不是到了文件结束{fgets(buf,sizeof(buf),p1);//从文件中读取code(buf);//加密这个字符串fputs(buf,p2);//写入到另一个文件decode(buf);//解密字符串fputs(buf,p3);//写入到另一个文件}fclose(p1);//关闭文件fclose(p2);fclose(p3);return0;}chunli@ubuntu:~/pointer$gcc-std=c99main.c&&./a.outchunli@ubuntu:~/pointer$catcpoy1.txt-sxmno*F}~nsy8rH-sxmno*F}~nvsl8rH-sxmno*F}~|sxq8rHysn*nomyno2mrk|*4}3sx~*vox*G*}~|vox2}3Ersvo2vox7734}55*7G*;:Eysn*myno2mrk|*4}3sx~*vox*G*}~|vox2}3Ersvo2vox7734}55*5G*;:Esx~*wksx2sx~*k|qm6*mrk|*4k|q}eg3PSVO*4z;*G*pyzox2,wksx8m,6,|,3EPSVO*4z<*G*pyzox2,mzy;8~~,6,,3EPSVO*4z=*G*pyzox2,mzy<8~~,6,,3Emrk|*pe;:<>gErsvo2+poyp2z;3399ヴ需耄垰§pqo~}2p6}soyp2p36z;3E99耄myno2p3E99僵餮滽p~}2p6z<3E99烴諶耄nomyno2p3E99滽p~}2p6z=3E99烴諶耄pmvy}o2z;3E99ンpmvy}o2z<3Epmvy}o2z=3E|o|x*:Echunli@ubuntu:~/pointer$chunli@ubuntu:~/pointer$catcpoy2.txt#include<stdio.h>#include<stdlib.h>#include<string.h>voiddecode(char*s){intlen=strlen(s);while(len--)*s++-=10;}voidcode(char*s){intlen=strlen(s);while(len--)*s+++=10;}intmain(intargc,char*args[]){FILE*p1=fopen("main.c","r");FILE*p2=fopen("cpoy1.txt","w");FILE*p3=fopen("cpoy2.txt","w");charbuf[1024];while(!feof(p1))//判断是不是到了文件结束{fgets(buf,sizeof(buf),p1);//从文件中读取code(buf);//加密这个字符串fputs(buf,p2);//写入到另一个文件decode(buf);//解密字符串fputs(buf,p3);//写入到另一个文件}fclose(p1);//关闭文件fclose(p2);fclose(p3);return0;}
从文件格式化读取
#include<stdio.h>#include<stdlib.h>#include<string.h>intmain(intargc,char*args[]){charbuf[1024];sprintf(buf,"%s--%d","1234",5678);//格式化输出到->字符串数组printf("%s\n",buf);FILE*p=fopen("hello.txt","w");fprintf(p,"%s%d\n",buf,9999);//格式化输出到文件fclose(p);FILE*p1=fopen("hello.txt","r");fscanf(p1,"%s\n",buf);//从文件格式化输入遇到空格就会停止printf("%s\n",buf);FILE*p2=fopen("hello.txt","r");fgets(buf,99,p2);//从文件格式化输入遇到空格不会停止printf("%s",buf);fclose(p);return0;}chunli@ubuntu:~/pointer$gcc-std=c99main.c&&./a.out1234--56781234--56781234--56789999chunli@ubuntu:~/pointer$cathello.txt1234--56789999
文件输入初步1
#include<stdio.h>#include<stdlib.h>#include<string.h>intmain(intargc,char*args[]){charbuf[1024];FILE*p=fopen("hello.txt","w");for(inta=0;a<10;a++){sprintf(buf,"%s--%d","1234",a);//格式化输出到->字符串数组fprintf(p,"%s%d\n",buf,a*a);//格式化输出到文件}fclose(p);return0;}chunli@ubuntu:~/pointer$gcc-std=c99main.c&&./a.outchunli@ubuntu:~/pointer$cathello.txt1234--001234--111234--241234--391234--4161234--5251234--6361234--7491234--8641234--981
获取键盘输入到文件
#include<stdio.h>#include<stdlib.h>#include<string.h>intmain(intargc,char*args[]){charbuf[1024];FILE*p=fopen("hello.txt","w");for(inta=0;a<3;a++){gets(buf);fprintf(p,"%s\n",buf);//格式化输出到文件}fclose(p);return0;}chunli@ubuntu:~/pointer$gcc-std=c99main.c&&./a.outmain.c:Infunction‘main’:main.c:10:3:warning:‘gets’isdeprecated(declaredat/usr/include/stdio.h:638)[-Wdeprecated-declarations]gets(buf);^/tmp/ccESW6f6.o:Infunction`main':main.c:(.text+0x54):warning:the`gets'functionisdangerousandshouldnotbeused.1231qazasdfghjchunli@ubuntu:~/pointer$cathello.txt1231qazasdfghj
追加写入文件
#include<stdio.h>#include<stdlib.h>#include<string.h>intmain(intargc,char*args[]){charbuf[1024];FILE*p=fopen("hello.txt","a");//对已有文件追加内容如果文件不存在就创建for(inta=0;a<3;a++){gets(buf);fprintf(p,"%s\n",buf);//格式化输出到文件}fclose(p);return0;}chunli@ubuntu:~/pointer$gcc-std=c99main.c&&./a.outmain.c:Infunction‘main’:main.c:10:3:warning:‘gets’isdeprecated(declaredat/usr/include/stdio.h:638)[-Wdeprecated-declarations]gets(buf);^/tmp/cc1C6VG0.o:Infunction`main':main.c:(.text+0x54):warning:the`gets'functionisdangerousandshouldnotbeused.qwertfg123451qazchunli@ubuntu:~/pointer$cathello.txt1231qazasdfghjqwertfg123451qaz
linux 系统之间通过网络传输文本文件1一次发送一个字符
参考我的linux网络通信
http://990487026.blog.51cto.com/10133282/1773543
替换主函数就可以了
#include<stdio.h>#include<stdlib.h>#include<string.h>#include"myudp.h"intmain(intargc,char*args[]){if(argc<3){printf("请输入2个参数1发送/2接收需要发送文件名/接收另存为文件名\n");return0;}if(atoi(args[1])==1){chardestined[]="10.11.12.4";FILE*read=fopen(args[2],"r");charc=0;while((c=fgetc(read))!=EOF){send_socket(destined,8080,&c,sizeof(char));//printf("%c",c);}c=EOF;send_socket(destined,8080,&c,sizeof(char));}if(atoi(args[1])==2){FILE*write=fopen(args[2],"w");bind_socket(8080);charbuf[100]={0};charIP[100]={0};while(1){recv_socket(buf,1,IP);if(buf[0]==EOF)break;fprintf(write,"%s",buf);}}return0;}编译chunli@ubuntu:~/pointer$gcc-std=c99main.c-L.-lmyudp发送端执行chunli@ubuntu:~/pointer$./a.out1main.c接收端执行chunli@ubuntu:~/pointer$./a.out2recieve
linux 系统之间通过网络传输文本文件2一次发送一行字符
参考我的linux网络通信
http://990487026.blog.51cto.com/10133282/1773543
替换主函数就可以了
#include<stdio.h>#include<stdlib.h>#include<string.h>#include"myudp.h"intmain(intargc,char*args[]){if(argc<3){printf("请输入2个参数1发送/2接收需要发送文件名/接收另存为文件名\n");return0;}if(atoi(args[1])==1){chardestined[]="10.11.12.4";FILE*read=fopen(args[2],"r");charbuf[999]={0};while(!feof(read)){fgets(buf,sizeof(buf),read);//从p1读取一行send_socket(destined,8080,buf,strlen(buf));}buf[0]=EOF;send_socket(destined,8080,buf,sizeof(char));}if(atoi(args[1])==2){FILE*write=fopen(args[2],"w");bind_socket(8080);charbuf[999]={0};charIP[100]={0};while(1){memset(buf,0,sizeof(buf));recv_socket(buf,sizeof(buf),IP);fprintf(write,"%s",buf);if(buf[0]==EOF){break;}}}return0;}编译chunli@ubuntu:~/pointer$gcc-std=c99main.c-L.-lmyudp接收端运行chunli@ubuntu:~/pointer$./a.out2recieve.c发送端运行chunli@ubuntu:~/pointer$catsendtcpdump详细用法第一种是关于类型的关键字主要包括hostnetport,例如host210.27.48.2指明210.27.48.2是一台主机net202.0.0.0指明202.0.0.0是一个网络地址port23指明端口号是23。如果没有指定类型缺省的类型是host.第二种是确定传输方向的关键字主要包括src,dst,dstorsrc,dstandsrc,这些关键字指明了传输的方向。举例说明src210.27.48.2,指明ip包中源地址是210.27.48.2,dstnet202.0.0.0指明目的网络地址是202.0.0.0。如果没有指明方向关键字则缺省是srcordst关键字。chunli@ubuntu:~/pointer$./a.out1send接收端查看chunli@ubuntu:~/pointer$catrecievetcpdump详细用法第一种是关于类型的关键字主要包括hostnetport,例如host210.27.48.2指明210.27.48.2是一台主机net202.0.0.0指明202.0.0.0是一个网络地址port23指明端口号是23。如果没有指定类型缺省的类型是host.第二种是确定传输方向的关键字主要包括src,dst,dstorsrc,dstandsrc,这些关键字指明了传输的方向。举例说明src210.27.48.2,指明ip包中源地址是210.27.48.2,dstnet202.0.0.0指明目的网络地址是202.0.0.0。如果没有指明方向关键字则缺省是srcordst关键字。
更高效率。文件发送
#include<stdio.h>#include<stdlib.h>#include<string.h>#include"myudp.h"#include<sys/stat.h>intmain(intargc,char*args[]){if(argc<3){printf("请输入2个参数1发送/2接收需要发送文件名/接收另存为文件名\n");return0;}if(atoi(args[1])==1){chardestined[]="10.11.12.4";FILE*read=fopen(args[2],"r");structstatst;stat(args[2],&st);//调用stat函数会把文件的相关信息放入结构st当中char*buf=(char*)malloc(st.st_size);//在堆里面根据文件实际大小动态分配一个数组char*log=buf;memset(buf,0,st.st_size);//把堆当中申请的内存清空while(!feof(read)){chartmp[100]={0};fgets(tmp,sizeof(tmp),read);//从p1读取一行strcat(buf,tmp);}send_socket(destined,8080,buf,strlen(buf));charc=EOF;send_socket(destined,8080,&c,sizeof(char));//free(log);//fclose(read);}if(atoi(args[1])==2){FILE*write=fopen(args[2],"w");bind_socket(8080);charbuf[999]={0};charIP[100]={0};while(1){memset(buf,0,sizeof(buf));recv_socket(buf,sizeof(buf),IP);fprintf(write,"%s",buf);if(buf[0]==EOF){break;}}}return0;}编译chunli@ubuntu:~/pointer$gcc-std=c99main.c-L.-lmyudpchunli@ubuntu:~/pointer$catsendtcpdump详细用法第二种是确定传输方向的关键字主要包括src,dst,dstorsrc,dstandsrc发送端chunli@ubuntu:~/pointer$./a.out1send接收端chunli@ubuntu:~/pointer$./a.out2recieve
【产生一定数量的随机数保存到文件再从文件中读出并排序保存到另外一个文件中】
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<time.h>intcreate_rand(intnum){srand((unsignedint)time(NULL));FILE*p=fopen("a.txt","w");if(p==NULL)return0;for(inti=0;i<num;i++){fprintf(p,"%d\n",rand());}fclose(p);return0;}intread_arr(int*arr,intlen){FILE*p=fopen("a.txt","r");if(p==NULL)return0;inti=0;intvalue=0;while(!feof(p)&&len--){fscanf(p,"%d",&value);arr[i]=value;i++;}fclose(p);return0;}intwrite_arr(int*arr,intlen){FILE*p=fopen("b.txt","w");if(p==NULL)return0;for(inti=0;i<len;i++){fprintf(p,"%d\n",arr[i]);}fclose(p);return0;}voidprintf_arr(int*arr,intlen){for(inti=0;i<len;i++){printf("arr[%d]=%d\n",i,arr[i]);}}voidbobble(int*arr,intlen){for(inti=0;i<len;i++){for(intj=1;j<len-i;j++){if(arr[j]<arr[j-1]){inttmp=arr[j];arr[j]=arr[j-1];arr[j-1]=tmp;}}}}intmain(){intarr[9999]={0};create_rand(sizeof(arr)/sizeof(int));read_arr(arr,sizeof(arr)/sizeof(int));printf_arr(arr,sizeof(arr)/sizeof(int));bobble(arr,sizeof(arr)/sizeof(int));printf("-------------------------------------\n");printf_arr(arr,sizeof(arr)/sizeof(int));write_arr(arr,sizeof(arr)/sizeof(int));}编译并执行chunli@ubuntu:~/pointer$gcc-std=c99main.c&&./a.out
fseek函数
ftell函数
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<time.h>intmain(){charbuf[999]={0};intnum=0;FILE*p=fopen("text","r");if(p==NULL)return0;/*文件位置设定*/fseek(p,0,SEEK_SET);//SEEK_SET,设定文件开始位置//fseek(p,-6,SEEK_END);//SET_END,从文件末尾向前偏移N个字节为有效(负数向前移动正数向后移动)printf("当前位置%ld\n",ftell(p));while(!feof(p)){memset(buf,0,sizeof(buf));//fseek(p,3,SEEK_CUR);//在当前位置向后偏移N个字节fgets(buf,sizeof(buf),p);printf("当前位置%ld\t%s",ftell(p),buf);}}编译运行chunli@ubuntu:~/file$gcc-std=c99main.c&&./a.out当前位置0当前位置8abcdefg当前位置21HelloWorld!当前位置45Welcometochunlihome!当前位置68欢迎学习C语言当前位置75123456当前位置103此行下面有一个空行当前位置104当前位置104chunli@ubuntu:~/file$
fprintf()是临时写到内存中的
fflush函数
将数据实时写入磁盘
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<time.h>intmain(){charbuf[20]={0};intnum=0;FILE*p=fopen("text","w");if(p==NULL)return0;while(1){memset(buf,0,sizeof(buf));scanf("%s",buf);if(strncmp(buf,"exit",4)==0){break;}//当输入的字符是exit就退出fprintf(p,"%s\n",buf);//将数据写入临时内存fflush(p);//将本次的数据从内存同步到磁盘(效率极低)}fclose(p);//此时才将数据从内存写入到磁盘return0;}chunli@ubuntu:~/file$gcc-std=c99main.c&&./a.out12342141exit
fwrite写二进制文件
#include<stdio.h>#include<stdlib.h>#include<string.h>intmain(){FILE*p=fopen("text.dat","wb");if(p==NULL)return0;charbuf[100]={"1111\r\n1111\r\n2222\r\n3333"};//fwrite(buf,1,strlen(buf),p);//这两种方式是一样的fwrite(buf,strlen(buf),1,p);//一次写strlen的长度写1次fclose(p);return0;}文件二进制加密解密chunli@ubuntu:~/file$catmain.c#include<stdio.h>#include<stdlib.h>#include<string.h>#defineCODE0x1234intmain(){//文件加密哈{FILE*p1=fopen("main.c","rb");if(p1==NULL)return0;FILE*p2=fopen("1加密.dat","wb");if(p2==NULL)return0;while(!feof(p1)){charbuf[999]={0};fgets(buf,sizeof(buf),p1);//读一行字符串size_tlen=strlen(buf);for(inti=0;i<len;i++)//处理每个字符串{inttmp=buf[i]^CODE;//加密并转换为intfwrite(&tmp,sizeof(int),1,p2);//写入文件}}fclose(p1);fclose(p2);}//文件解密{FILE*p1=fopen("1加密.dat","rb");if(p1==NULL)return0;inttmp=0;FILE*p2=fopen("2解密.dat","wb");if(p2==NULL)return0;while(fread(&tmp,sizeof(int),1,p1))//此处不能用feof判断{charc=tmp^CODE;//将读取的int解密转换为charfwrite(&c,sizeof(char),1,p2);}fclose(p1);fclose(p2);}return0;}chunli@ubuntu:~/file$gcc-std=c99main.c&&./a.outchunli@ubuntu:~/file$lltotal24K-rw-rw-r--1chunlichunli3.9KMay3116:271加密.dat-rw-rw-r--1chunlichunli973May3116:272解密.dat-rwxrwxr-x1chunlichunli8.8KMay3116:27a.out-rw-rw-r--1chunlichunli973May3116:28main.c
二进制文件复制
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<sys/stat.h>intmain(intargc,char*args[]){if(argc<3){printf("参数不够\n");return0;}FILE*p1=fopen(args[1],"rb");if(p1==NULL){printf("打开文件%s失败\n",args[1]);return0;}FILE*p2=fopen(args[2],"wb");if(p2==NULL){printf("打开文件%s失败\n",args[2]);return0;}/*方式一*/charbuf[10]={0};//设定缓存区大小while(!feof(p1))//此处不能用feof判断{size_tsize=fread(buf,1,sizeof(buf),p1);//printf("%ld\n",size);//查看本次读取了多少个有效字节fwrite(buf,1,size,p2);//写入有效个字节}/*方式二最高效structstatst;stat(args[1],&st);char*buf=malloc(st.st_size);fread(buf,st.st_size,1,p1);fwrite(buf,st.st_size,1,p2);free(buf);*/fclose(p1);fclose(p2);return0;}chunli@ubuntu:~/file$gcc-std=c99-omycpmain.c&&./mycp/sbin/ifconfig./ifconfig_copychunli@ubuntu:~/file$chmod+xifconfig_copychunli@ubuntu:~/file$./ifconfig_copyeth0Linkencap:EthernetHWaddr00:0c:29:ed:22:c1inetaddr:10.11.12.4Bcast:10.11.12.255Mask:255.255.255.0inet6addr:fe80::20c:29ff:feed:22c1/64Scope:LinkUPBROADCASTRUNNINGMULTICASTMTU:1500Metric:1RXpackets:85812errors:0dropped:14overruns:0frame:0TXpackets:55751errors:0dropped:0overruns:0carrier:0collisions:0txqueuelen:1000RXbytes:15630825(15.6MB)TXbytes:26146882(26.1MB)
结构体与文件
#include<stdio.h>#include<stdlib.h>#include<string.h>structstudent{intID;charname[100];};intmain(){//把结构体写入文件{structstudentst[]={{0,"刘德华0"},{1,"刘德华1"},{2,"刘德华2"},{3,"刘德华3"},{4,"刘德华4"},{5,"刘德华5"},{6,"刘德华6"},{7,"刘德华7"},{8,"刘德华8"},{9,"刘德华9"},};FILE*p=fopen("data","wb");if(p==NULL){printf("创建data文件失败\n");return0;}fwrite(st,sizeof(structstudent),10,p);fclose(p);}//修改结构体文件{structstudentst={45,"周杰伦"};FILE*p=fopen("data","rb+");if(p==NULL){printf("打开文件data失败\n");return0;}fseek(p,sizeof(st)*2,SEEK_SET);//将第3个位置的信息修改为stfwrite(&st,sizeof(st),1,p);fclose(p);}//从文件中读出结构体{FILE*p=fopen("data","rb");if(p==NULL){printf("打开文件data失败\n");return0;}structstudentst;memset(&st,0,sizeof(st));while(fread(&st,sizeof(st),1,p)){printf("Id=%u,name=%s\n",st.ID,st.name);}fclose(p);}return0;}chunli@ubuntu:~/file$gcc-std=c99main.c&&./a.outId=0,name=刘德华0Id=1,name=刘德华1Id=45,name=周杰伦Id=3,name=刘德华3Id=4,name=刘德华4Id=5,name=刘德华5Id=6,name=刘德华6Id=7,name=刘德华7Id=8,name=刘德华8Id=9,name=刘德华9
从结构体文件中读出指定的数据并返回耗时
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<time.h>structstudent{intID;charname[100];};intmain(){//把结构体写入文件{structstudentst[]={{0,"刘德华0"},{1,"刘德华1"},{2,"刘德华2"},{3,"刘德华3"},{4,"刘德华4"},{5,"刘德华5"},{6,"刘德华6"},{7,"刘德华7"},{8,"刘德华8"},{9,"刘德华9"},};FILE*p=fopen("data","wb");if(p==NULL){printf("创建data文件失败\n");return0;}fwrite(st,sizeof(structstudent),10,p);fclose(p);}//从文件中读出结构体{FILE*p=fopen("data","rb");if(p==NULL){printf("打开文件data失败\n");return0;}structstudentst;unsignedintID=0;clock_tct;while(1){memset(&st,0,sizeof(st));printf("请输入ID:");scanf("%u",&ID);if(ID==0){break;}ct=clock();fseek(p,sizeof(st)*(ID-1),SEEK_SET);fread(&st,sizeof(st),1,p);ct=clock()-ct;printf("ms=%ld,Id=%u,name=%s\n",ct,st.ID,st.name);}fclose(p);}return0;}chunli@ubuntu:~/file$gcc-std=c99main.c&&./a.out请输入ID:1ms=40,Id=0,name=刘德华0请输入ID:1ms=9,Id=0,name=刘德华0请输入ID:9ms=9,Id=8,name=刘德华8请输入ID:4ms=9,Id=3,name=刘德华3
结构体数组信息保存到二进制文件从二进制文件读出在结构体数组中插入一条记录保存到文件
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<time.h>structstudent{intID;charname[100];};voidinsert(structstudent*p,intlen){for(inti=len-1;i>=2;i--){p[i+1]=p[i];}}intmain(){//把结构体写入文件{structstudentst[]={{0,"刘德华"},{1,"宏碁"},{2,"戴尔"},{3,"华为"},{4,"阿里巴巴"},{5,"51CTO"},{6,"清华大学"},{7,"贝尔实验室"},{8,"阿基米德"},{9,"C/C++"},{11,"六一儿童节"},};FILE*p=fopen("data","wb");if(p==NULL){printf("创建data文件失败\n");return0;}fwrite(st,sizeof(structstudent),sizeof(st)/sizeof(structstudent),p);fclose(p);}//从文件中读出结构体{FILE*p=fopen("data","rb");if(p==NULL){printf("打开文件data失败\n");return0;}structstudent*pst=calloc(100,sizeof(structstudent));//在内存的动态存储区中分配n个长度为size的连续空间函数返回一个指向分配起始地址的指针intindex=0;while(fread(&pst[index],sizeof(structstudent),1,p))//一直读{printf("读文件%d->Id=%u,name=%s\n",index,pst[index].ID,pst[index].name);index++;}fclose(p);//在这个结构体中插入数据insert(pst,index);//此时从序号为2的结构体index都向后移动了以为pst[2].ID=12;//为新的结构体赋值strcpy(pst[2].name,"Windows怎么样");//遍历这个结构体for(inti=0;i<index+1;i++){printf("遍历结构体%d->ID=%u,name=%s\n",i,pst[i].ID,pst[i].name);}//将修改的结果保存到文件{FILE*p=fopen("data","wb");if(p==NULL){printf("创建文件失败\n");return0;}fwrite(&pst,sizeof(structstudent),index+1,p);fclose(p);}}return0;}chunli@ubuntu:~/file$gcc-std=c99main.c&&./a.out读文件0->Id=0,name=刘德华读文件1->Id=1,name=宏碁读文件2->Id=2,name=戴尔读文件3->Id=3,name=华为读文件4->Id=4,name=阿里巴巴读文件5->Id=5,name=51CTO读文件6->Id=6,name=清华大学读文件7->Id=7,name=贝尔实验室读文件8->Id=8,name=阿基米德读文件9->Id=9,name=C/C++读文件10->Id=11,name=六一儿童节遍历结构体0->ID=0,name=刘德华遍历结构体1->ID=1,name=宏碁遍历结构体2->ID=12,name=Windows怎么样遍历结构体3->ID=2,name=戴尔遍历结构体4->ID=3,name=华为遍历结构体5->ID=4,name=阿里巴巴遍历结构体6->ID=5,name=51CTO遍历结构体7->ID=6,name=清华大学遍历结构体8->ID=7,name=贝尔实验室遍历结构体9->ID=8,name=阿基米德遍历结构体10->ID=9,name=C/C++遍历结构体11->ID=11,name=六一儿童节
我有一份学生名单文件需要为每个学生随机ID值存储到文件再读出来按照ID顺序打印到屏幕
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<time.h>structstudent{unsignedintID;charname[20];};voidcreate_num(intarr[],intlen)//将传过来的数组装入乱序随机值{inti=0;intj=0;srand((unsignedint)time(NULL));while(i<len){inttmp=rand()%len;for(j=0;j<i;j++){if(arr[j]==tmp){break;}}if(j==i){arr[i]=tmp;i++;}}}intmain(){////////从文件中读取信息修改存储到文件///////////////////////////////////////////////{srand((unsignedint)time(NULL));FILE*p=fopen("student","r");if(p==NULL){return0;}FILE*p2=fopen("data","wb");if(p2==NULL){return0;}structstudentst={0,""};charbuf[100]={0};intarr[100];//定义一个数组create_num(arr,sizeof(arr)/sizeof(int));//为数组载入n个乱序不重复个数值intindex=0;while(!feof(p)){fgets(st.name,sizeof(st.name),p);intlen=strlen(st.name);if(st.name[len-1]=='\n'){st.name[len-1]=0;}//去除每一行的'\n'字符st.ID=arr[index++];//把数组的乱序不重复值拿出来//printf("%d,%s\n",st.ID,st.name);fwrite(&st,sizeof(st),1,p2);}fclose(p);fclose(p2);}////////从文件中读取////////////////////////////////////////////////{FILE*p=fopen("data","rb");if(p==NULL){return0;}structstudentst[100];structstudenttmp;intindex=0;while(fread(&st[index++],sizeof(structstudent),1,p));//将文件读取到内存/////////将结构体按照ID排序一下//////////////////////////////////////////////////for(inti=1;i<index-1;i++){for(intj=1;j<index-i-1;j++){if(st[j].ID<st[j-1].ID){tmp=st[j];st[j]=st[j-1];st[j-1]=tmp;}}}////////////遍历输出结构体信息//////////////////////////////////////////for(inti=0;i<index-2;i++){printf("%d,%s\n",st[i].ID,st[i].name);}}return0;}学生姓名为文件chunli@ubuntu:~/file$catstudent张三丰李小龙朴槿惠联想戴尔孙悟空收音机银河系西瓜编译执行chunli@ubuntu:~/file$gcc-std=c99main.c&&./a.out16,戴尔23,联想41,孙悟空55,西瓜58,收音机71,银河系74,张三丰84,李小龙97,朴槿惠
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。