C Files

CHARACTER I/O FUNCTIONS:
Character input functions read one character at a time from a text stream. Character output functions write one character at the time to a text stream.
These functions can be divided into two categories,
1. Terminal Character I/O
2. Terminal and File Character I/O
TERMINAL CHARACTER I/O
C declares a set of character input/output functions that can only be used with the standard streams: standard input (stdin), standard output (stdout).
Read a Character: getchar ()
This function is to read exactly one character from the keyboard; it reads the next character from the standard input stream.
Syntax for using getchar () function is as follows,

ch =getchar ();


Its return value is integer. Up on successful reading returns the ASCII value of character. If there is an error returns EOF.
Write a Character: putchar ()
This function provides for printing exactly one character to the Monitor.
Syntax for using putchar () function is as follows,

ch =getchar ();  /* input a character from keyboard*/
     putchar (ch);       /* display character on the Monitor */


Its return value is integer. Up on successful writing returns the ASCII value of character. If there is an error returns EOF.
Program:
#include <stdio.h>
Void  main( )
{
   int c;

   printf( "Enter a value :");
   c = getchar( );

   printf( "\nYou entered: ");
   putchar( c );
}

TERMINAL AND FILE CHARACTER I/O
The terminal character input/output functions are designed for convenience; we don’t need to specify the stream. Here, we can use a more general set of functions that can be used with both the standard streams and a file.
These functions require an argument that specifies the stream associated with a terminal device or a file.
When used with a terminal device, the streams are declared and opened by the system, the standard input stream (stdin) for the keyword and standard output stream (stdout) for the monitor.
When used with a file, we need to explicitly declare the stream, it is our responsibility to open the stream and associate with the file.
Read a Character: getc () and fgetc ()
The getc functions read the next character from the file stream, which can be a used-defined stream or stdin, and converts it in to an integer. This function has one argument which is the file pointer declared as FILE.
If the read detects an end of file, the function returns EOF, EOF is also returned if any error occurs. The functionality of getc/fgetc same.
Syntax for using getc/fgetc,
getc (fp);
fgetc (fp);


Example,
char ch;
ch = getc (stdin); /* input from keyboard */
ch =fgetc (fileptr); /* input from a file */
Write a Character: putc () and fputc ()
The putc functions write a character to the file stream specified which can be a user-defined stream, stdout, or stderr. The functionality of putc/ fputc same.
For fputc, the function takes two arguments. The first parameter is the character to be written and the second parameter is the file, The second parameter is the file pointer declared as FILE.
If the character is successfully written, the function returns it. If any error occurs, it returns EOF.
Syntax,
   
putc (char, *fp);
fputc (char, *fp);



Example,
char ch;
ch = getc (stdin); /* input from keyboard */
putc (ch, stdout);   /* output to the screen */
putc (ch, outfileptr); /*output to a file */
LINE I/O FUNCTIONS
Reading Strings: gets () and fgets ()
The gets and fgets function take a line (terminated by a new line) from the input stream and make a null-terminated string out of it. These are sometimes called line-to-string input function.
gets ()
gets function reads a string from terminal, reading is terminated by new line.
 The newline (\n) indicates the end of the string, it is replaced by null (\0) character in the memory by the function gets () as shown in Figure 6.5.
The source of data for the gets function is standard input.
gets function takes only one parameter, the name of the string, its syntax is as follows,

gets (string_name);



Figure: Working of gets/fgets functions
fgets ()
fgets function reads a string from file or keyboard.
It reads characters from the specified file until a new line character has been read or until n-1 characters has been read, whichever occurs first. The function automatically places null character at the end as shown in Figure 6.5.
The source of data for fgets can be a file or standard input.
The general format,
fgets (string, length, fp);


First argument is name of the string in which the read data from the file is to be placed. Second argument is the length of the string to be read and last argument is the file pointer.
fgets also reads data from terminal. Below example illustrates this,

fgets (str, 10, stdin);

Writing Strings: puts () and fputs ()
puts/fputs functions take a null-terminated string from memory and write it to a file or the monitor. These are sometimes called string-to-line output functions.
puts ()
puts function writes a string to terminal, writing is terminated by null character. the null character is replaced with a new line inputs as shown in Figure 6.6.
puts function takes only one parameter, the name of the string, its syntax is as follows,

puts (string_name);

fputs ()
fputs function writes a string to a file or monitor.
Characters stored in the string are written to the file identified by fileptr until the null character is reached. The null character is not written to the file as shown in Figure 6.6.
The general format forms are

fputs (string, fp);


First argument is address of the string in which the read data is to be placed. Second argument is the file pointer.



Figure : Working of puts/fputs Functions

FORMATED CONSOLE I/O
We have already familiar with two formatting functions scanf and printf. The functions printf() and scanf() perform formatted output and input –i.e.., they can read and write the data in various formats that are under  control.
The printf() function writes data to the console. The scanf() function reads data from the keyboard.These two functions can be used only with the keyboard and monitor.
The C library defines two more general functions, fscanf and fprintf, that can be used with any txt stream.
Formatted Output with printf ()
This function provides for formatted output to the screen.  The syntax is:
printf (“format string”, var1, var2 …);
The “format string” includes a listing of the data types of the variables to be output and, optionally, some text and control character(s).
Example:
float a=10.5; int b=15;
printf (“You entered %f and %d \n”, a, b);
Format Conversion Specifiers
d -- displays a decimal (base 10) integer
l  -- used with other Specifiers to indicate a "long"
e -- displays a floating point value in exponential notation
f -- displays a floating point value
g -- displays a number in either "e" or "f" format
c -- displays a single character
s -- displays a string of characters
Formatted Input with scanf ()
This function provides for formatted input from the keyboard. The syntax is:
scanf ( “format string” , &var1, &var2, …) ;
The “format string” is a listing of the data types of the variables to be input and the & in front of each variable name tells the system Where to store the value that is input.
It provides the address for the variable.
Example:
float a; int b;
scanf (“%f %d”, &a, &b);
STREAM
  1.  A stream is a general name given to a flow of data.
  2.  All input and output is performed with streams.
  3.  A "stream" is a sequence of characters organized into lines.
  4.  Each line consists of zero or more characters and ends with the "newline" character.
  5.  ANSI C standards specify that the system must support lines that are at least 254 characters in length (including the new line character).
  6.  A stream can be associated with a physical device, terminal, or with the file stored in memory. The following Figure 6.1 illustrates the data flow between external device(c Program), buffer and file.


STREAM FILE PROCESSING
A file exists as an independent entity with a name known to the O.S. A stream is an entity created by the program. To use a file in our program, we must associate the program’s stream name with the file name.
In general, there are four steps to processing a file.
1. Create a stream
2. Open a file
3. Process the file (read or write data)
4. Close file
Creating a Stream
We can create a stream when we declare it. The declaration uses the FILE type as shown below,
FILE *fp;


The FILE type is a structure that contains the information needed for reading and writing a file, fp is a pointer to the stream.

Opening File
Once stream has been created, we can ready to associate to a file. In the next section we will discuss in detail.
Closing the Stream
When file processing is complete, we close the file. After closing the file the stream is no longer available.
Files:
When the program is terminated, the entire data is lost in C programming. If you want to keep large volume of data, it is time consuming to enter the entire data. But, if file is created, these information can be accessed using few commands.
There are large numbers of functions to handle file I/O in C language. In this tutorial, you will learn to handle standard I/O(High level file I/O functions) in C.
High level file I/O functions can be categorized as:
1. Text file
2. Binary file
File Operations
1. Creating a new file
2. Opening an existing file
3. Reading from and writing information to a file
4. Closing a file
أحدث أقدم