1

創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站制作、網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)與策劃設(shè)計(jì),中衛(wèi)網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)10年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:中衛(wèi)等地區(qū)。中衛(wèi)做網(wǎng)站價(jià)格咨詢:18980820575
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include stdio.h
#include time.h
/*把從src地址開始的字符串復(fù)制到以dest開始的字符串中*/
char* strcopy (char* dest, char *src) {
char* r = dest;
while((*r++ = *src++)!='\0');
return dest;
}
int main () {
char *str1="abcde";
char *str2;
str2 = strcopy (str2, str1); /*把str1復(fù)制到str2*/
puts (str2);
return 0;
}
他是字符串的復(fù)制,函數(shù)strcpy(字符數(shù)組1,字符串2)作用就是 將字符串2復(fù)制到字符數(shù)組1 中去。
例如 char str1[10]='',
str2[]={“china”}
strcpy(str1,str2);
這樣str1[]數(shù)組的內(nèi)容就是“china”。
擴(kuò)展資料:
C語言庫函數(shù),常用庫函數(shù)有:
1、scanf格式輸入函數(shù)
2、printf格式輸出函數(shù)
3、systemdos命令函數(shù)
4、sort排序
5、main主函數(shù)
6、fgets文件讀取字符串函數(shù)
7、fputs文件寫入字符串函數(shù)
8、fscanf文件格式讀取函數(shù)
9、fprintf文件格式寫入函數(shù)
10、fopen打開文件函數(shù)
11、getchar輸入字符函數(shù)
12、putchar輸出字符函數(shù)
13、malloc動(dòng)態(tài)申請(qǐng)內(nèi)存函數(shù)
參考資料來源:百度百科-函數(shù)
1、首先需要建立一個(gè)新的文件,輸入頭文件和主函數(shù)。
2、接下來需要定義變量類型。
3、設(shè)置完變量類型之后開始調(diào)用cpy函數(shù)。
4、接下來需要定義一個(gè)函數(shù),并定義變量類型。
5、最后加一個(gè)字符串結(jié)束符,并在主函數(shù)中輸出。
6、編譯。運(yùn)行,可以看到字符串a(chǎn)復(fù)制到字符串b中。
#include stdio.h
int copy_str(char *src, char *dst)
{
if(!src || !dst)
{
return -1;
}
while(*src!='\0')
{
*dst++ = *src++;
}
*dst = '\0';
return 0;
}
int main()
{
char *s1 = "hello world";
char s2[512] = {0};
copy_str(s1, s2);
printf("%s", s2);
return 0;
}
C語言實(shí)現(xiàn)一個(gè)簡(jiǎn)單的文件復(fù)制功能,Linux環(huán)境下。
思路步驟:(下代碼最重要的邏輯步驟清晰)
第一步:打開源文件(要復(fù)制的文件),打開文件的方式以讀的方式就可以了。
Linux C打開文件的庫函數(shù)有:int open(const char *pathname,int flags),int open(const char *pathname,mode_t mode),以及 FILE *fopen(const char *path,const char *mode),FILE *fdopen(int fd,const char *mode),這幾個(gè)函數(shù),具體的使用方法就查看manual就可以了。
第二步:創(chuàng)建目標(biāo)文件,所用的函數(shù)也是上面那幾個(gè)。
第三步:讀取文件。庫函數(shù)有:size_t read(int fd,void *buf,size_t count),
size_t fread(void *ptr,size_t size,size_t nmemb,FILE *stream)
第三步:寫入目標(biāo)文件。用的庫函數(shù):size_t write(int fd,void *buf,size_t count),
size_t fwrite(void *ptr,size_t size,size_t nmemb,FILE *stream)
第四步:關(guān)閉文件。庫函數(shù):int fclose(FILE *fp) ,int close(int fd)
思路步驟就是這樣子的了。下面是具體的代碼實(shí)現(xiàn)。
#include
#include
#include
#include
#include
#include
int main(int argc,char *argv[])
{
int fd_source_file,fd_copy_file;//用接受int open()函數(shù)返回的值
//FILE *fp_source_file,*fp_copy_file;//如果用FILE *fopen()函數(shù)的話
int size_read,size_write;
char buf[1024];
char copy_file_name[50];
//檢查參數(shù)的輸入
if(argc3)
{
printf("usage: ./a.out source_file_path copy_file_path\n");
exit(1);
}
//復(fù)制目標(biāo)文件名
strcpy(copy_file_name,argv[2]);
//打開源文件
if( (fd_source_file=open(argv[1],O_RDONLY,00744))0 )
{
perror("open source file error");
exit(1);
}
//創(chuàng)建目標(biāo)文件
if( (fd_copy_file=open(argv[1],O_CREAT|O_RDWR)) 0 )
{
perror("create copy file error");
exit(1);
}
do
{
//讀取文件內(nèi)容
if( (size_read=read(fd_source_file,buf,1024)) 0 )
{
perror("read source file error");
exit(1);
}
//寫入目標(biāo)文件
if( (size_write=write(fd_copy_file,buf,sieze_read))0 )
{
perror("wrire file error");
exit(1);
}
}while(size_read==1024)
return 0;
}
#includestdio.h
char *custom_cpy(char *to,char *from);
int main(int argc,char *argv[]){
char *from = "i like c langanger";
char to[30];
char *ptr = custom_cpy(to,from);
printf("%s,%s\n",ptr,to);
return 0;
}
char *custom_cpy(char *to,char *from){
if(to == NULL || from == NULL){
return NULL;
}?
char *p = to;
for(;*from!='\0';from++,to++){
*to = *from;
}?
*to = '\0';//拷貝完畢之后一定要加一個(gè)結(jié)束符號(hào)
return p;
}
擴(kuò)展資料:
字符串相關(guān)函數(shù)應(yīng)用:
1. 連接運(yùn)算 concat(s1,s2,s3…sn) 相當(dāng)于s1+s2+s3+…+sn。
例:concat(‘11’,'aa’)='11aa’;
2. 求子串。 Copy(s,I,I) 從字符串s中截取第I個(gè)字符開始后的長度為l的子串。
例:copy(‘a(chǎn)bdag’,2,3)=’bda’;
3. 刪除子串。過程 Delete(s,I,l) 從字符串s中刪除第I個(gè)字符開始后的長度為l的子串。
例:s:=’abcde’;delete(s,2,3);結(jié)果s:=’ae’;
4. 插入子串。 過程Insert(s1,s2,I) 把s1插入到s2的第I個(gè)位置
例:s:=abc;insert(‘12’,s,2);結(jié)果s:=’a12bc’;
5. 求字符串長度 length(s) 例:length(‘12abc’)=5;
參考資料:
百度百科-字符串
分享文章:c語言復(fù)制函數(shù),c語言復(fù)制函數(shù)全部替換嗎
本文鏈接:http://chinadenli.net/article8/dsicoop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、移動(dòng)網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站內(nèi)鏈、網(wǎng)站改版、云服務(wù)器
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)