C 使用管道文件传输名称

C 使用管道文件传输名称,c,C,我有3个名为pre、sort和pipe的“.c”文件。Pre从控制台获取用户输入的名称和GPA。如果GPA大于或等于3.0,则名称将存储到结构中 这是pre.c文件: #include<stdio.h> #include<stdlib.h> #include<string.h> struct student{ char temp_name[50]; char names[50]; }; int main() { int read_index =

我有3个名为pre、sort和pipe的“.c”文件。Pre从控制台获取用户输入的名称和GPA。如果GPA大于或等于3.0,则名称将存储到结构中

这是pre.c文件:

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

struct student{
  char temp_name[50];
  char names[50];
};

int main()
{
  int read_index = 0;
  float check_gpa;

  struct student data[read_index];

  printf("Enter student name and GPA: \n");
  scanf("%s %f\n", data[read_index].temp_name, &check_gpa);
  read_index++;

  while(scanf("%s %f\n", data[read_index].temp_name, &check_gpa) != EOF)
       {
         if (check_gpa >= 3.0)
            {
              strcpy(data[read_index].names, data[read_index].temp_name);
              read_index++;
            }
       }

  return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<string.h>
#include<sys/wait.h>

int main()
{
  char *args[] = {"./sort", NULL};
  char *argv[] = {"./pre", NULL};

  int pipe_end[2];
  int pipe_id;
  pipe(pipe_end);

  if (pipe(pipe_end)==-1) // Check for pipe functionality
    {
        perror("Pipe Failed");
        return 1;
    }

  pipe_id = fork();

  if (pipe_id < 0) //Check for fork functionality
  {
    printf("Fork failed");
    return 1;
  }
  else if(pipe_id == 0)//Child
  {
    close(pipe_end[0]);
    dup(pipe_end[0]);
    execvp(argv[0], argv);
  }
  else //Parent
  {
    wait(NULL);
    close(pipe_end[1]);
    dup2(pipe_end[1], 0);
    close(pipe_end[0]);
    execvp(args[0], args);

  }

  return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<string.h>
#include<sys/wait.h>

struct student{
  char temp_name[50];
  char names[50];
};

int main()
{
  int print_index1 = 0,
      print_index2,
      read_index,
      SIZE;

struct student data[print_index1];

SIZE = print_index1;


printf("Students with GPA's greater than or equal to 3.0, listed in alphabetical order:\n ");
for(print_index1 = 0; print_index1 < SIZE; print_index1++)
   {

            for(print_index2 = print_index1 + 1; print_index2 < SIZE; print_index2++)
               {
                  if(strcmp(data[print_index1].names, data[print_index2].names) > 0)
                    {
                      strcpy(data[print_index1].temp_name, data[print_index2].names);
                      strcpy(data[print_index2].names, data[print_index1].names);
                      strcpy(data[print_index1].names, data[print_index1].temp_name);

                    }
                }

      printf("%s\n", data[print_index1].names);
    }

  return 0;
}
#包括
#包括
#包括
结构学生{
字符临时名称[50];
字符名称[50];
};
int main()
{
int read_index=0;
浮动支票;
结构学生数据[读取索引];
printf(“输入学生姓名和GPA:\n”);
scanf(“%s%f\n”,数据[读取索引]。临时名称,&检查gpa);
read_index++;
而(scanf(“%s%f\n”,数据[read\u index].temp\u name,&check\u gpa)!=EOF)
{
如果(检查\u gpa>=3.0)
{
strcpy(数据[读取索引]。名称,数据[读取索引]。临时索引名称);
read_index++;
}
}
返回0;
}
管道文件链接pre和sort文件,以便将pre中的数据发送到sort

这是pipe.c文件:

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

struct student{
  char temp_name[50];
  char names[50];
};

int main()
{
  int read_index = 0;
  float check_gpa;

  struct student data[read_index];

  printf("Enter student name and GPA: \n");
  scanf("%s %f\n", data[read_index].temp_name, &check_gpa);
  read_index++;

  while(scanf("%s %f\n", data[read_index].temp_name, &check_gpa) != EOF)
       {
         if (check_gpa >= 3.0)
            {
              strcpy(data[read_index].names, data[read_index].temp_name);
              read_index++;
            }
       }

  return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<string.h>
#include<sys/wait.h>

int main()
{
  char *args[] = {"./sort", NULL};
  char *argv[] = {"./pre", NULL};

  int pipe_end[2];
  int pipe_id;
  pipe(pipe_end);

  if (pipe(pipe_end)==-1) // Check for pipe functionality
    {
        perror("Pipe Failed");
        return 1;
    }

  pipe_id = fork();

  if (pipe_id < 0) //Check for fork functionality
  {
    printf("Fork failed");
    return 1;
  }
  else if(pipe_id == 0)//Child
  {
    close(pipe_end[0]);
    dup(pipe_end[0]);
    execvp(argv[0], argv);
  }
  else //Parent
  {
    wait(NULL);
    close(pipe_end[1]);
    dup2(pipe_end[1], 0);
    close(pipe_end[0]);
    execvp(args[0], args);

  }

  return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<string.h>
#include<sys/wait.h>

struct student{
  char temp_name[50];
  char names[50];
};

int main()
{
  int print_index1 = 0,
      print_index2,
      read_index,
      SIZE;

struct student data[print_index1];

SIZE = print_index1;


printf("Students with GPA's greater than or equal to 3.0, listed in alphabetical order:\n ");
for(print_index1 = 0; print_index1 < SIZE; print_index1++)
   {

            for(print_index2 = print_index1 + 1; print_index2 < SIZE; print_index2++)
               {
                  if(strcmp(data[print_index1].names, data[print_index2].names) > 0)
                    {
                      strcpy(data[print_index1].temp_name, data[print_index2].names);
                      strcpy(data[print_index2].names, data[print_index1].names);
                      strcpy(data[print_index1].names, data[print_index1].temp_name);

                    }
                }

      printf("%s\n", data[print_index1].names);
    }

  return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
int main()
{
char*args[]={“/sort”,NULL};
char*argv[]={“/pre”,NULL};
int pipe_end[2];
管道内径;
管道(管端);
if(pipe(pipe_end)==-1)//检查管道功能
{
perror(“管道故障”);
返回1;
}
管道id=fork();
if(pipe_id<0)//检查fork功能
{
printf(“Fork失败”);
返回1;
}
如果(管道id==0)//Child,则为else
{
关闭(管道末端[0]);
dup(管道末端[0]);
execvp(argv[0],argv);
}
else//Parent
{
等待(空);
关闭(管道末端[1]);
dup2(管端[1],0);
关闭(管道末端[0]);
execvp(args[0],args);
}
返回0;
}
排序文件从结构数组中获取名称,并按字母顺序进行排序,然后将其打印到控制台。这就是问题开始的地方,因为当我运行管道文件时,我会输入名称和GPA,但当我通过按Ctrl+D启动EOF(这是必需的,不能更改)时,我希望字符串被发送到排序并按字母顺序显示,但这不会发生

这是sort.c文件:

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

struct student{
  char temp_name[50];
  char names[50];
};

int main()
{
  int read_index = 0;
  float check_gpa;

  struct student data[read_index];

  printf("Enter student name and GPA: \n");
  scanf("%s %f\n", data[read_index].temp_name, &check_gpa);
  read_index++;

  while(scanf("%s %f\n", data[read_index].temp_name, &check_gpa) != EOF)
       {
         if (check_gpa >= 3.0)
            {
              strcpy(data[read_index].names, data[read_index].temp_name);
              read_index++;
            }
       }

  return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<string.h>
#include<sys/wait.h>

int main()
{
  char *args[] = {"./sort", NULL};
  char *argv[] = {"./pre", NULL};

  int pipe_end[2];
  int pipe_id;
  pipe(pipe_end);

  if (pipe(pipe_end)==-1) // Check for pipe functionality
    {
        perror("Pipe Failed");
        return 1;
    }

  pipe_id = fork();

  if (pipe_id < 0) //Check for fork functionality
  {
    printf("Fork failed");
    return 1;
  }
  else if(pipe_id == 0)//Child
  {
    close(pipe_end[0]);
    dup(pipe_end[0]);
    execvp(argv[0], argv);
  }
  else //Parent
  {
    wait(NULL);
    close(pipe_end[1]);
    dup2(pipe_end[1], 0);
    close(pipe_end[0]);
    execvp(args[0], args);

  }

  return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<string.h>
#include<sys/wait.h>

struct student{
  char temp_name[50];
  char names[50];
};

int main()
{
  int print_index1 = 0,
      print_index2,
      read_index,
      SIZE;

struct student data[print_index1];

SIZE = print_index1;


printf("Students with GPA's greater than or equal to 3.0, listed in alphabetical order:\n ");
for(print_index1 = 0; print_index1 < SIZE; print_index1++)
   {

            for(print_index2 = print_index1 + 1; print_index2 < SIZE; print_index2++)
               {
                  if(strcmp(data[print_index1].names, data[print_index2].names) > 0)
                    {
                      strcpy(data[print_index1].temp_name, data[print_index2].names);
                      strcpy(data[print_index2].names, data[print_index1].names);
                      strcpy(data[print_index1].names, data[print_index1].temp_name);

                    }
                }

      printf("%s\n", data[print_index1].names);
    }

  return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
结构学生{
字符临时名称[50];
字符名称[50];
};
int main()
{
int print_index1=0,
打印索引2,
read_索引,
大小;
结构学生数据[print_index1];
尺寸=打印尺寸1;
printf(“GPA大于或等于3.0的学生,按字母顺序排列:\n”);
用于(print_index1=0;print_index10)
{
strcpy(数据[print\u index1]。临时名称,数据[print\u index2]。名称);
strcpy(数据[print\u index2]。名称,数据[print\u index1]。名称);
strcpy(数据[print\u index1]。名称,数据[print\u index1]。临时名称);
}
}
printf(“%s\n”,数据[print\u index1]。名称);
}
返回0;
}
我用用户输入单独测试了这两个文件,它们都正常工作。但是出现了一个新问题,如果您查看排序文件并注意到我有一个while循环,它接受了一个for循环条件,我认为这是合理的>它在一周前工作,但现在没有(除非是侥幸)。但这就是我的困境,我似乎无法将用户输入从“pre.c”转到“sort.c”,我真的非常感谢您的帮助。
我还认为“sort.c”文件中的while循环会导致打印名称时出现问题。

“……每次计算时,它的值应大于零……”
(参考
读取索引
),您的则不会。您试图使用
struct student数据[read_index]声明大小为零的
struct student的VLA。那是行不通的。相反,选择一个合理的最大值,例如
struct student data[128]
(或提示用户首先输入最大学生人数)您可能还有其他问题——这是我遇到的第一个主要问题。@DavidC.Rankin如果我自己运行“sort.c”,声明就起作用了。你认为这是管道出现问题的原因吗?是的,你用
struct student data[read_index]调用未定义的行为数据
并增加
读取索引++未定义的行为意味着任何事情都可能发生——从看似正常运行到分段故障。我会确保您的存储是有效的,然后继续处理您的代码。请参阅,由于
pre.c
不打印它读取的数据,因此其他程序无法读取它不生成的数据也就不足为奇了。请注意,如果运行
pre.c
并将其标准输出连接到管道,则
pre.c
中的提示将被发送到管道中(并且在屏幕上不可见)。您可以将提示打印到
stderr
而不是
stdout