I need to write a string field-char customer_name[20]- in a structure data type to a file using fprintf,fscanf,scanf.

I failed to use the below syntax. Please I need to not go outside
scanf,fprintf,printf .

/*  Book name          : The prodessional programmers guide to C
File name          : E:\programs\tc\iti01\ch09\main\01setupm.c
Program discription: file setuping -up -Version 01-ver01-W
Logic              :
1-open file for output
2-while more customers
2-1-input customers record
2-2-write customer record to file
3-write end of file record to file
4-close file
*/



#include <stdio.h>
#include <conio.h>
/*TV Rental file Set_up -Version 01
-Setting up a file containing sutomer information*/
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%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");
for (i=0;(*cust).customer_name != '\n' || (i<=20);i++)
scanf("%c",&(*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);



}

Recommended Answers

All 6 Replies

Why on God's green earth are you using K&R syntax?

As for printing strings with fprintf, use %s, but make sure that the string is terminated by a null character ('\0') or you'll have issues.

can you give me a clue about the exact syntax ?

I mean by one statement as below :

fprintf(fp_setup,"%4d%2d%c\n",customer);

Member Avatar for Siersan
fprintf(fp_setup, "%s %4d %2d %c\n",
  customer->customer_name,
  customer->customer_no,
  customer->no_of_weeks,
  customer->tv_type);

How will I read the string using scanf in that function :


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");
for (i=0;(*cust).customer_name != '\n' || (i<=20);i++)
scanf("%c",&(*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;


}

The way you're doing it is fine with a few minor changes:

int i;

for (i = 0; i < 19; i++) {
  scanf("%c", &cust->customer_name[i]);
  if (cust->customer_name[i] == '\n')
    break;
}
cust->customer_name[i] = '\0';

printf("|%s|\n", cust->customer_name);

This reads n - 1 characters at most, and copies a null character to the last open cell to make the array a string. This copy will overwrite a newline if detected. The printf call verifies that it works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.