writing string field to disk file

H

happy

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

Eric Sosman

happy said:
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);
 
H

happy

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

Walter Roberson

: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.]
 
C

CBFalconer

happy said:
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.
 
H

happy

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

Michael Mair

happy said:
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
 
C

CBFalconer

happy said:
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?
 

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

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,241
Latest member
Lisa1997

Latest Threads

Top