Need some help about file handling.

This is a discussion on Need some help about file handling. within the C Programming forums, part of the General Programming Boards category; My problem is the function edit and del this is the file must be edited and deleted. Partnumber --------partdescription -------------------partprice ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    11

    Need some help about file handling.

    My problem is the function edit and del
    this is the file must be edited and deleted.


    Partnumber --------partdescription -------------------partprice

    AOC5E02011 -----AOC 5E COLORED MONITOR--- -----4300.00
    BOKA118001 -----BOKA 118 SPEAKERS - 250 W ------5300.00
    RTEK123001 -----REALTEK MAINBOARD --------------5300.00

    but when the edit is done the output become like this

    AOC5E02011 d 0.00
    AOC d 5.00
    COLORED d 5.00
    MONITOR ds 4300.00
    BOKA118001 sdf 4300.00
    BOKA d 118.00
    SPEAKERS sd 118.00
    250 f 118.00
    W f 5300.00
    RTEK123001 ds 5300.00
    REALTEK sd 5300.00
    MAINBOARD f sa 5300.00



    Any idea how to fix this?



    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<ctype.h>
    #include<stdlib.h>
    #include<string.h>
    
    
    struct record
    {char des[26],num[10];
    float price;
    }trans;
    
    
    FILE *f1,*f2;
    
    
    
    
    void main()
    { void add(struct record);
    void edit(struct record);
    void del(struct record);
    
    
    char ans;
    clrscr();
    do{
    
    
         printf("[A]dd\n");
         printf("[C]hange\n");
         printf("[D]elete\n");
         printf("E[x]it\n");
         printf("Choose: ");
         ans=getche();
         printf("\n");
    switch(toupper(ans))
    {
    case 'A':add(trans);break;
    case 'C': edit(trans);break;
    case 'D': del(trans);break;
    default: printf("Invalid Input\n");break;
    }}
    while (toupper(ans)!='X');
    
    
    getch();
    }
    
    
    
    
    void add (struct record trans)
    {clrscr();
    char ans;
    f1=fopen("record.txt","a");
    do
    {
    printf("Part Number:\n");
    scanf("%s",trans.num);
    printf("Part Description:\n ");
    scanf("\n");
    gets(trans.des);
    printf("Price:\t");
    scanf("%f",&trans.price);
    fprintf(f1,"%s\t%s\t%0.2f\n",trans.num,trans.des,trans.price);
    printf("Another record? Y/N: ");
    ans=getche();
    }while(toupper(ans)=='Y');
    fclose(f1);
    }
    
    
    void edit(struct record trans)
    {char neym[20],ans,code;
    int g;
    printf("Enter number to be edited: ");
    scanf("%s",neym);
    f1=fopen("record.txt","r");
    f2=fopen("record1.txt","w");
    printf("\nChange code P or D:\t");
    code=getche();
    if(toupper(code)=='D')
    {
    while(fscanf(f1,"%s\t%s\t%f\n",trans.num,trans.des,&trans.price)!=EOF)
    {
    if(strcmpi(neym,trans.num)==0)
    {
    printf("Enter new description :");
    scanf("\n");
    gets(trans.des);
    strcpy(trans.num,neym);
    trans.price=trans.price;
    }
    fprintf(f2,"%s\t%s\t%0.2f\n",trans.num,trans.des,trans.price);
    }
    }
    if(toupper(code)=='P')
    {
    while(fscanf(f1,"%s\t%s\t%f\n",trans.num,trans.des,&trans.price)!=EOF)
    {
    if(strcmpi(neym,trans.num)==0)
    {
    printf("Enter new price :");
    scanf("%f",&trans.price);
    }
    fprintf(f2,"%s\t%s\t%0.2f\n",trans.num,trans.des,trans.price);
    }
    }
    
    
    remove("record.txt");
    fclose(f1);
    fclose(f2);
     rename("record1.txt","record.txt");
    }
    
    
    void del(struct record trans)
    {char neym[20];
    
    
    f1=fopen("record.txt","r");
    f2=fopen("record1.txt","w");
    printf("Enter part number:\t ");
    scanf("%s",neym);
    while(fscanf(f1,"%s\t%s\t%f\n",trans.num,trans.des,&trans.price)!=EOF)
    {if(strcmpi(neym,trans.num)!=0)
    fprintf(f2,"%s\t%s\t%0.2f\n",trans.num,trans.des,trans.price);
    }
    remove("record.txt");
    fclose(f1);
    fclose(f2);
    rename("record1.txt","record.txt");
    }
    Last edited by tgeandpudding; 03-24-2013 at 05:08 AM.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,673
    Read this -> SourceForge.net: Indentation - cpwiki
    Then post readable code.

    You might try closing a file before trying to remove it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    11
    Code:
    while(fscanf(f1,"%s\t%s\t%f\n",trans.num,trans.des,&trans.price)!=EOF)
    {
    if(strcmpi(neym,trans.num)==0)
    {
    printf("Enter new description :");
    scanf("\n");
    gets(trans.des);
    strcpy(trans.num,neym);
    trans.price=trans.price;
    }
    fprintf(f2,"%s\t%s\t%0.2f\n",trans.num,trans.des,trans.price);
    }
    



    i think this is my problem.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,440
    Yikes. There are so many things wrong with that code that it is no clear what it's doing right, let alone what it's doing wrong.

    1) The indentation is pitiful, making it difficult to understand what code is executed as a result of each conditional (if (...)) or in each loop.

    2) main() returns int, not void.

    3) <conio.h> and the various functions declared in it (clrscr(), getch(), getche(), .....) are not standard C.

    4) strcmpi() is not a standard function.

    5) gets() is one of the most dangerous methods you could have picked to read a line from the user. Yes, it is standard. But it is scheduled for removal from a future iteration of the C standard because it is so dangerous. scanf("%s", ...) is only marginally less dangerous.

    6) fscanf() reports errors in various ways. Returning EOF is only one of the ways.

    7) remove() rarely works if it is used to delete a file that is open for reading or writing.

    8) The format string you're using to read your file using fscanf() is not suitable for reading a file with content like you described. In particular, the %s format stops when it encounters whitespace.

    9) Similarly, the format string you're using to write the data out using fprintf() will not produce a file that looks like you describe.


    As to how to fix the above ..... you need to throw out the code and start again. Get one part of required functionality working (and tested) before moving on to the next. As opposed to just hacking out a heap of code that approximates what you're trying to do without testing as you go, followed by prayer, followed by begging for help in a forum.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIle handling!!!
    By rishabhsharma in forum C Programming
    Replies: 4
    Last Post: 12-06-2012, 12:02 PM
  2. File Handling -Read write and edit a file
    By aprop in forum C Programming
    Replies: 3
    Last Post: 02-27-2010, 01:01 PM
  3. File handling help
    By stevy123 in forum C Programming
    Replies: 7
    Last Post: 04-20-2007, 09:31 AM
  4. File Handling?!?
    By Twiggy in forum C Programming
    Replies: 1
    Last Post: 10-23-2001, 11:43 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21