<< Chapter < Page Chapter >> Page >

Assuming that we have entered John, Jean-Francois and Yoko as the 3 names, myfile.txt would contain:

myfile.txt
Name 1 [John ]
Name 2 [Jean-Franc]
Name 3 [Yoko ]

The fputc() function prototype is as follows.

int fputc(int character, FILE *fp);

The fputc() function writes a character to the file associated with fp. The character is written at the current position of the fp as indicated by the internal position indicator, which is then advanced one character

Return value: If there are no errors, the same character that has been written is returned.If an error occurs, EOF is returned and the error indicator is set.

Write the program that creates a file called alphabet.txt and writes ABCDEFGHIJKLMNOPQRSTUVWXYZ to it.

#include<stdio.h>int main () {FILE * fp; char c;fp = fopen ("alphabet.txt","w"); if (fp!=NULL){ for (c = 'A' ; c<= 'Z' ; c++) {fputc ((int) c , fp); }fclose (fp); }return 0; }
Got questions? Get instant answers now!

The fputs() function prototype is as follows.

int fputs(char *str,FILE *fp);

The fputs() function writes the string pointed to by str to the file associated with fp.

Return value: On success, a non-negative value is returned. On error, the function returns EOF. The null that terminates str is not written and it does not automatically append a carriage return/linefeed sequence.

Write the program allows to append a line to a file called myfile.txt each time it is run.

#include<stdio.h>int main () {FILE * fp; char name [50]; puts ("Please, enter a name: ");gets (name); fp = fopen ("myfile.txt","a");fputs (name,fp); fclose (fp);return 0; }
Got questions? Get instant answers now!

Read data from text files

When reading data from text files, C provides three functions: fscanf() , fgetc() , fgets() .

The fscanf() function prototype is as follows.

int fscanf(FILE *fp, char *format, ...);

This function reads data from the file specified by file pointer fp and stores them according to the parameter format into the locations pointed by the additional arguments. The additional arguments should point to already allocated objects of the type specified by their corresponding format tag within the format string.

Return value: On success, the function returns the number of items successfully read. This count can match the expected number of readings or be less -even zero- in the case of a matching failure. In the case of an input failure before any data could be successfully read, EOF is returned.

Read an integer number and a character from file associated with a file pointer fp and stores them to two variables a and c.

fscanf(fp, "%d %c",&a,&c);
Got questions? Get instant answers now!
#include<stdio.h>int main () {char str [80];float f; FILE * fp;fp = fopen ("myfile.txt","w+"); fprintf (fp, "%f %s", 3.1416, "PI");rewind (fp); fscanf (fp, "%f",&f); fscanf (fp, "%s", str);fclose (fp); printf ("I have read: %f and %s \n",f,str);return 0; }
Got questions? Get instant answers now!

This sample code creates a file called myfile.txt and writes a float number and a string to it. Then, the stream is rewinded and both values are read with fscanf. It finally produces an output similar to:

I have read: 3.141600 and PI

Questions & Answers

what is inflation
Sama Reply
inflation is the general price increase of goods and services in an economy.
tesfie
Inflation is the persistent rise in the general price level
T-Max
inflation is characterized by increase in the general price of goods and services. when there is too much money in circulation. increase in demand of goods pursuing fewer goods. when purchasing power of money decreases .
Ejikeme
inflation is the persistent rise general price level
Habeeb
inflation is the persistent increase in price
Machall
different between demand and quantity demand
Farhan Reply
No difference
MansoorAfghan
demand is the overall demand for it
MansoorAfghan
actually theres no difference
MansoorAfghan
quantity demanded is used in Equilibrium of d and s
MansoorAfghan
for evrything else u use deman
MansoorAfghan
how to calculate inflation
Richard Reply
Explain the factors that have led to high quantity demanded
Ogwang Reply
price of the product increase of price substitute product as people shift to cheap one
Black
what are the methods used by trade union to increase wages of their members?
Black Reply
strike
Pearl
the size of the commodity
Mensah
increase demand of labour decrease supply of labour
Black
I do support your answer Jackel.
keshav
but how do they do it?
Black
by increasing more labour and reduced the suppliers
Mensah
they can not increase labour, they increase demand of labour.
Black
how do they increase demand for labor?
Black
by analyzing the market equilibrium , cost reduction and cost control , savings in time .
yash
decreasing supply of labour are achieved through training and certification that require for you to employed, you must have certificate, also trade union encouraged government to restrict migration into the country causing shortage of labour supply. Note that the aim of union is to enhance life
Black
objective of union: better working conditions, liveable wage, protect member from unfair treatment which are done through negotiations betweens representative and management. known as collective bargaining.
Black
what is the nature of economics?
Tyscar Reply
economics is a social science since it seeks to solve social problem of scarcity
Jamal
main concerns is the decision individuals make on the allocation of scarce resources among the competing wants
Black
in the short run firm produce a positive as long as the price is larger than what?
yoel Reply
what is economic
Bah Reply
economics is the study of managing the resources in order to maximize the needs and satisfy the wants to a great extent in a regulated set-up..
Muhammad
One explanation for deviation when there is no impact on balance of trade
Shaneel
economic s is a social science that deals with human behavior as a relationship between ends and scarce means which has alternative uses
Derokiz
economic is a study of mankind in ordinary business of life
FIDELIS
economics it is the study of social science that deals with human behaviour as relationship between ends and scarce means which have alternative uses
salam
what is diminishing returns
Blessed Reply
what is the difference between calculus linear equation and derivative?
Bti
whats inferior goods?
jaamac Reply
Good having low quality , also known as giffin goods. When income increases people shift to better quality goods . Hence having a negative effect on inferior goods rather than positive relation ( ie when income increases demand increases but not in case of inferior goods ) example wheat and bajra .
yash
What do u understand by the word ENDS in professor Lord L C Robinson definition of Economics?
Kaba Reply
I understand that ENDS is the unlimited needs of human. But we have limited resources to achieve our unlimited needs/wants. Thank you.
Midhun
variable is a factor that can change
nyodb Reply
did you understand definition of variablE
nyodb
what is monopoly
Shahid Reply
A market situation where there is a single seller of a product for the buyers.
harmony
monopoly is when you have a product in the market and only one supplier got this product so he starts rising price and dominate the market, cause this product doesn't have a competitor
Anas
A market structure characterized by a single seller, selling a unique product in the market. In a monopoly market, the seller faces no competition, as he is the sole seller of goods with no close substitute.
Jale
This is a kind of market that only one seller dominate the market without no competition of a y product or seller.
Ojo
what is production possibility curve?
Lisa
a production possibility curve is a diagram that shows two goods produced in an economy in such a way that increase in the production of one good cannot be done without the decrease on the production of the other. there are efficient points, inefficient points and unattainable points on the PPC
The
production possibility curve or frontier is useful analytical tool for illustrating the concepts of scarcity, choice and opportunity costs.
wilflay
The International Year of Soils, 2015 (IYS 2015) was declared by the Sixty-eighth session of the United Nations General Assembly on December 20th, 2013 after recognizing December 5th as World Soil Day.The purpose of the IYS is to raise awarenessworldwide of the importance of soils for food security,
what
what is indifference curve
Evet
Monopoly is the explicit right given to business or entrepreneurship by the government to operate as the only entity in the economy
wilflay
what is economic system ?
wilflay
economical matter solve this system is know as economic system
what
indifference curve is a diagram that shows the combination of only 2 commodities in such a way that each point on the indifference curve gives the same level of satisfaction
The
all consumer is equal consumtion as definite all ..that type curve is indifference curve
what
what is variable?
Rajeev
economic structure of any area is ,city or country is called economic system
Jairam
what is the difference between rational and irrational choice
Amina
help
Amina
Rational choice theory is an economic principle that assumes that individuals always make prudent and logical decisions that provide them with the highest amount of personal utility. ... Most mainstream academic assumptions and theories are based on rational choice theory.
Anas
Irrationality is cognition, thinking, talking, or acting without inclusion of rationality. It is more specifically described as an action or opinion given through inadequate use of reason, or through emotional distress or cognitive deficiency.
Anas
help
Mensah
The meaning of inverse
Mensah
?
Anas
inverse what ? inverse means opposite .. like if ones going down other goes up so inverse relationship
MansoorAfghan
Definition of Inversely Related: Two variables are inversely related when an increase in one variable causes a reduction in the other variable. For example, when the price of a good increases, its quantity demanded decreases.
Anas
what is opotunity cost
salam
is an alternative forgone after the best choice have been selected.for example when you have cocacola and pepsi and you choose cocacola oportunity cost will be pepsi
Amin
explain the meaning of price cealing and price floor..?
Amin
Opportunity cost is the cost express in terms of forgone, alternatives after a choice have been made.
cyril
A price ceiling is the maximum price a seller can legally charge a buyer for a good or service. Consumers must not pay a price above the pries ceiling
Ntui
A price floor is that is imposed to protect consumers it can not be above the equilibrium price. It's the lowest price that producers must accept from a sales
Ntui
A price ceiling is a price control mechanism issued by the government to protect the consumers from high-priced commodities. It goes to set the highest amount a seller can charge per unit quantity of his product. N.B: This price mechanism isn't binding if equilibrium exists.
harmony
A price floor is a price control mechanism that seeks to protect the sellers by setting the lowest amount a seller can receive for his products. In this case, a seller will not sell his products for any amount less than the price floor amount.
harmony
It is argued that under optimization, since there is the second-order sufficient condition, the first-order condition is not necessary. Discuss.
John Reply
It is argued that in optimization the first part of second order condition appears opposite to their interpretation. Explain why you think otherwise.
John
Would you expect the kinked demand curve to be more extreme (like a right angle) or less extreme (like a normal demand curve) if each firm in the cartel produces a near-identical product like OPEC and petroleum? What if each firm produces a somewhat different product? Explain your reasoning.
Shauna Reply
we need to understand the things that manufacturer industies need to overcome the change of price, including all the all factors
Franck
hi
Jale
I expect the demand curve of such a case to be less extreme almost a horizobtal line.
tesfie
hi
Hashim
Hi
Prajwal
hi to
Sarparah
hello
Kaba
in a comparison of the stages of meiosis to the stage of mitosis, which stages are unique to meiosis and which stages have the same event in botg meiosis and mitosis
Leah Reply
Researchers demonstrated that the hippocampus functions in memory processing by creating lesions in the hippocampi of rats, which resulted in ________.
Mapo Reply
The formulation of new memories is sometimes called ________, and the process of bringing up old memories is called ________.
Mapo Reply
Got questions? Join the online conversation and get instant answers!
QuizOver.com Reply

Get the best Algebra and trigonometry course in your pocket!





Source:  OpenStax, Introduction to computer science. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10776/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Introduction to computer science' conversation and receive update notifications?

Ask