作者laechan (小太保)
看板mud_sanc
標題[閒聊] C語言練習
時間Sun Aug 7 12:14:27 2011
/*
test.txt
=======================
adm_c laechan
admin belldandy
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define file_name "test.txt"
int main()
{
int i;
char str[]="hello world!",tmp1[50],tmp2[50];
FILE *fp;
// printf("hello world!\n"); // 一定要練的XD
for(i=1;i<=2;i++)
printf("%d.%s\n",i,str);
printf("please input a word: ");
scanf("%s",tmp1);
printf("the word you inputed is %s.\n",tmp1);
printf("please input another word: ");
scanf("%s",tmp2);
strcat(tmp1,tmp2);
printf("tmp1+tmp2=%s.\n",tmp1);
// 檔案處理
if((fp=fopen(file_name,"r"))!=NULL)
{
printf("the file %s is exist.\n",file_name);
while(!feof(fp))
{
if(fscanf(fp,"%s %s",tmp1,tmp2)==2)
printf("%s is a %s\n",tmp2,tmp1);
}
}
fclose(fp);
return 1;
}
===執行結果===
1.hello world!
2.hello world!
please input a word: abc
the word you inputed is abc.
please input another word: def
tmp1+tmp2=abcdef.
the file test.txt is exist.
laechan is a adm_c
belldandy is a admin
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.26.181.15
1F:→ laechan :下午再繼續, 今天目標是寫出一些檔案跟字串處理函數 08/07 12:15
2F:→ laechan :指標的使用是最麻煩的地方 08/07 12:15
※ 編輯: laechan 來自: 117.19.126.121 (08/08 10:00)
3F:推 tonyofthu :C語言耶~ 好懷念~ :P 08/08 10:52
4F:→ laechan :今天練一練明天大概就可以小試一下 08/08 12:56