作者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