1. 程式人生 > >利用strrchr函式從字串中分離字元

利用strrchr函式從字串中分離字元

比如在用FIFO寫單伺服器多使用者的程式中,要分離出使用者請求行中的路徑,可以用strrchr函式。
#include <string.h>
函式原型:extern char * strrchr (const char *s, int c)

引數說明:s為一個字串的指標,c為一個待查詢字元。

查詢在s字串中最後一次出現字元c的位置。

使用者請求行格式為 PID+ “ ”+path,要分離path,即可這樣做:

#include<stdio.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<errno.h>
#include<string.h>
//從一個字串中分割出什麼

int main(int argc, char const *argv[])
{
	char requestline[128];
	char *path;
	printf("please enter the request line: ");
	fgets(requestline,128,stdin);//fgets函式遇到空格不停止
	char c = ' ';
	path = strrchr(requestline,c);
	fputs(path,stdout);

	return 0;
}

執行結果:
strrchr函式