文章目錄
- 1.scanf函數相關應用
- 2.在一串字元中取出小數
- 3.兩個檔案copy
- 4.指標一些操作
- 5. stdout、stdin、stderr
- 6.sscanf、sprintf函數
1.scanf函數相關應用
#include <stdio.h>
int main( )
{
int i=0;
float f;
char c1,c2;
//scanf("%3d%f",&i,&f);//3d表示只取3個位
//printf("i=%010d,f=%010.2f\n",i,f);//前面的0表示剩餘的空位用0補齊。
int w;
//for (w=1;w<5;w++)
//printf("%c,%*c\n",'a',w,'a');//中間的*代表每次輸入時列印w-1個空格(若w-1<0,以0計算)。
//scanf("%3d%3d",&i,&w);//截取3位,如果在這3位中有空白字元,停止。
//printf("i=%d,w=%d\n",i,w);
//scanf("%*d%d",&i,&w);//*表示禁止,第一次輸入的數字忽略。
//printf("i=%d,w=%d\n",i,w);
//scanf("%[a-d]%c",&c1,&c2);//輸入[a-d]之間的字元,如果沒有輸入a-d之間的字元,
//程式不能正常取數值,注意,輸入字元時,不能過濾空白字元。[^a-d]表示非(a-z)之間字元
//printf("c1=%c,c2=%c\n",c1,c2);
scanf("%*[^\n]");
scanf("%*c");//清空輸入緩衝區。(==fflush(stdin))
char str1[20],str2[20];
//scanf("%*[^\n]");scanf("%*c");
//scanf("%[a-z]%[^a-z^\n]", str1, str2);//後面的^\n表示輸入的資料會立即生效。
scanf("%[a-z]%[^a-z^\n]", str1, str2);//a-z表示只提取a-z之間字元,若輸入為abc123ty,str1=abc.
printf("str1=%s, str2=%s\n", str1, str2);
return 0;
}
2.在一串字元中取出小數
#include <stdio.h>
int main()
{
float farray[20];int i=0;
FILE *pf=fopen("1.txt","r");
float d;
char ch;
while (ch=getc(pf),ch!=EOF)//沒有取到檔案結尾時
{
if (ch<='9' && ch>='1')
{
ungetc(ch,pf);//使用ungetc才能將小數的第一位元取到,不然會錯過一位。因為當ch中為數字時,指標已經向後走了一位,此時後面的scanf會錯過一位,所以要用ungetc。
fscanf(pf,"%f",&d);
farray[i++]=d;
printf("%f\n",d);
}
}
fclose(pf);
return 0;
}
#include <stdio.h>
#include <ctype.h>
//把整數取出來
void main( void )
{
int ch;
int result = 0;
printf( "Enter an integer: " );
while ( (ch=getchar())!=EOF && isdigit(ch)!=0 )
{result=result*10+ch;}
if (ch!=EOF)
{ungetc(ch,stdin);}
printf("%c\n",getchar());
}
3.兩個檔案copy
//從命令列輸入源檔案名稱和目標檔案名,完成copy
#include <stdio.h>
int main( int argc,int *argv[] )
{
FILE *psoruce,*pdest;char c;
if (argc!=3)//輸入格式錯誤
{
fprintf(stderr,"the usage is: %s source dest\n",argv[0]);
return 1;
}
if ( psoruce=fopen(argv[1],"r"),NULL==psoruce )
{
fprintf(stderr,"open %s fail!\n",argv[1]);
return 1;
}
if ( pdest=fopen(argv[2],"w"),NULL==pdest )
{
fprintf(stderr,"open %s fail!\n",argv[2]);
fclose(psoruce);
return 1;
}
while (c=getc(psoruce),c!=EOF)
{putc(c,pdest);}
fclose(psoruce);fclose(pdest);
}
附:
int *restrict a//restrict 是在間接定址的時候進行最佳化,在c99中新增的,這個關鍵字只能用於修飾指標,因為指標才有間接定址。
register int x=10;//register是指將變數請求放到cpu寄存器中,這樣對它操作的速度會很快,但是只是請求,程式不一定會放在寄存器中。
volatile int y=20;//volatile表示此變數是一個易變變數,在用它的時候要小心,可以阻止編譯器的一些最佳化操作,
4.指標一些操作
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void pt(int (*a)[4],int n)
{
int i,j;
for (i=0;i<n;i++)
{
for (j=0;j<4;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("\n");
}
int main()
{
//double x;
//strcpy((char *)&x,"abcde");
//printf("%s\n",&x);
//int y;
//char *pc=(char*)&y;
//*pc='a';
//*(pc+1)='b';
//*(pc+2)='c';
//*(pc+3)='d';
//printf("%#x\n",y);//#x輸出就是0x
// char *p=(char *)malloc(8);
// strcpy(p,"abcde");
// printf("%s\n",p);
// free(p);
int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};
// double *pd=(double*)a;
// int i=0;
// for(i=0;i<6;i++)
// pd[i]=i+0.12;
// for (i=0;i<6;i++)
// {
// printf("%lf\t",pd[i]);
// }
char *pc=(char*)a;
strcpy(pc,"abcdefg");
printf("%s\n",pc);//分配的記憶體可以儲存給double型,也可以給char來應用 。
//int (*p)[4];//p和*p都是一樣的,因為p是一個數組指標,指向一個數組,所以它的*p和p是一樣的,p+1指向下組資料,p+1=*(p+1).只不過類型不一樣。
//p=a;
//char **pa=(char **)a;//windows的編譯檢查得到了加強,要做強制類型轉換才可以。
//*pa=1;*(pa+1)=2;
// printf("%p\n",p);
// printf("%p\n",*p);
// printf("%d\n",**p);
// printf("%p\n",p+1);
// printf("%p\n",*p+1);
// printf("%d\n",**(p+1));
//printf("%c %c\n",*pa,*(pa+1)); //pa指向二維數組展開的一維數組的首地址,pa+1是向後走4個位元組(若在int 型中,剛好指向下一個位元組,若在char和double中,不會是這種情況)。
//pt(a,3);
return 0;
}
5. stdout、stdin、stderr
#include <stdio.h>
//每一個進程都預設的開啟這三個檔案:
//FILE *stdin; //標準輸入裝置檔案(鍵盤)
//FILE *stdout; //標準輸出裝置檔案(顯示器)
//FILE *stderr; //標準錯誤輸出裝置檔案(顯示器)
int main()
{
fprintf(stdout,"%s","hello world!\n");//此句同2句是一樣的。
//printf("%s","hello world!\n");
fprintf(stderr,"%s","hello world!\n");
int x;
fscanf(stdin,"%d",&x);//此句同下句是一樣的。
scanf("%d",&x);
/* stdout和stderr的區別:
1.stdout有輸出緩衝區,stderr沒有輸出緩衝區。
2.stdout可以重新導向,stderr沒有重新導向(也就是stdout可以把輸出結果送到其它地方,stderr不可以);
3.什麼時候stdout會清空緩衝區:
當輸出換行時、緩衝區滿時、用fflush重新整理時、等待使用者輸入時、程式運行結束時,當緩衝區清空時,緩衝區的內容會送到顯示器*/
return 0;
}
6.sscanf、sprintf函數
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x=100;
int y=200;
char c;
double d;
char s[100]={0};
//sprintf(s,"%d-%d",x,y);//sprint 可以向得到想要的字串類型。
//printf("%s\n",s);
//char *ps="100";
//int z=atoi(ps);//atoi將一個字串轉化成一個整數
//printf("%d\n",z);
//double d=3.14;
//sprintf(s,"%7.3f",d);
//printf("%s\n",s);
char *ps="a 10 3.14 string";
sscanf(ps,"%c%d%lf%s",&c,&x,&d,s);
printf("%c-%d-%lf-%s",c,x,d,s);
return 0;}