Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 我怎样才能避免垃圾?_C_Arrays_String_File - Fatal编程技术网

C 我怎样才能避免垃圾?

C 我怎样才能避免垃圾?,c,arrays,string,file,C,Arrays,String,File,但当我运行它时,它不起作用 我正在尝试制作一个简单的程序来从文件中写入/读取单词: 写: strcat(word[j],home); 全文如下: fp = fopen ( "houses.txt", "a" ); fprintf(fp,"%s&",home); fclose ( fp ); printf(" Inserted element\n"); charc,home[50],word[100]; strcpy(home,“”); int i=0,del1=0,del2=0,j;

但当我运行它时,它不起作用

我正在尝试制作一个简单的程序来从文件中写入/读取单词:

写:

strcat(word[j],home);
全文如下:

fp = fopen ( "houses.txt", "a" );
fprintf(fp,"%s&",home);
fclose ( fp );
printf(" Inserted element\n");
charc,home[50],word[100];
strcpy(home,“”);
int i=0,del1=0,del2=0,j;
文件*fp;
fp=fopen(“houses.txt”,“r”);
而(c!=EOF)
{
c=getc(fp);
字[i]=c;
i=i+1;
如果(c=='&')
{
del2=i-1;
strcpy(home,“”);

对于(j=del1;j如果您要做的只是打印文件中的每个
&
分隔字符串,那么您应该将字符读入缓冲区,直到找到
&
。然后,将
&
替换为
\0
,打印缓冲区,然后将插入点重置为缓冲区的开头。类似于this(注意没有任何错误检查)


你到底在做什么?家是什么?字是什么?垃圾输入,垃圾输出。很简单。
home
是如何声明的?那么如果它被覆盖,那么
strlen
将如何找到它呢???
strlen
将找到一些
'\0'
,可能远离数组末尾(除非整个内存中的每个字节都不为零,否则很可能会出现分段错误)。
strcat
要求将
char*
作为其第一个参数,但
word[i]
可能是
char
。我仍然不知道您在这里究竟想实现什么。
fp = fopen ( "houses.txt", "a" );
fprintf(fp,"%s&",home);
fclose ( fp );
printf(" Inserted element\n");
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 );
#include <stdio.h>

int main(int argc, char **argv)
{
    char home[50];
    int i, c;
    FILE *fp;

    fp = fopen ("houses.txt", "r");

    i = 0;

    while ((c = fgetc(fp)) != EOF) {
        if (c == '&') {
            home[i] = '\0';
            puts(home);
            i = 0;
        }
        else {
            home[i++] = c;
        }
    }

    fclose ( fp );

    return 0;
}
#include <stdio.h>

int main(int argc, char **argv)
{
    char home[50];
    FILE *fp;

    fp = fopen ("houses.txt", "r");

    while (fscanf(fp, "%[^&]&", home) == 1) {
        puts(home);
    }

    fclose ( fp );

    return 0;
}