标签:des style blog http color io os ar for
fopen后必须判断 FILE *p 是否返回NULL
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define S_KEY 10
void code(const char * src , const char * dest)
{
FILE * fp1 = fopen( src , "r" );
FILE * fp2 = fopen( dest , "w" );
if( !fp1 || !fp2)
{
printf("打开文件失败\n");
exit( -1 );
}
char buf[1024];
int len = 0 , i = 0;
while(fgets( buf , sizeof(buf) , fp1) != NULL)
{
len = strlen(buf);
for( i = 0 ; i < len ; ++i)
buf[i] += S_KEY;
fputs( buf , fp2);
}
}
void decode(const char * src , const char * dest)
{
FILE * fp1 = fopen( src , "r" );
FILE * fp2 = fopen( dest , "w" );
if( !fp1 || !fp2)
{
printf("打开文件失败\n");
exit( -1 );
}
char buf[1024];
int len = 0 , i = 0;
while(fgets( buf , sizeof(buf) , fp1) != NULL)
{
len = strlen(buf);
for( i = 0 ; i < len ; ++i)
buf[i] -= S_KEY;
fputs( buf , fp2);
}
}
int main(int argc , char * argv[])
{
if( argc < 4)
{
printf("参数一 是加密(1)或解密(2)\n");
printf("参数二 是源文件\n参数三 是目标文件\n");
exit(-1);
}
if( atoi(argv[1]) == 1)
code(argv[2],argv[3]);
else if(atoi(argv[1]) == 2)
decode(argv[2],argv[3]);
return 0;
}
#include <stdio.h>
int main()
{
FILE *fp = fopen( "test.txt" , "r" ); //这个文件是有hello world 因为有空格 只能读出来hello
char buf[100] ;
fscanf( fp , "%s" , buf);
printf("%s\n" , buf );
fclose( fp);
return 0;
}
FILE *p = fopen( "a.txt" , "wb");
#include <stdio.h>
int main()
{
FILE *fp = fopen("a.txt","r+");
if(!fp)
{
printf("打开文件失败\n");
return -1;
}
char buf[1024] = "刘威";
fputs(buf , fp);
return 0;
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "myudp.h"
#pragma comment (lib,"myudp.lib")
int main(int argc , char *argv[])
{
if(argc < 3)
{
printf("[./app] [FILE_NAME] [SEND:1] [IP]\n");
printf("[./app] [FILE_NAME] [RECV:2]\n");
return -1;
}
if( atoi(argv[2]) == 1)
{
FILE *fp = fopen( argv[1] , "r");
if( !fp )
{
printf("打开文件失败\n");
return -1;
}
if(argv[3] == NULL)
{
printf("请输入你需要发送的IP\n");
return -1;
}
char buf[1024] , ch;
while( !feof( fp ) )
{
memset(buf , 0 , sizeof(buf));
fgets( buf , sizeof(buf) , fp );
send_socket( argv[3], 8080, buf, sizeof(buf));
}
ch = EOF;
send_socket( argv[3], 8080, &ch, sizeof(ch));
fclose(fp);
}
else if( atoi(argv[2]) == 2) //接收的
{
FILE *fp = fopen( argv[1] , "w");
if( !fp )
{
printf("打开文件失败\n");
return -1;
}
char buf[1024];
char IP[1024];
bind_socket( 8080 );
while( 1 )
{
memset( buf , 0 , sizeof(buf));
memset( IP , 0 , sizeof(IP));
recv_socket(buf, sizeof(buf), IP);
if( buf[0] == EOF)
break;
fputs( buf , fp);
}
fclose(fp);
}
return 0;
}
#include <stdio.h>
int main()
{
FILE *fp = fopen("a.txt","r");
char buf[1024];
int count = 0;
while(fgets(buf,sizeof(buf),fp))
{
count++;
}
printf("%d\n",count);
return 0;
}
#include <stdio.h>
int main()
{
char buf[1024];
int count = 0;
FILE *fp = fopen("a.txt","r");
while(!feof(fp))
{
fgets(buf,sizeof(buf),fp);
count++;
}
printf("%d\n",count);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int file_count(FILE *fp)
{
char buf[1024] , len = 0;
while( fgets( buf , sizeof(buf) , fp) != NULL )
{
len++;
}
rewind(fp);
return len;
}
void show_array(int *p , int len)
{
int i;
for( i = 0 ; i < len ; i++)
printf("%d\n",p[i]);
}
//选择排序
void sort_array(int *p , int len)
{
int i, j , min , tmp;
for(i = 0 ; i < len ; ++i)
{
min = i;
for(j = i+1 ; j < len ; ++j)
if(p[min] > p[j])
min = j;
if(min != i)
{
tmp = p[i];
p[i] = p[min];
p[min] = tmp;
}
}
}
int main()
{
FILE *fp = fopen("t.txt" , "r");
if(!fp)
{
printf("打开文件失败\n");
return -1;
}
int len = file_count(fp) , i = 0;
int *p = (int *)malloc(sizeof(int) * len);
memset(p , 0 , sizeof(int) * len);
char buf[1024] = {0};
for( i = 0 ; i < len ; i++)
{
fgets( buf , sizeof(buf) , fp);
p[i] = atoi(buf);
}
//show_array( p , len);
sort_array( p , len);
//show_array( p , len);
FILE *fp2 = fopen("sort.txt" , "w");
for( i = 0 ; i < len ; i++)
fprintf(fp2 , "%d\n" , p[i]);
free(p);
fclose(fp);
fclose(fp2);
return 0;
}
标签:des style blog http color io os ar for
原文地址:http://www.cnblogs.com/l6241425/p/3965306.html