473,387 Members | 1,771 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

scanf with strings

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


Nov 14 '05 #1
7 2668
In article <11**********************@g14g2000cwa.googlegroups .com>,
happy <eh***********@yahoo.com> wrote:
: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?
--
Rome was built one paycheck at a time. -- Walter Roberson
Nov 14 '05 #2
I am talking about the customer_name field . How can I write to disk
file using scanf,fscanf?

Nov 14 '05 #3
"happy" <eh***********@yahoo.com> writes:
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);
--
"We put [the best] Assembler programmers in a little glass case in the hallway
near the Exit sign. The sign on the case says, `In case of optimization
problem, break glass.' Meanwhile, the problem solvers are busy doing their
work in languages most appropriate to the job at hand." --Richard Riehle
Nov 14 '05 #4
I will reissue my question . How to form and write string field to a
disk file in one statement usinf fprintf?

Nov 14 '05 #5
In article <11**********************@l41g2000cwc.googlegroups .com>,
happy <eh***********@yahoo.com> wrote:
: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?

--
Live it up, rip it up, why so lazy?
Give it out, dish it out, let's go crazy, yeah!
-- Supertramp (The USENET Song)
Nov 14 '05 #6
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);

Nov 14 '05 #7
In article <11**********************@f14g2000cwb.googlegroups .com>,
happy <eh***********@yahoo.com> wrote:
: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.
--
millihamlet: the average coherency of prose created by a single monkey
typing randomly on a keyboard. Usenet postings may be rated in mHl.
-- Walter Roberson
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Steve Zimmerman | last post by:
This post is not intended as an argument to any other post, just some simple scanf experiments that I wanted to share. I found experiments 5 and 6 the most educational. Also, I thought...
11
by: | last post by:
Hi all...this is a great forum, In one of my posts, someone tell me that is more secure use input function with 'A field-width specifier'... So my question, which function i should use ?. ...
3
by: linguae | last post by:
Hello. In my C program, I have an array of character pointers. I'm trying to input character strings to each index of the character pointer array using scanf(), but when I run the program, I get...
11
by: QQ | last post by:
Hello I am running this code int main(void) { char A,B; printf("Please input A: \n"); scanf("%2s",A); printf("Please input B: \n"); scanf("%2s",B); printf("A is %s,B is %s\n",A,B); }
14
by: iwinux | last post by:
Hi. Before I use scanf(), I must malloc the memory for it, like this: //Start char * buffer; buffer = malloc(20); scanf("%s", &buffer); //End
8
by: john | last post by:
I'm trying to use scanf() to get a string that is terminated by a $sign: Reading a file line that has: account number, name (terminated by $sign) and six numbers: 000001 John Doe$ 4 5 6 7 8 9 ...
20
by: Xavoux | last post by:
Hello all... I can't remind which function to use for safe inputs... gets, fgets, scanf leads to buffer overflow... i compiled that code with gcc version 2.95.2, on windows 2000 char tmp0 =...
3
by: Tinku | last post by:
#include<stdio.h> main() { char line; scanf("%", line); printf("%s", line); } it will read and print the line but what is "%" in general we gives %s, %c .
13
by: AMD | last post by:
Hello, I often need to parse strings which contain a mix of characters, integers and floats, the C-language scanf function is very practical for this purpose. I've been looking for such a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.