strcpy(home,"");
for(j=del1;j<del2;j++){
home[ strlen(home) ] = word[j];
printf("your house is %s",home);
但是我得到了垃圾。我试着这样做:
strcat(word[j],home);
但是当我运行它时它没有工作
我正在尝试编写一个简单的程序来从文件中写入/读取单词:
WRITTE:
fp = fopen ( "houses.txt", "a" );
fprintf(fp,"%s&",home);
fclose ( fp );
printf(" Inserted elementn");
读:char c, home[50],word[100];
strcpy(home,"");
int i=0,del1=0,del2=0,j;
FILE *fp;
fp = fopen ( "houses.txt", "r" );
while (c!=EOF)
{
c=getc(fp);
word[i]=c;
i=i+1;
if (c=='&')
{
del2=i-1;
strcpy(home,"");
for(j=del1;j<del2;j++)
{
strcat(word[i], home);// OR home[ strlen(home) ] = word[j];
}
del1=del2;
printf("%s n",home);
}
}
fclose ( fp );
如果您要做的只是打印文件中每个&
分隔的字符串,那么您应该将字符读入缓冲区,直到找到&
。然后,将&
替换为