On 24 Oct 2006 23:13:59 -0700, "Rav" <rav.tech.cs@gmail.comwrote in
comp.lang.c:
Quote:
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_report,"%s",rec.name)' 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.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html