File handling with structures problem

R

Rav

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.
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

Rav said:
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
Possibly the rec.name contains a newline character.
fprintf(f_report,"''%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;
 
J

Jack Klein

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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top