Thread: File Handling cant be handled....pls sumbody look into it

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    1

    Thumbs down File Handling cant be handled....pls sumbody look into it

    First I am new to C programming so pls dont laugh looking at my code I am trying to make a program in C using file handling just to store and do the calculations but my trainer says that no need to do this in C as it will require database concept to get the required results.....I kindda took it in a challenging mode just to see whether I can prepare the Simple form in C not using any database concept.....
    The code is to do certain tasks done at a shopping Mall but in a very small manner....again pls dont laugh.....

    I am giving the part of the code below wherein I am facing the problem for only 2 cases in switch -:

    Code:
    case 3:
               // Customer Pricing list of the products bought      
                tot=0;
                      do {
                          
                       printf("\n Enter the Product Code==> ");
                       scanf("%d",&code);
                      fp=fopen("C:/Product Details.txt","r");
                      ag=fopen("C:/Reprice listing.txt","r");
                               if(ag==NULL)
                                   printf("\n Objection.....No such file exists...please retry");
                                   
                               else 
                                    {
                                         for(i=0;i<n;i++)
                                         {
                                         if(q[i].newprice!=s[i].price)
                                         //fread(&q[i],sizeof(q[i]),1,ag);
                                         tot=tot+q[i].newprice;
                                         } 
                                    }
                                    if(fp==NULL)
                                    printf("\n Objection.....No such file exists...please retry");
                                    else 
                                    {   
                                           for(i=0;i<n;i++)  
                                           {
                                             if(code==s[i].prcde)
                                             tot=tot+s[i].price;              
                                           }   
                                        printf("\n Do you like to continue ? Press Y for yes==> ");
                                        fflush(stdin);
                                        scanf("%c",&vote);         
                                    }  
                            }while(vote=='Y' || vote=='y');
                             fclose(fp);        
                               printf("\n The total price for the customer is = INR %f",tot);
                               fflush(stdin);
                           break;
    
    
                 case 4:  
    
           // Below code is for re-pricing of the products price list                      
                          printf("\n Enter the product code for repricing==> ");
                          scanf("%d",&recno);
                          fp=fopen("C:/Product Details.txt","r");
                          ag=fopen("C:/Reprice listing.txt","w");
                          if(fp==NULL)
                             printf("\n Objection.....No such file exists...please retry");
                              else
                                   {
                                      fread(&s[i],sizeof(s[i]),1,fp);
                                        for(i=0;i<n;i++)
                                         {
                                          if(recno==s[i].prcde)
                                            {
                                              if(ag==NULL)
                                          printf("\n Objection.....No such file exists...please retry");
                                                 else
                                                  {
                                                    printf("\n Enter the new price = ");
                                                    scanf("%f",&q[i].newprice);
                                                  }
                                             }
                                             q[i].prcde1=s[i].prcde; 
                                      fwrite(&q[i],sizeof(q[i]),1,ag);   
                                          }
                                   }
                          fclose(fp);
                          fclose(ag);
                                    break;
    Now the problem I am facing here is that in Case 4: the new price cant be overwritten on the previous Case 1 (not given here used for taking input from user) but is getting updated in a 2nd file wherein I am showing only the product code and the change of price effected when selected Case 5 to show the result. But it erases the other data of the products which is already entered when Case 5: is selected. Case 2(not given here) will show all the fileds like product code,name and price. Now whne Case 3: is selected for pricing of the products after price change done in Case 4: it retains the old price and ADDS up with the new price and then shows the result..........I am like at total loss and need sumone who can show me the way where I can go to fix the prob....pls help thanks

  2. #2
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Code:
    ag=fopen("C:/Reprice listing.txt","w");
    this will create a new file each time it's called, so any data written to it before would be overwritten.

    Code:
    fopen(..., "r+");  /* open for read/write of an existing file */
    fopen(..., "w+"); /* create new file for read/write   */
    fopen(..., "a+");  /* open for append at end of file  */
    you might consider just opening the files in whatever mode you need once and use fseek.

    It's been a while since I played with C/C++, so I may be off, but I *think* with structured data, you may want to open the files in binary mode and not text.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Code:
    fflush(stdin);
    DON'T use fflush on an input stream. That results in undefined behavior.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM