TOP CRICKETER HOUSE PRICE

Image
TOP CRICKETERS HOUSE IN THE WORLD RICKY POINTING 69.8 CRORE SHANE WATSON  62.8 CRORE YUVRAJ SINGH 60 CRORE MICHEAL CLARKE 59.3 CRORE DAVID WARNER 45.3 CRORE SHANE WARNE 38.4 CRORE SACHIN TENDULKAR 38 CRORE VIRAT KOHLI HOUSE 34 CRORE ROHIT SHARMA 30CR BRETT LEE HOUSE  27.9 CRORE

WORKING OF THE FILE



To open a file you need to use the fopen function, which returns a FILE pointer. Once you've opened a file, you can use the FILE pointer to let the compiler perform input and output functions on the file.
FILE *fopen(const char *filename, const char *mode);
Here filename is string literal which you will use to name your file and mode can have one of the following values
w  - open for writing (file need not exist)
a  - open for appending (file need not exist)
r+ - open for reading and writing, start at beginning
w+ - open for reading and writing (overwrite file)
a+ - open for reading and writing (append if file exists)
Note that it's possible for fopen to fail even if your program is perfectly correct: you might try to open a file specified by the user, and that file might not exist (or it might be write-protected). In those cases, fopen will create a file if you specify file mode as "w", "w+", "a", or "a+" otherwise it will return 0, the NULL pointer.
Here's a simple example of using fopen:
FILE *fp;

fp=fopen("/home/tutorialspoint/test.txt", "r");
This code will open test.txt for reading in text mode. To open a file in a binary mode you must add a b to the end of the mode string; for example, "rb" (for the reading and writing modes, you can add the b either after the plus sign - "r+b" - or before - "rb+")
To close a function you can use the function:
int fclose(FILE *a_file);
fclose returns zero if the file is closed successfully.
An example of fclose is:
fclose(fp);
To work with text input and output, you use fprintf and fscanf, both of which are similar to their friends printf and scanf except that you must pass the FILE pointer as first argument.
Try out following example:
#include <stdio.h>

main()
{
   FILE *fp;

   fp = fopen("/tmp/test.txt", "w");
   fprintf(fp, "This is testing...\n");
   fclose(fp;);

}
Thsi will create a file test.txt in /tmp directory and will write This is testing in that file.
Here is an example which will be used to read lines from a file:
#include <stdio.h>

main()
{
   FILE *fp;
   char buffer[20];

   fp = fopen("/tmp/test.txt", "r");
   fscanf(fp, "%s", buffer);
   printf("Read Buffer: %s\n", %buffer );
   fclose(fp;);

}
It is also possible to read (or write) a single character at a time--this can be useful if you wish to perform character-by-character input. The fgetc function, which takes a file pointer, and returns an int, will let you read a single character from a file:
int fgetc (FILE *fp);

Comments

Post a Comment

Popular posts from this blog

TECHNICAL INTERVIEW QUESTION

MS DHONI