By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,402 Members | 1,127 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,402 IT Pros & Developers. It's quick & easy.

writing string field to disk file

P: n/a
I will reissue my question . How to form and write string field to a
disk file in one statement usinf fprintf?

my struct as below :

struct record
{
char customer_name[30];
int customer_no;
char product_type;
};

I need to write that sturcture in one statement using any write command
to disk file .

Nov 14 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a


happy wrote:
I will reissue my question . How to form and write string field to a
disk file in one statement usinf fprintf?

my struct as below :

struct record
{
char customer_name[30];
int customer_no;
char product_type;
};

I need to write that sturcture in one statement using any write command
to disk file .


Assuming `stream' is a `FILE*' that has been opened for
output to the desired file and that `rec' is an instance of
`struct record',

fprintf (stream, "Victim %s, Number %d, Product %c\n",
rec.customer_name, rec.customer_no, rec.product_type);

--
Er*********@sun.com

Nov 14 '05 #2

P: n/a
How will I scanf the
customer_name?-----scanf("%s",&(*cust).customer_name);-
That below is the full syntax.

#include <stdio.h>
#include <conio.h>

struct customer_record /*Defining a csutomer record*/
{
int customer_no;
char customer_name[20];
int no_of_weeks;
char tv_type;
};
main ()
{
/*-------1-open file for output*/
struct customer_record customer;
FILE *fp_setup;
if ((fp_setup=fopen("setup.txt","w")) == NULL)
{
printf("\nCan not open file 'setup.txt' for writing \n");
printf("Program is termainted");
exit(0);
}
clrscr();

/*-------2-while more customers----------separate function-------*/
do {
/*-------2-1-input customers record------separate function-------*/
customer_input(&customer);

/*-------2-2-write customer record to file*/
fprintf(fp_setup,"%4d%2d%c\n",customer);
} while (another_customer()=='y');
/*-------3-write end of file record to file */
fprintf(fp_setup,"%4d%s%2d%c\n",9999," ",99,' ');
/*-------4-close file*/
fclose(fp_setup);
return 0;
}
customer_input(cust)
/*---------------------*/
struct customer_record *cust;
{
int i=0;
printf("\nEnter customer number :");
scanf("%4d",&(*cust).customer_no);
printf("\nEnter customer name :");
scanf("\n");
scanf("%s",&(*cust).customer_name);
printf("\nEnter number of weeks rent due :");
scanf("%2d",&(*cust).no_of_weeks);
printf("Enter type of rental -c for colors TV");
printf("\n -b for black and white TV");
printf("\n -v for video");
printf("\n -o for other : ");
scanf("\n");
scanf("%c",&(*cust).tv_type);
return 0;
}

another_customer()
/*----------------*/
{
char another;
printf("\nAnother new_screen_customer for input (y or n) : ");
scanf("\n");
scanf("%c",&another);
return (another);
}

Nov 14 '05 #3

P: n/a
In article <11*********************@z14g2000cwz.googlegroups. com>,
happy <eh***********@yahoo.com> wrote:
:How will I scanf the
:customer_name?-----scanf("%s",&(*cust).customer_name);-

scanf("%s", cust->customer_name);

Naming an array without any offset is the same as taking the
address of its first element.

Note that if the customer name might have spaces in it [quite likely]
then you cannot read it with a single scanf %s format.
[Hint: the characters that are valid in customer names are only
a subset of the possible input characters.]

--
Cottleston, Cottleston, Cottleston pie.
A bird can't whistle and neither can I. -- Pooh
Nov 14 '05 #4

P: n/a
happy wrote:

I will reissue my question . How to form and write string field
to a disk file in one statement usinf fprintf?

my struct as below :

struct record
{
char customer_name[30];
int customer_no;
char product_type;
};

I need to write that sturcture in one statement using any write
command to disk file .


It won't do you too much good in the long run, but you might try:

struct record r;

.... code to create and open file f ....
.... code to populate the fields of r ....
fwrite(f, "%s,%d,%c\n",
r.customer_name, r.customer_no, r.product_type);

Please learn to indent your code, and with spaces, not tabs.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

Nov 14 '05 #5

P: n/a
I can not write string using fprintf as a part of a structure. AS below
:
fprintf(fp_setup,"%4d%2d%c\n",customer);

Nov 14 '05 #6

P: n/a
happy wrote:
I can not write string using fprintf as a part of a structure. AS below
:
fprintf(fp_setup,"%4d%2d%c\n",customer);


First: Please quote a minimum of context, so everybody knows
what you are replying to and what the problem is.

Second: You have 3 format specifiers vs. 1 passed argument.
This will lead to undefined behaviour. Even without me knowing
what your problem is, I can spot that.

Third: The original problem was that you wanted to write a structure
"in one go".
a) Use binary files and fwrite()
b) Use text files and fprintf() and write out the members of your
structure. If this is a recurring task, then write a function to
do it.
Read the FAQ on the advantages of the second approach.
http://www.eskimo.com/~scs/C-faq/top.html

Cheers
Michael
--
E-Mail: Mine is a gmx dot de address.

Nov 14 '05 #7

P: n/a
happy wrote:

I can not write string using fprintf as a part of a structure. AS below
:
fprintf(fp_setup,"%4d%2d%c\n",customer);


Is this cryptic message embodying illegal code and undefined
behaviour supposed to have some meaning to anyone, and if so, to
whom, and what meaning?

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.