Jump to content

File processing help!!!!

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
russian777

russian777

    Newbie

  • Members
  • PipPip
  • 29 posts
So yeah , got my app like 95% working , just getting alot of problems with file processing. I got my programme to write and save to a file name "PRODUCTS2.txt" and it saves perfectly . But my application has a module/function which shud allow the user to enter the product id and the programme displays the characteristics of it. Here's what i have but it doesnt work , just displays "Product not found".


void searchFile()

{

     int choice;

     FILE *ptr;

     record p;

     char search[20];

     

     

     if((ptr=fopen("PRODUCTS2.txt","r"))==NULL)

     {

          printf("File cud not be opened\n");

     }

     else

     {

         

         printf("Enter name of product\n");

         scanf("%s",&search);

         

         while(!feof(ptr))

         {

               if(strcmp(p.name,search)==0)

               {

                     printf("%i%f%i",p.id,p.price,p.quantity);

                     fscanf(ptr,"%i%f%i",p.id,p.price,p.quantity);

               }

                              

               else

               {

                   printf("Product not found\n");

               }

         }

     }

}


And record is a datatype :

struct Record

{

		char id[10];

		char name[20];

	        float price;

		char category[15];

		int quantity;


};



If anyone can help me to generate a "delete" function which asks the user for the id and deletes it , i wud infinitly be grateful.

#2
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
How is data in the PRODUCTS2.txt file stored?

#3
russian777

russian777

    Newbie

  • Members
  • PipPip
  • 29 posts
This is my save module, using "ab" storage method.

void insertFile()

{

     record p;

     FILE *ptr;

     int choice;

     

     do

     {

       if((ptr=fopen("PRODUCTS2.txt","ab"))==NULL)

       {

          printf("File cannot be opened");

       }

       else

       {

          printf ("Enter product ID\n");

          scanf ("%s",&p.id);

          

          printf ("Enter Product name\n");

          scanf("%s",&p.name);

          

          printf ("Enter product price\n");

          scanf("%f",&p.price);

          

          printf ("Enter product category\n");

          scanf ("%s",&p.category);

          

          printf("Enter the quantity\n");

          scanf("%i",&p.quantity); 

          

          if(!feof(stdin))

          {

              fprintf(ptr,"%s %s %f %s %i\t\n",p.id,p.name,p.price,p.category,p.quantity);

              fclose(ptr);

          }

          

          printf("Enter 1 to enter another record\nEnter 9 to EXIT\n");

          scanf("%i",&choice);

       }

     }while(choice!=9);

}