Thread: having problems with updating records of one file to another file

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    4

    having problems with updating records of one file to another file

    i am having problems of updating record present in one file and writing them onto another file in the same record format. it will write the input from command line onto incomingr.dat but not collectr.dat.no errors .
    the code is as follows
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    //#include<conio.h>
    struct request
    {
    float percent_seq;
    char type_of_request[20];
    int request_size;
    };
    
    struct collectrequest
    {
    float percent_seq;
    char type_of_request[20];
    int request_size;
    };
    struct request r;
    int main()
    {
    FILE *fp;
    
    fp=fopen("INCOMINGR.DAT","w");
    if(fp==NULL)
    {
    printf("cannot open file");
    return 0;
    }
    
    printf("\n enter percent_seq,type_of_request and request_size");
    scanf("%f%s%d",&r.percent_seq,r.type_of_request,&r.request_size);
    fprintf(fp,"%f%s%d\n",r.percent_seq,r.type_of_request,r.request_size);
    fflush(stdin);
    insertcollectrequest(fp);
    
    
    fclose(fp);
    return 0;
    }
    int insertcollectrequest(FILE *fp)
    {
    FILE *fp2;
    struct collectrequest cr;
    fp2=fopen("COLLECTR.DAT","w+i");
    if(fp2==NULL)
    {
    printf("cannot open file");
    return 0;
    }
    while(fread(&r,sizeof(r),1,fp)==1)
    {
    fprintf(fp2,"%f%s%d\n",cr.percent_seq,cr.type_of_request,cr.request_size);
    }
    fclose(fp2);
    }

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    If you properly indent your code, it will be more readable, and more people will read it.

    "fflush(stdin)" results in undefined behavior according to the C standard, so until you get rid of that it cannot be eliminated as the potential source of your problem. I presume you are using it to get rid of characters left in the stdin stream buffer. There are better ways to deal with this that do not involve invoking undefined behavior.

    For example, write a function that reads a line of input into a local char buffer and extracts the data you want from that.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You are reading your fp file in records and trying to write out text... is there some reason you need to do that?

    Also you are reading record r but printing to file from record cr ... I'm thinking you should make up your mind

    Pluse as already noted ... Some from of structured text formatting ... horizontal indentation, vertical white space, etc. is going to make it a lot --a huge lot-- easier to find this kind of stuff in your code. Take a look at how other people indent and separate their code... there's a reason for it.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > fp2=fopen("COLLECTR.DAT","w+i");
    What does "i" mode do?
    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.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    4
    no it was printed by mistake the only w+ is the mode

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    > fp2=fopen("COLLECTR.DAT","w+i");
    What does "i" mode do?
    Create selfish code?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > no it was printed by mistake the only w+ is the mode
    How can you print something by mistake, from doing a copy/paste from your code editor to the forum?

    Unless you're telling us this isn't your real code, and that you're just wasting everyone's time
    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.

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    4
    the actual code is
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    //#include<conio.h>
    /*this structure is for taking a request from command line*/
    struct request
    {
    float percent_seq;
    char type_of_request[20];
    int request_size;
    };
    /*this struct is for having that incoming request appended onto it until a condition is reached*/
    struct collectrequest
    {
    float percent_seq;
    char type_of_request[20];
    int request_size;
    };
    struct request cr;
    int main()
    {
    FILE *fp;
    
    fp=fopen("INCOMINGR.DAT","w");
    if(fp==NULL)
    {
    printf("cannot open file");
    return 0;
    }
    
    printf("\n enter percent_seq,type_of_request and request_size");
    scanf("%f%s%d",&r.percent_seq,r.type_of_request,&r.request_size);
    fprintf(fp,"%f%s%d\n",r.percent_seq,r.type_of_request,r.request_size);
    fflush(stdin);
    insertcollectrequest(fp);
    
    
    fclose(fp);
    return 0;
    }
    
    int insertcollectrequest(FILE *fp)
    {
    FILE *fp2;
    struct collectrequest cr;
    fp2=fopen("COLLECTR.DAT","w+");
    if(fp2==NULL)
    {
    printf("cannot open file");
    return 0;
    }
     while((fscanf(fp,"%f%s%d",&r.percent_seq,r.type_of_request,&r.request_size))!=EOF)
    {
    fprintf(fp2,"%f%s%d\n",r.percent_seq,r.type_of_request,r.request_size);
    }
    fclose(fp2);
    }

  9. #9
    Registered User
    Join Date
    May 2011
    Posts
    4
    i want to read records from file in record format as well as write on to other file in record format.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You keep using a variable called "r", but you don't have one. How are you even compiling this code?

    You also don't prototype your function, which means that may only work by the grace of your compiler, plus you haven't gotten rid of fflush(stdin) yet.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Updating Records
    By nuddy in forum C Programming
    Replies: 18
    Last Post: 04-24-2010, 08:19 PM
  2. Reading records from File
    By hopeolicious in forum C++ Programming
    Replies: 5
    Last Post: 04-20-2005, 10:37 AM
  3. help, updating file via file pointer
    By godhand in forum C Programming
    Replies: 4
    Last Post: 09-24-2003, 12:00 AM
  4. Counting records in a file
    By mjpars in forum C Programming
    Replies: 3
    Last Post: 08-22-2003, 06:53 AM
  5. Writing records to a .dat file
    By Linette in forum C++ Programming
    Replies: 2
    Last Post: 01-24-2002, 02:31 AM