[閒聊] C語言練習
/*
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
→
08/07 12:15, , 1F
08/07 12:15, 1F
→
08/07 12:15, , 2F
08/07 12:15, 2F
※ 編輯: laechan 來自: 117.19.126.121 (08/08 10:00)
推
08/08 10:52, , 3F
08/08 10:52, 3F
→
08/08 12:56, , 4F
08/08 12:56, 4F