92 date:2021.2.18
要点:
fopen(文件名,使用文件方式)
fputc(要输出的字符,文件指针)
详细代码如下:
#include <stdio.h>
#include <stdlib.h>
int fun(char *source, char *target)
{
FILE *fs,*ft; char ch;
/**********found**********/
if((fs=fopen(source, "r"))==NULL) //以只读方式打开文件
return 0;
if((ft=fopen(target, "w"))==NULL)
return 0;
printf("\nThe data in file :\n");
ch=fgetc(fs);
/**********found**********/
while(!feof(fs)) //feof()函数来检查是否到文件结尾
{
putchar( ch );
/**********found**********/
fputc(ch,ft); //fputc()函数用于将一个字符写道磁盘文件上去
ch=fgetc(fs);
}
fclose(fs); fclose(ft);
printf("\n\n");
return 1;
}
void main()
{
char sfname[20] ="myfile1",tfname[20]="myfile2";
FILE *myf; int i; char c;
myf=fopen(sfname,"w");
printf("\nThe original data :\n");
for(i=1; i<30; i++){
c='A'+rand()%25;fprintf(myf,"%c",c); printf("%c",c); }
fclose(myf);printf("\n\n");
if (fun(sfname, tfname)) printf("Succeed!");
else printf("Fail!");
}
要点:
可作为编程大题
详细代码如下:
#include <stdio.h>
void fun (long s, long *t)
{
int d;
long sl=1;
*t = 0;
while ( s > 0)
{
d = s%10;
/************found************/
if (d%2==0)
{
*t=d* sl+ *t;
sl *= 10;
}
/************found************/
s /= 10;
}
}
void main()
{
long s, t;
printf("\nPlease enter s:"); scanf("%ld", &s);
fun(s, &t);
printf("The result is: %ld\n", t);
}
要点:
ASCII码
详细代码如下:
#include <stdio.h>
#include <string.h>
void fun(char *s, char t[])
{
/*
analyse:
目标:
将 s 字符串中除下标为偶数&&ASCII码值也为偶数外 ,其余的全部删除;
剩余的字符放入 t数组中;
*/
int i,j = 0;
for(i = 0; s[i] != '\0'; i++)
{
if((i % 2==0) && (s[i] %2 ==0))
{
t[j] = s[i];
j++;
}
}
t[j] = '\0'; //字符串最后加上结束标识
}
void main()
{
char s[100], t[100];
void NONO ( );
printf("\nPlease enter string S:"); scanf("%s", s);
fun(s, t);
printf("\nThe result is: %s\n", t);
NONO();
}