Thread: code help

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    2

    code help

    Code:
    
    
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<assert.h>
    struct consumer {
    int number;
    int n;
    };
    main () {
    int count,i,f1;
    count=0;
    char c;
    float charge,fee;
    struct consumer consumer,fileconsumer;
    consumer.n=-1;
    printf("give me the number of the charge \n");
    fflush(stdin);
    scanf("%f",&charge);
    FILE *fp;
    
    
    while(1)
    {
    f1=0;
    scanf("%s",&c);
    if(c=='0')
    break;
    
    
    printf("give me the number of the calling cellphone ");
    scanf("%d",&consumer.number);
    fp=fopen("consumer.txt","+ab");
    assert(fp==NULL);
    for(i=0;i<count;i++)
    {
    rewind(fp);
    fseek(fp,sizeof(struct consumer)*i,SEEK_SET);
    fread(&fileconsumer,sizeof(struct consumer),1,fp);
    if(consumer.number==fileconsumer.number)
    {
    fileconsumer.n++;
    rewind(fp);
    fseek(fp,sizeof(struct consumer)*i,SEEK_SET);
    fwrite(&fileconsumer,sizeof(struct consumer),1,fp);
    f1=1;
    break;
    }
    }
    if(!f1)
    {
    consumer.n++;
    rewind(fp);
    fseek(fp,sizeof(struct consumer)*count,SEEK_SET);
    fwrite(&consumer,sizeof(struct consumer),1,fp);
    fclose(fp);
    count++;
    }
    }
    fp=fopen("consumer.txt","+ab");
    assert(fp==NULL);
    for(i=0;i<count;i++)
    {
    fseek(fp,sizeof(struct consumer)*i,SEEK_SET);
    fread(&fileconsumer,sizeof(struct consumer),1,fp);
    if(fileconsumer.n==0)
    {
    rewind(fp);
    fseek(fp,sizeof(struct consumer)*count,SEEK_SET);
    fread(&fileconsumer,sizeof(struct consumer),1,fp);
    rewind(fp);
    fseek(fp,sizeof(struct consumer)*(count-i),SEEK_END);
    fwrite(&fileconsumer,sizeof(struct consumer),1,fp);
    count--;
    }
    }
    for(i=0;i<count;i++)
    {
    rewind(fp);
    fseek(fp,sizeof(struct consumer)*i,SEEK_SET);
    fread(&fileconsumer,sizeof(struct consumer)*i,1,fp);
    if(fileconsumer.n>=10)
    {
    fee=charge*fileconsumer.n;
    fee=charge*fileconsumer.n-charge*fileconsumer.n/10;
    printf("the charge for the number %d it is %f \n",fileconsumer.number,fee);
    }
    else
    {
    fee=charge*fileconsumer.n;
    printf("the charge for the number %d it is %f \n ",fileconsumer.number,fee);
    }
    fileconsumer.n=0;
    fseek(fp,sizeof(struct consumer)*i,SEEK_SET);
    fwrite(&fileconsumer,sizeof(struct consumer)*i,1,fp);
    }
    
    
    fclose(fp);
    }

    it is running but in the end it texts me wrong

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    If you can't be bothered to indent and space out your code properly and ask a proper question, why would anyone be bothered to help you?

    A thinking human would describe what his program is supposed to do and what it is currently doing. And "code help" is a useless title. It contains no information.

    fflush(stdin) is non-standard at best (technically "undefined behaviour").

    It is incorrect to use assert to test if the file has opened. assert is for debugging, whereas a file not opening is something that can happen in normal operation.

    In scanf, the %s format is for reading a string, not a single char.

    .txt is a misleading file suffix for a binary file.

    It seems odd that you're opening the file in append mode. "rb+" would seem more apt. Append mode causes all writes to occur at the end of the file. I also believe that it is non-standard to put the + at the beginning as you do in "+ab".

    The rewinds are pointless.

    The way you handle the variable count is problematic.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    1. Main always returns int
    2. Your FILE pointer should be assigned NULL before it's used if not assigned at declaration
    3. It's also a good idea to assign the struct member "number" something also before it's read by scanf. Then there is no risk of over writing a possible garbage value.
    4. Update your compiler to at least a C99 compliant one if possible. (code::blocks) is one of the best IDE's for C and has a variety of options for compilers.
    Double Helix STL

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    As already mentioned, pick an indent style and stick to it.

    Here, for your delectation, courtesy of indent -kr -nut -ts2 -i2 -l120 -cli0 foo.c
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<assert.h>
    
    struct consumer {
      int number;
      int n;
    };
    
    main()
    {
      int count, i, f1;
      count = 0;
      char c;
      float charge, fee;
      struct consumer consumer, fileconsumer;
      consumer.n = -1;
      printf("give me the number of the charge \n");
      fflush(stdin);
      scanf("%f", &charge);
      FILE *fp;
    
      while (1) {
        f1 = 0;
        scanf("%s", &c);
        if (c == '0')
          break;
        printf("give me the number of the calling cellphone ");
        scanf("%d", &consumer.number);
        fp = fopen("consumer.txt", "+ab");
        assert(fp == NULL);
        for (i = 0; i < count; i++) {
          rewind(fp);
          fseek(fp, sizeof(struct consumer) * i, SEEK_SET);
          fread(&fileconsumer, sizeof(struct consumer), 1, fp);
          if (consumer.number == fileconsumer.number) {
            fileconsumer.n++;
            rewind(fp);
            fseek(fp, sizeof(struct consumer) * i, SEEK_SET);
            fwrite(&fileconsumer, sizeof(struct consumer), 1, fp);
            f1 = 1;
            break;
          }
        }
        if (!f1) {
          consumer.n++;
          rewind(fp);
          fseek(fp, sizeof(struct consumer) * count, SEEK_SET);
          fwrite(&consumer, sizeof(struct consumer), 1, fp);
          fclose(fp);
          count++;
        }
      }
    
      fp = fopen("consumer.txt", "+ab");
      assert(fp == NULL);
      for (i = 0; i < count; i++) {
        fseek(fp, sizeof(struct consumer) * i, SEEK_SET);
        fread(&fileconsumer, sizeof(struct consumer), 1, fp);
        if (fileconsumer.n == 0) {
          rewind(fp);
          fseek(fp, sizeof(struct consumer) * count, SEEK_SET);
          fread(&fileconsumer, sizeof(struct consumer), 1, fp);
          rewind(fp);
          fseek(fp, sizeof(struct consumer) * (count - i), SEEK_END);
          fwrite(&fileconsumer, sizeof(struct consumer), 1, fp);
          count--;
        }
      }
      
      for (i = 0; i < count; i++) {
        rewind(fp);
        fseek(fp, sizeof(struct consumer) * i, SEEK_SET);
        fread(&fileconsumer, sizeof(struct consumer) * i, 1, fp);
        if (fileconsumer.n >= 10) {
          fee = charge * fileconsumer.n;
          fee = charge * fileconsumer.n - charge * fileconsumer.n / 10;
          printf("the charge for the number %d it is %f \n", fileconsumer.number, fee);
        } else {
          fee = charge * fileconsumer.n;
          printf("the charge for the number %d it is %f \n ", fileconsumer.number, fee);
        }
        fileconsumer.n = 0;
        fseek(fp, sizeof(struct consumer) * i, SEEK_SET);
        fwrite(&fileconsumer, sizeof(struct consumer) * i, 1, fp);
      }
    
      fclose(fp);
    }
    Regarding the fopen mode:
    Quote Originally Posted by C99
    The argument mode points to a string. If the string is one of the following, the file is
    open in the indicated mode. Otherwise, the behavior is undefined. 214)
    r
    open text file for reading
    w
    truncate to zero length or create text file for writing
    a
    append; open or create text file for writing at end-of-file
    rb
    open binary file for reading
    wb
    truncate to zero length or create binary file for writing
    ab
    append; open or create binary file for writing at end-of-file
    r+
    open text file for update (reading and writing)
    w+
    truncate to zero length or create text file for update
    a+
    append; open or create text file for update, writing at end-of-file
    r+b or rb+ open binary file for update (reading and writing)
    w+b or wb+ truncate to zero length or create binary file for update
    a+b or ab+ append; open or create binary file for update, writing at end-of-file
    Regarding your use of 'a' mode.
    Quote Originally Posted by c99
    Opening a file with append mode (’a’ as the first character in the mode argument)
    causes all subsequent writes to the file to be forced to the then current end-of-file,
    regardless of intervening calls to the fseek function. In some implementations, opening
    a binary file with append mode (’b’ as the second or third character in the above list of
    mode argument values) may initially position the file position indicator for the stream
    beyond the last data written, because of null character padding.
    This nullifies your attempt to try and update records in place using fseek calls.

    > if (!f1)
    If a few lines earlier, you did f1=1, then there is no call to fclose() in this iteration of the while(1) loop. As a result, you end up with multiple open file handles, with no way to close them.

    > if (fileconsumer.n == 0)
    Perhaps you could explain in words what you think this code is doing.
    You seem to be trying to overwrite a 'deleted' record with a record from the end of the file. But you don't seem to do these things
    - actually delete the last record (which you now have a copy of)
    - actually check whether the moved record is one that should have been deleted anyway.

    Further, I think your fseek offset to SEEK_END should be negative, not positive.

    In general, your variable names could be a lot better. Names like 'count' and 'n' are far to general to convey any useful meaning.

    I would also suggest you remove the code which tries to compact the file until you have something basic working.
    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
    Jun 2015
    Posts
    1,640
    And the final piece of information:
    de lec ta tion
    noun (formal humorous)
    pleasure and delight
    from L. delectare 'to charm'

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-19-2012, 01:58 PM
  2. Replies: 1
    Last Post: 03-10-2010, 11:28 AM
  3. Replies: 14
    Last Post: 04-01-2008, 02:23 AM
  4. producing c/c++ code from flowcharts,pseudo code , algorithims
    By rohit83.ken in forum C++ Programming
    Replies: 3
    Last Post: 02-20-2008, 07:09 AM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

Tags for this Thread