奇怪的文件读取错误

时间:2015-11-03 16:04:43

标签: c multithreading pipe fopen fgets

此程序使用管道在进程之间传递消息。我用。编译 gcc -Wall -o p2 p2.c INIT.DAT LOG.DAT TRANS1 TRANS2
编译后我一直收到这个错误:

  

/usr/bin/ld:INIT.DAT:文件格式无法识别;作为链接器脚本处理
  /usr/bin/ld:INIT.DAT:1:语法错误

使用此源代码:

#include "my.h"

int main(int argc, char **argv)
{

   int pipe1[2], pipe2[2], pipe3[2];
   int pid[3];
   int p;
   int len, i, index, check, oneOrTwo;
   char val[10];
   char buff[MAXLINE];
   char strLog[MAXLINE];
   char* token;
   FILE *log, *trans1, *trans2, *init;
   //Create pipes
   p = pipe(pipe1);
   if(p < 0)
   {

      printf("pipe error");
      exit(0);

   }
   else
      printf("successful pipe1 = %d\n", p);

   p = pipe(pipe2);
   if(p < 0)
   {

      printf("pipe error");
      exit(0);

   }
   else
      printf("successful pipe2 = %d\n", p);

   p = pipe(pipe3);
   if(p < 0)
   {

      printf("pipe error");
      exit(0);

   }
   else
      printf("successful pipe3 = %d\n", p);

   //Store Manager
   pid[0] = fork();
   if(pid[0] == 0)
   {
      //Close unused channels
      if(close(pipe1[1]) == -1)
         printf("close error closing pipe1[1]\n");
      if(close(pipe2[0]) == -1)
         printf("close error closing pipe2[0]\n");
      if(close(pipe3[0]) == -1)
         printf("close error closing pipe3[0]\n");
      //open INI.DAT
      init = fopen(argv[1], "r");
      if(init == NULL)
      {

         printf("INIT.DAT failed to open");
         exit(0);

      }

      len = 0;
      //Gets TABLE size
      while(fgets(buff, MAXLINE, init) != NULL)
         len++;

      struct TABLE table[len];

      i = 0;
      init = fopen(argv[1], "r");
      //Fills table
      while(fgets(buff, MAXLINE, init) != NULL)
      {

         //id
         token = strtok(buff, " ");
         strcpy(table[i].id, token);
         //value
         token = strtok(NULL, " ");
         table[i].value = atoi(token);

         i++;

      }
      //Opens LOG.DAT
      log = fopen(argv[2], "r+");
      if(log == NULL)
      {

         printf("LOG.DAT failed to open");
         exit(0);

      }
      //Send and receive messages
      while(1)
      {

         check = 0;
         //Quit when pipe is closed
         if(read(pipe1[0], buff, MAXLINE) == -1)
            break;

         strcpy(strLog, "Store Manager received message: ");
         strcat(strLog, buff);
         fwrite(strLog, 1, sizeof(strLog), log);
         //1 or 2
         token = strtok(buff, " ");
         oneOrTwo = atoi(token);
         //pid
         token = strtok(NULL, " ");
         //U
         token = strtok(NULL, " ");
         //Update
         if(strcmp(token, "U") == 0)
         {
            //id
            token = strtok(NULL, " ");

            for(i = 0; i < len; i++)
            {

               if(token == table[i].id)
               {

                  check = 1;
                  index = i;

                  //value
                  token = strtok(NULL, " ");
                  table[i].value += atoi(token);

               }

            }
            //Update failed
            if(check == 0)
            {

               strcpy(strLog, "Store Manager sent message: Error updating table");
               fwrite(strLog, 1, sizeof(strLog), log);

               if(oneOrTwo == 1)
                  write(pipe2[1], "Error updating table", MAXLINE);
               else
                  write(pipe3[1], "Error updating table", MAXLINE);

            }
            //Update succeeded
            else
            {

               strcpy(strLog, "Store Manager sent message: U 0 ");
               strcat(strLog, table[index].id);
               strcat(strLog, " ");
               sprintf(val, "%d", table[index].value);
               strcat(strLog, val);

               fwrite(strLog, 1, sizeof(strLog), log);

               if(oneOrTwo == 1)
                  write(pipe2[1], strLog, MAXLINE);
               else
                  write(pipe3[1], strLog, MAXLINE);

            }

         }
         //Read
         else
         {

            check = 0;

            for(i = 0; i < len; i++)
            {
               //Read succeeded
               if(token == table[i].id)
               {

                  check = 1;
                  strcpy(strLog, "Store Manager sent message: R ");
                  sprintf(val, "%d", oneOrTwo);
                  strcat(strLog, val);
                  strcat(strLog, " ");
                  strcat(strLog, table[i].id);
                  strcat(strLog, " 0");

                  fwrite(strLog, 1, sizeof(strLog), log);

                  if(oneOrTwo == 1)
                     write(pipe2[1], strLog, MAXLINE);
                  else
                     write(pipe3[1], strLog, MAXLINE);

               }

            }
            //Read failed
            if(check == 0)
            {

               strcpy(strLog, "Store Manager sent message: Error reading table");

               fwrite(strLog, 1, sizeof(strLog), log);

               if(oneOrTwo == 1)
                  write(pipe2[1], "Error reading table", MAXLINE);
               else
                  write(pipe3[1], "Error updareadingting table", MAXLINE);

            }

         }

      }
      //Close files and quit process
      fclose(init);
      fclose(log);
      exit(0);
   }
   //P1
   pid[1] = fork();
   if(pid[1] == 0)
   {
      //Close unused channels
      if(close(pipe1[0]) == -1)
         printf("close error closing pipe1[0]\n");
      if(close(pipe2[1]) == -1)
         printf("close error closing pipe2[1]\n");
      //Open LOG.DAT
      log = fopen(argv[2], "r+");
      if(log == NULL)
      {

         printf("LOG.DAT failed to open");
         exit(0);

      }
      //Open TRANS1
      trans1 = fopen(argv[3], "r");
      if(log == NULL)
      {

         printf("LOG.DAT failed to open");
         exit(0);

      }
      //Send and receive messages
      while(fgets(buff, MAXLINE, trans1) != NULL)
      {

         strcpy(strLog, "Process 1 sent message: ");
         strcat(strLog, buff);

         fwrite(strLog, 1, sizeof(strLog), log);

         write(pipe1[1], buff, MAXLINE);

         read(pipe2[0], buff, MAXLINE);

         strcpy(strLog, "Process 1 received message: ");
         strcat(strLog, buff);

         fwrite(strLog, 1, sizeof(strLog), log);

         sleep(3);

      }
      //Close files and quit process
      fclose(trans1);
      fclose(log);
      exit(0);

   }
   //P2
   pid[2] = fork();
   if(pid[2] == 0)
   {
      //Close unused channels
      if(close(pipe1[0]) == -1)
         printf("close error closing pipe1[0]\n");
      if(close(pipe3[1]) == -1)
         printf("close error closing pipe3[1]\n");
      //Open LOG.DAT
      log = fopen(argv[2], "r+");
      if(log == NULL)
      {

         printf("LOG.DAT failed to open");
         exit(0);

      }
      //Open TRANS2
      trans2 = fopen(argv[4], "r");
      if(log == NULL)
      {

         printf("LOG.DAT failed to open");
         exit(0);

      }
      //Send and receive messages
      while(fgets(buff, MAXLINE, trans1) != NULL)
      {

         strcpy(strLog, "Process 1 sent message: ");
         strcat(strLog, buff);

         fwrite(strLog, 1, sizeof(strLog), log);

         write(pipe1[1], buff, MAXLINE);

         read(pipe3[0], buff, MAXLINE);

         strcpy(strLog, "Process 1 received message: ");
         strcat(strLog, buff);

         fwrite(strLog, 1, sizeof(strLog), log);

         sleep(3);

      }
      //Close files and quit process
      fclose(trans2);
      fclose(log);
      exit(0);

   }
   //Wait for processes to quit
   for(i = 0; i < 3; i ++)
   {

      if(waitpid(pid[i], NULL, 0) == -1)
      {

         printf("waitpid error");
         exit(0);

      }

   }

}

使用这些输入文件:

(NOT IN FILE) INIT.DAT
DISNEY 3  
INTEL 56

(NOT IN FILE) TRANS1
R XXXX  
R DISNEY

(NOT IN FILE) TRANS2
R INTEL  
U INTEL 45  
U DISNEY 21  
U DISNEY 21  
R DISNEY  
U DISNEY 21  
U DIY 21  

格式化

LOG.DAT is empty.

我不知道为什么我会收到此错误,因为即使我将INIT.DAT更改为INIT.txt或任何其他文件格式,它仍然无法正常工作。它必须是我的代码的问题,但我看不到它。

感谢。

0 个答案:

没有答案