title: Read text file. If same input, print "Existed"
body: <p>Here is the text file data : </p>
  
  <p>The value 78, 2, 98 are the productID. </p>
  
  <pre><code>78 MINERALWATER 123 345.00
  2 RAYBAN 2 450.00
  98 ADIDASWATCH 19 200.00
  </code></pre>
  
  <p>If user enter input which is the same as productID above. How can I print, "The product already exist!"</p>
  
  <p>Here is the part of code I already alter in <code>int addProduct()</code>.</p>
  
  <pre><code>    printf("Enter product ID : ");
      scanf(" %d", &amp;a.id);
  
      productID = a.id;
  
      fp = fopen("inventory.txt","r");
      while(fscanf(fp, "%d %s %d %f", &amp;a.id, a.name, &amp;a.quantity, &amp;a.price)==4)
          {
              ///The Output Came From Text File Must Be Same As User Instructed
              if(a.id == productID)
              {
              printf("The item already exist!");
              }
              else{
                   gotoxy(22,10);
      printf("Enter product name : ");
      scanf(" %s", a.name);
  
      gotoxy(22,12);
      printf("Enter product quantity : ");
      scanf(" %d", &amp;a.quantity);
  
      gotoxy(22,14);
      printf("Enter product price : ");
      scanf(" %f", &amp;a.price);
  
  
      gotoxy(22,17);
      ///Store @ Write Product Data Inside FP Text File
      fprintf(fp, "%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
  
      printf("Record saved!\n\n");
  
              }
          }
  </code></pre>
  
  <p>User input value 2 and store at <code>a.id</code>. Then, compiler scan the whole file, if <code>a.id = productID</code>, then print Item already exist. Else, continue adding new product. </p>
  
  <p>But, it fails. Compiler keep on adding data although user input value 2 and text file became : </p>
  
  <pre><code>78 MINERALWATER 123 345.00
  2 RAYBAN 2 450.00
  98 ADIDASWATCH 19 200.00
  2 NEWITEM 4 219.00
  </code></pre>
  
  <p>Here is my full code without altercation of looping if same productID was exist : point of interest is on <code>int addProduct()</code></p>
  
  <pre class="lang-c prettyprint-override"><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;windows.h&gt;
  #include &lt;ctype.h&gt;
  #include &lt;conio.h&gt;
  
  ///Function Prototype
  void gotoxy( int column, int line );
  int main();
  int addProduct();
  int displayProduct();
  int searchProduct();
  int reorderProduct();
  int updateProduct();
  
  ///Global Variables
  struct product
  {
      int quantity, reorder, i, id;
      char name[20];
      float price;
  };
  
  COORD coord = {0, 0};
  
  ///GotoXY Function
  void gotoxy (int x, int y)
  {
  coord.X = x; coord.Y = y;
  
  SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
  }
  
  ///Main Function
  int main()
  {
      int choice;
  
          ///GotoXY Output
          gotoxy(17,5);
          printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
  
          gotoxy(17,20);
          printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
  
          ///Menu Display
          gotoxy(22,8);
          printf("1. Add Product\n\n");
  
          gotoxy(22,10);
          printf("2. Display Product\n\n");
  
          gotoxy(22,12);
          printf("3. Search Product\n\n");
  
          gotoxy(22,14);
          printf("4. Reorder Level of Product\n\n");
  
          gotoxy(22,16);
          printf("5. Update Product\n\n");
  
          gotoxy(22,18);
          printf("6. Exit\n\n");
  
          ///Choice Input
          gotoxy(22,22);
          printf("Please Enter Your Choice : ");
          scanf(" %d", &amp;choice);
  
          ///Choice Using Switch Case
          switch(choice)
              {
              case 1 :    addProduct();
                          break;
  
              case 2 :    displayProduct();
                          break;
  
              case 3 :    searchProduct();
                          break;
  
              case 4 :    reorderProduct();
                          break;
  
              case 5 :    updateProduct();
                          break;
  
              case 6 :    break;
  
              default :   system("cls");
                          main();
  
              }
  return (0);
  }
  
  
  ///Add Product Function
  int addProduct()
  {
      ///File Pointer
      FILE * fp;
      int number;
      char checker;
      struct product a;
  
      ///Clear Screen On Command Prompt
      system("cls");
  
      ///FP Text File is Open. A = Append
      fp = fopen("inventory.txt","a+t");
  
          ///Do-While Loop For Product Input
          do
          {
          system("cls");
  
          ///GotoXY Output
          gotoxy(17,5);
          printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
  
          gotoxy(17,20);
          printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
  
          ///4 Input Of Add Product | Product ID | Product Name | Product Quantity | Product Price |
          gotoxy(22,8);
          printf("Enter product ID : ");
          scanf(" %d", &amp;a.id);
  
          gotoxy(22,10);
          printf("Enter product name : ");
          scanf(" %s", a.name);
  
          gotoxy(22,12);
          printf("Enter product quantity : ");
          scanf(" %d", &amp;a.quantity);
  
          gotoxy(22,14);
          printf("Enter product price : ");
          scanf(" %f", &amp;a.price);
  
  
          gotoxy(22,17);
          ///Store @ Write Product Data Inside FP Text File
          fprintf(fp, "%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
  
          printf("Record saved!\n\n");
  
          ///Close FP Text File
          fclose(fp);
  
  
          ///Choice Input For User Enter New Product Or Back To Main
          gotoxy(22,22);
          printf("Do you want to enter new product? Y / N : ");
  
          scanf(" %c", &amp;checker);
  
          ///Change To Uppercase, If User Enter Lowercase Character
          checker = toupper(checker);
  
          system("cls");
          }
          ///If Checker Input is Y @ Yes : Continue Loop Adding Product
          while(checker=='Y');
  
              ///If Checker Input Is N @ No : Back To Main
              if(checker == 'N')
                  {
                  main();
                  }
  
              else
              {
                  do
                  {
                  system("cls");
  
                  ///GotoXY Output
                  gotoxy(17,5);
                  printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
  
                  gotoxy(17,20);
                  printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
  
                  gotoxy(18,8);
                  printf("&gt;&gt;&gt; Wrong Input! Please Enter Y Or N Only! &lt;&lt;&lt;");
  
                  gotoxy(19,12);
                  printf("Do You Want To Enter New Product? Y / N : ");
                  scanf(" %c", &amp;checker);
  
                  ///Change To Uppercase, If User Enter Lowercase Character
                  checker = toupper(checker);
                  }
                  ///Keep On Looping If User Enter Other Than Y Or N.
                  while(checker != 'Y' &amp;&amp; checker != 'N');
  
                  ///If Input Y : Add Product
                  if(checker == 'Y')  {   addProduct();  }
  
                  ///If Input N : Back To Main
                  if(checker == 'N')  {   system("cls");
                                          main();
                                      }
  
              }
  
  return(0);
  }
  
  
  ///Display Product Function
  int displayProduct()
  {
      ///File Pointer
      FILE * fp;
  
      struct product a;
      system("cls");
  
      ///FP Text File is Open. R = Read Only
      fp = fopen("inventory.txt","r");
  
          ///GotoXY Output
          gotoxy(17,5);
          printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
  
          gotoxy(5,6);
          printf("======================================================================");
  
          ///Table Title
          gotoxy(5,7);
          printf("Product ID\t\t Product Name\t\t Quantity\t Unit Price\n");
  
          gotoxy(5,8);
          printf("======================================================================");
  
          ///Content Of Text File Is Read And Store Into Variables
          gotoxy(0,10);
          while(fscanf(fp, "%d %s %d %f", &amp;a.id, a.name, &amp;a.quantity, &amp;a.price)==4)
          {
          ///List Of Product From Text File Is Display On Command Prompt
          printf("\t%-10d\t %-12s\t\t %8d\t %8.2f\n\n", a.id, a.name, a.quantity, a.price);
          }
  
          ///Close FP Text File
          fclose(fp);
  
          ///GotoXY Output
          printf("\t\t \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
  
          printf("\n\n\t\t    &gt;&gt; Press any key to return to Main Menu &lt;&lt;");
  
      getch();
      system("cls");
  
      ///Back To Main Function
      main();
  
  return (0);
  }
  
  
  
  int searchProduct()
  {
      ///File Pointer
      FILE * fp;
  
      system("cls");
      struct product a;
      int productID;
  
      ///FP Text File is Open. R = Read Only
      fp = fopen("inventory.txt","r");
  
          ///GotoXY Output
          gotoxy(17,5);
          printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
  
          gotoxy(17,20);
          printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
  
          ///Input Product ID Want To Search
          gotoxy(22,8);
          printf("Please enter product ID : ");
          scanf(" %d", &amp;productID);
  
          gotoxy(5,10);
          printf("======================================================================");
  
          ///Table Title
          gotoxy(5,12);
          printf("Product ID\t\t Product Name\t\t Quantity\t Unit Price\n");
  
          gotoxy(5,14);
          printf("======================================================================");
  
  
              gotoxy(0,16);
              ///Content Of Text File Is Read And Store Into Variables
              while(fscanf(fp, "%d %s %d %f", &amp;a.id, a.name, &amp;a.quantity, &amp;a.price)==4)
              {
                  ///The Output Came From Text File Must Be Same As User Instructed
                  if(a.id == productID)
                  {
                  printf("\t%-10d\t %-12s\t\t %8d\t %8.2f\n\n", a.id, a.name, a.quantity, a.price);
                  }
              }
  
          ///Close FP Text File
          fclose(fp);
          gotoxy(17,20);
          printf("\n\n\t\t    &gt;&gt; Press any key to return to Main Menu &lt;&lt;");
  
      getch();
      system("cls");
      main();
  
  return(0);
  }
  
  
  int reorderProduct()
  {
      ///File Pointer
      FILE * fp;
  
      system("cls");
      struct product a;
      int productQuantity;
  
      ///FP Text File is Open. R = Read Only
      fp = fopen("inventory.txt","r");
  
          ///GotoXY Output
          gotoxy(17,5);
          printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
  
          ///Input Minimum Product Quantity
          gotoxy(22,8);
          printf("Please enter minimum reorder level : ");
          scanf(" %d", &amp;productQuantity);
  
          gotoxy(5,10);
          printf("======================================================================");
  
          ///Table Title
          gotoxy(5,12);
          printf("Product ID\t\t Product Name\t\t Quantity\t Unit Price\n");
  
          gotoxy(5,14);
          printf("======================================================================");
  
          gotoxy(0,16);
          ///Content Of Text File Is Read And Store Into Variables
          while(fscanf(fp, "%d %s %d %f", &amp;a.id, a.name, &amp;a.quantity, &amp;a.price)==4)
          {
              if(productQuantity &gt;= a.quantity)
              {
              ///The Output Came From Text File Must Be Greater Or Equal As User Instructed (Item Need To Be Reorder)
              printf("\t%-10d\t %-12s\t\t %8d\t %8.2f\n\n", a.id, a.name, a.quantity, a.price);
              }
          }
  
          ///Close FP Text File
          fclose(fp);
  
          ///GotoXY Output
          printf("\t\t \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
  
          printf("\n\n\t\t    &gt;&gt; Press any key to return to Main Menu &lt;&lt;");
  
      getch();
      system("cls");
      main();
  
  return (0);
  }
  
  
  
  int updateProduct()
  {
      ///File Pointer
      FILE *fp, *fp2;
  
      system("cls");
      struct product a;
      int productID;
  
      ///FP Text File is Open. R = Read Only
      fp = fopen("inventory.txt","r");
      rewind(fp);
  
      ///FP Text File is Open. R = Read Only
      fp2 = fopen("temporary.txt" ,"a");
  
          ///GotoXY Output
          gotoxy(17,5);
          printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
  
          gotoxy(17,20);
          printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
  
          ///Input Product ID To Be Update
          gotoxy(22,8);
          printf("Enter product ID : ");
          scanf(" %d", &amp;productID);
  
          ///Content Of Text File FP Is Read
          while (!feof(fp))
          {
          ///Content Is Store Into Variables
          fscanf(fp, "%d %s %d %f", &amp;a.id, a.name, &amp;a.quantity, &amp;a.price);
  
              if (feof(fp)) { continue; }
  
              if (a.id == productID) continue;
  
              fprintf(fp2, "%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
  
          }
  
          ///Close FP Text File
          fclose(fp);
          fclose(fp2);
  
          ///Inventory Text File Is Remove
          remove("inventory.txt");
          ///Temporary Text File Is Rename Into Inventory Text File
          rename("temporary.txt", "inventory.txt");
  
          ///FP Text File is Open. R = Append
          fp = fopen("inventory.txt","a+t");
  
          ///3 Input Of Add Product | Product Name | Product Quantity | Product Price |
          gotoxy(22,10);
          printf("Enter product name : ");
          scanf(" %s", a.name);
  
          gotoxy(22,12);
          printf("Enter product quantity : ");
          scanf(" %d", &amp;a.quantity);
  
          gotoxy(22,14);
          printf("Enter product price : ");
          scanf(" %f", &amp;a.price);
  
          gotoxy(22,17);
          ///Store @ Write Product Data Inside FP Text File
          fprintf(fp, "%d %s %d %f\n\n", productID, a.name, a.quantity, a.price);
          printf("Record saved!\n\n");
  
          ///Close FP Text File
          fclose(fp);
  
          printf("\n\n\t\t    &gt;&gt; Press any key to return to Main Menu &lt;&lt;");
  
      getch();
      system("cls");
      main();
  
  return(0);
  }
  </code></pre>