scanf with strings

H

happy

I need to write a string field in a structure data type to a file using
fprintf,fscanf.

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



 
W

Walter Roberson

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

:scanf("%4d",&(*cust).customer_no);

scanf("%4d", &cust->customer_no);

But what are you doing saving the result directly into the cust
structure without having validated the input, and without
even checking the return value from scanf() to see whether you
got what you were looking for?
 
H

happy

I am talking about the customer_name field . How can I write to disk
file using scanf,fscanf?
 
B

Ben Pfaff

happy said:
I am talking about the customer_name field . How can I write to disk
file using scanf,fscanf?

scanf() and fscanf() cannot be used to write to disk files.
You can write a string to a disk file with fputs(), e.g.:
fputs("my string\n", my_file);
 
H

happy

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

Walter Roberson

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

What difficulty are you encountering with your current code?
How do the file contents you get out differ from your expected contents?
 
H

happy

I need to write the whole structure oncew in one statment using any
command . Please help me in that direction as :
fprintf(fp_setup,"%4d%2d%c\n",customer);
 
W

Walter Roberson

:I need to write the whole structure oncew in one statment using any
:command .

You cannot write a whole structure using any of the *print* or *scan*
routines -- you can only write pieces at a time, one piece per
additional argument to *printf().

If you want to write a whole structure at once, then you need
to use a routine that does not have any format specifiers. However,
be cautioned that if you write a whole structure at once, you are
also writing out any internal padding it might have [which might
have nearly any content unless you have taken special care],
so you will need to take that padding into account when you are
reading it back in.
 

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,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top