473,471 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

File handling with structures problem

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.

Oct 25 '06 #1
2 1618
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_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;
Oct 25 '06 #2
On 24 Oct 2006 23:13:59 -0700, "Rav" <ra*********@gmail.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_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
Oct 26 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Limey Drink | last post by:
Hi all, Firstly :-) , is there any where I can search through archived newsgroup posts so I am not cluttering up the newsgroup with repeated queries ? And secondly :-), I know this has...
9
by: Hans-Joachim Widmaier | last post by:
Hi all. Handling files is an extremely frequent task in programming, so most programming languages have an abstraction of the basic files offered by the underlying operating system. This is...
1
by: Sean W. Quinn | last post by:
Hey folks, I have a question regarding file handling, and the preservation of class structure. I have a class (and I will post snippets of code later in the post) with both primitive data...
5
by: Chathu | last post by:
Hello everyone........... I have a problem on retriving a content of a binary file I wrote into. My program user structures, dynamic allocation of memory and files. I take the infomation into a...
7
by: John Dann | last post by:
I'm trying to read some binary data from a file created by another program. I know the binary file format but can't change or control the format. The binary data is organised such that it should...
3
by: nkrisraj | last post by:
Hi, I have a following structure: typedef struct { RateData rdr; int RateID; char RateBalance; } RateInfo;
1
by: Galen Somerville | last post by:
And yet another VB6 to VB2005 problem. All helpful suggestions appreciated. As you can see in the code below, my structures use fixed length strings and known array sizes. Consequently I can save...
14
by: chance | last post by:
Hello, I have a file on disk called TEMP.ZIP and I would like to somehow get this into a memory stream so I can eventually do this: row = dataStream.ToArray() However, I am not sure of the...
13
by: sachin | last post by:
Hi, Is it possible to do something like this: unsigned char arr = { #include "cFile.c" } I need that C source file cFile.c to compile and its binary output to include in array.
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.