File handling with structures problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rav

    #1

    File handling with structures problem

    Hi everyone, i need help on this one. i have declared a structure as

    struct record
    {
    int i;
    int j;
    char name[20];
    char place[20];
    };

    struct record rec;

    now, after creating multiple records of type 'rec' and assigining each
    member the value, i want to generate a text report file in which each
    record element of type 'rec' is displayed sequentially.

    like, if rec.i = 10, rec.j=20, rec.name = "ravs" and rec.place =
    "india" then the format of the record should be like,
    i j name place
    =============== =========
    10 20 ravs india

    i tried to do that with the 'fprintf' function (e.g.
    'fprintf(f_repo rt,"%s",rec.nam e)' where f_report is a pointer of type
    'FILE *' named 'report.txt') but i am not able to figure it out because
    after printing any string member of 'rec'(e.g. rec.name), the next
    member gets printed in next line, this is not what i want..i want all
    the members to be printed in the same line. i have tried to remove this
    but didn't succeed. plz help.

  • Nils O. Selåsdal

    #2
    Re: File handling with structures problem

    Rav wrote:
    Hi everyone, i need help on this one. i have declared a structure as
    >
    struct record
    {
    int i;
    int j;
    char name[20];
    char place[20];
    };
    >
    struct record rec;
    >
    now, after creating multiple records of type 'rec' and assigining each
    member the value, i want to generate a text report file in which each
    record element of type 'rec' is displayed sequentially.
    >
    like, if rec.i = 10, rec.j=20, rec.name = "ravs" and rec.place =
    "india" then the format of the record should be like,
    i j name place
    =============== =========
    10 20 ravs india
    >
    i tried to do that with the 'fprintf' function (e.g.
    'fprintf(f_repo rt,"%s",rec.nam e)' where f_report is a pointer of type
    Possibly the rec.name contains a newline character.
    fprintf(f_repor t,"''%s''",rec. name) can often be helpful to show blank
    characters.

    Get rid of that newline, e.g.
    rec.name[strlen(rec.name ) - 1 ] = 0; /*if you know it has a newline*/
    or perhaps even better(?)
    char *tmp = strchr(rec.name ,'\n');
    if(tmp)
    *tmp = 0;

    Comment

    • Jack Klein

      #3
      Re: File handling with structures problem

      On 24 Oct 2006 23:13:59 -0700, "Rav" <rav.tech.cs@gm ail.comwrote in
      comp.lang.c:
      Hi everyone, i need help on this one. i have declared a structure as
      >
      struct record
      {
      int i;
      int j;
      char name[20];
      char place[20];
      };
      >
      struct record rec;
      >
      now, after creating multiple records of type 'rec' and assigining each
      member the value, i want to generate a text report file in which each
      record element of type 'rec' is displayed sequentially.
      >
      like, if rec.i = 10, rec.j=20, rec.name = "ravs" and rec.place =
      "india" then the format of the record should be like,
      i j name place
      =============== =========
      10 20 ravs india
      >
      i tried to do that with the 'fprintf' function (e.g.
      'fprintf(f_repo rt,"%s",rec.nam e)' where f_report is a pointer of type
      'FILE *' named 'report.txt') but i am not able to figure it out because
      after printing any string member of 'rec'(e.g. rec.name), the next
      member gets printed in next line, this is not what i want..i want all
      the members to be printed in the same line. i have tried to remove this
      but didn't succeed. plz help.
      Almost certainly the problem is in the code that you didn't show us,
      that is in the code that assigns data to the members of the structure.

      If the fprintf() call you showed above is what your code is actually
      doing, fprintf() is not adding a newline after the string. So somehow
      when you are putting data into the char array in the structure, you
      are including a '\n'.

      If you are reading input with fgets(), you need to remove the '\r' at
      the end of the input line yourself, fgets() does not do it for you.

      You can use a function like this to remove newlines from the end of a
      string, if one is present. It requires that you include <string.h>.

      char *remove_newline (char *s)
      {
      if (s)
      {
      char *nlp = strrchr(s, '\r');
      if (nlp)
      {
      *nlp = '\0';
      }
      }
      return s;
      }

      --
      Jack Klein
      Home: http://JK-Technology.Com
      FAQs for
      comp.lang.c http://c-faq.com/
      comp.lang.c++ http://www.parashift.com/c++-faq-lite/
      alt.comp.lang.l earn.c-c++

      Comment

      Working...