Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/69.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 在txt文件中查找字符串并显示一行_C - Fatal编程技术网

C 在txt文件中查找字符串并显示一行

C 在txt文件中查找字符串并显示一行,c,C,嘿,伙计们,在这里写代码,我有点迷路了 int main() { FILE *fp; struct info { char name[15]; char surename[15]; char gender[15]; char education[15]; } info; char c; int i, j, a; struct info sem; beginning:

嘿,伙计们,在这里写代码,我有点迷路了

int main()
{

    FILE *fp;
    struct info
    {
        char name[15];
        char surename[15];
        char gender[15];
        char education[15];

    } info;

    char c;
    int i, j, a;
    struct info sem;

  beginning:

    scanf("%d", &a);

if(a==1)
{
fp = fopen("info.txt", "r");
     for(i=0;!feof(fp);i++)
 {   fscanf(fp, "%s %s %s %s", 
                sem.name,
                sem.surname,
                sem.gender,
                sem.education,);
                printf("%s, %s, %s, %s\n",
                sem.name,
                sem.surname,
                sem.gender,
                sem.education,);
     }   
       fclose(fp);
  goto beginning;
    }
   if (a == 2)

    {
        FILE *fp = fopen("info.txt", "r");
        char tmp[256] = { 0x0 };
        while (fp != NULL && fgets(tmp, sizeof(tmp), fp) != NULL)

        {

            if (strstr(tmp, "bachelors"))

                /* Code works fine until this part */

                fprintf(fp, "\n%s %s %s %s %s %s",
                        sem.name, sem.surname, sem.gender, sem.education,);

        }
        if (fp != NULL)
            fclose(fp);
        goto beginning;
    }
在找到字符串“Bamber”后,它应该打印找到它的那一行的其余部分,在这个例子中,一个拥有学士学位的用户的名字、姓氏等,但是它没有,如果有人能指出我的错误,我将非常感激

执行以下操作: 1.修改结构 2.摆脱代码中的
GOTO
(这是一个糟糕的编程标准) 3.正确的代码缩进

检查下面的代码

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
    struct info
    {
        char name[15];
        char surname[15];
        char gender[15];
        char education[15];

    } sem;

    FILE *fp=NULL;
    int i, a;
    char tmp[256] = {0x0};

    while(1)
    {
        printf("Enter the value\n");
        scanf("%d", &a);

        if((fp = fopen("info.txt", "r")) != NULL)
        {

            switch(a)       // assuming your code is just an snippet and actual code has more than 2 conditions
            {               // use if-else if just 2 condition checks

                case 0:
                        exit(0);        // quit the program

                case 1: 

                    for(i=0;!feof(fp);i++)
                    {
                        fscanf(fp, "%s %s %s %s", sem.name, sem.surname, sem.gender, sem.education);
                        printf("%s, %s, %s, %s\n",sem.name,sem.surname,sem.gender,sem.education);
                    }   

                    break;

                case 2:

                    while (fgets(tmp, sizeof(tmp), fp) != NULL)
                    {
                        if (strstr(tmp, "bachelors"))
                        {
                            /* Code works fine until this part */
                            fprintf(fp, "\n%s %s %s %s", sem.name, sem.surname, sem.gender, sem.education);                         
                        }   
                    }

                    break;

                default:    printf("Default statement");                                        
            }

            fclose(fp);

        }
    }
}
#包括
#包括
#包括
int main()
{
结构信息
{
字符名[15];
查氏[15];
性别[15];
儿童教育[15];
}扫描电镜;
FILE*fp=NULL;
int i,a;
char tmp[256]={0x0};
而(1)
{
printf(“输入值”);
scanf(“%d”和“&a”);
如果((fp=fopen(“info.txt”,“r”))!=NULL)
{
开关(a)//假设您的代码只是一个片段,而实际代码有两个以上的条件
{//如果只有两个条件检查,则使用if-else
案例0:
退出(0);//退出程序
案例1:
对于(i=0;!feof(fp);i++)
{
fscanf(fp,“%s%s%s%s”,sem.name,sem.姓氏,sem.gender,sem.education);
printf(“%s、%s、%s、%s\n”、sem.name、sem.姓氏、sem.gender、sem.education);
}   
打破
案例2:
while(fgets(tmp,sizeof(tmp),fp)!=NULL)
{
if(strstr(tmp,“单身人士”))
{
/*在这部分之前,代码工作正常*/
fprintf(fp,“\n%s%s%s%s”,sem.name,sem.姓氏,sem.gender,sem.education);
}   
}
打破
默认:printf(“默认声明”);
}
fclose(fp);
}
}
}

看起来您没有给
sem
分配任何内容,但您正在打印
sem
成员希望它包含一些内容。不,不是可怕的
转到
!抱歉,我忘了添加代码部分
while(fgets(tmp,sizeof(tmp),fp)!=NULL)
似乎也不需要您只需执行
fgets
,然后继续