954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Structure char field to a disk file

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


}

ehab_aziz2001
Newbie Poster
5 posts since Feb 2005
Reputation Points: 10
Solved Threads: 0
 

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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

can you give me a clue about the exact syntax ?

ehab_aziz2001
Newbie Poster
5 posts since Feb 2005
Reputation Points: 10
Solved Threads: 0
 

I mean by one statement as below :

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

ehab_aziz2001
Newbie Poster
5 posts since Feb 2005
Reputation Points: 10
Solved Threads: 0
 
fprintf(fp_setup, "%s %4d %2d %c\n",
  customer->customer_name,
  customer->customer_no,
  customer->no_of_weeks,
  customer->tv_type);
Siersan
Light Poster
45 posts since Jan 2005
Reputation Points: 12
Solved Threads: 2
 

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


}

ehab_aziz2001
Newbie Poster
5 posts since Feb 2005
Reputation Points: 10
Solved Threads: 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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You