Rabu, 02 Desember 2015

C File

C File

What is File?
file is an object on a computer that stores data, information, settings, command that are used with computer program.

in windows, files are indentified either by the file extension. for example ".exe" for executeable file, ".dll" file for DLL file and many others.
files also have an illegal files character, they are \ / ? * : " > < |

there are 2 kinds of files in general:
1.Text File
text file is a computer file that only contains text and has no special formatting. in windows, usually text file has ".txt" extension. examples: plain text, source code, html file, etc.

2.Binary File
any file that contains formatted text, non-text characters. examples: program file, image file, etc.


Redirection?
One way to get input into a program or display an output from a program we use standard input output. and it's means that if we want to read in data we use scanf() and to write out data we use printf().

but when we want to redirect and take input from a file. we can use input redirection.
     % a.out < inputfile (the "<" mark for input a file)
and when we want to display the output, we can use output redirection.
     % a.out >output file (the ">" mark for output a file)


Opening a File
to open a file, you can use fopen() function. you also can use fopen() function to create a new file.
ussually, the structure how to open a file is by this following
FILE *fileopen("url of file or filename.txt", "mode");

and these are the mode usually used to open a file:
  • "r" (read) = to open a text file, the file must exist.
  • "w" (write) = to create a new file, to open a file for writing because the file doesn't exist. if the name already exist, it will be discarded and will be an empty file.
  • "a" (append) = open a file for appending mode, if the file doesn't exist, a new file will be create. this is for output at the end of the line, expanding it.
  • "r+" (read/update) = open a file for update (both input and output). the file must exist.
  • "w+" (write/update) = to create a new file and open it for update(both input and output). if the name already exist, it will be discarded and will be an empty file.
  • "a+" (append/update) = open a file for reading and writing (input and output). the reading will start from begining but writing can only be appended.

With the mode specifiers above the file is open as a text file. In order to open a file as a binary file, a "b" character has to be included in the mode string. This additional "b" character can either be appended at the end of the string (thus making the following compound modes: "rb", "wb", "ab", "r+b", "w+b", "a+b") or be inserted between the letter and the "+" sign for the mixed modes ("rb+", "wb+", "ab+").

The new C standard (C2011, which is not part of C++) adds a new standard subspecifier ("x"), that can be appended to any "w" specifier (to form "wx""wbx""w+x" or "w+bx"/"wb+x"). This subspecifier forces the function to fail if the file exists, instead of overwriting it.


fopen() example:
#include <stdio.h>
int main () {
FILE *pfile;
pfile = fopen ("anyfile.txt", "w");
if (pfile!=NULL) {
     fputs ("fopen() example", pfile);
     fclose (pfile);
     }
     return 0;
}


Writing a File
The function fputc() writes the character value of the argument c to the output stream referenced by fp. It returns the written character written on success otherwise EOF if there is an error.
#include <stdio.h>
int main () {
FILE *pfile;
char c;
pfile = fopen ("alphabet.txt", "w");
if (pfile!=NULL) {
for (c='A';c<='Z';c++)
     fputc( c, pfile);
     fclose (pfile);
     }
     return 0;
}

The function fputs() writes the string s to the output stream referenced by fp. It returns a non-negative value on success, otherwise EOF is returned in case of any error. Make sure you have /tmp directory available. If it is not, then before proceeding, you must create this directory on your machine
#include <stdio.h>
int main () {
   FILE *pfile;
   char sentence [256];

   printf("Enter sentence to append: ");
   fgets(sentence,256,stdin);
   pfile = fopen ("text1", "a");
   fputs (sentence,pfile);
   fclose (pfile);
   return 0;
}

Reading a File
The fgetc() function reads a character from the input file referenced by fp. The return value is the character read, or in case of any error, it returns EOF. The following function allows to read a string from a stream −
char *fgets(char *buf, int n, FILE *fp);

The functions fgets() reads up to n-1 characters from the input stream referenced by fp. It copies the read string into the buffer buf, appending a nullcharacter to terminate the string.
#include <stdio.h>
main() {
   FILE *fp;
   char buff[255];

   fp = fopen("/tmp/test.txt", "r");
   fscanf(fp, "%s", buff);
   printf("1 : %s\n", buff );

   fgets(buff, 255, (FILE*)fp);
   printf("2: %s\n", buff );
   
   fgets(buff, 255, (FILE*)fp);
   printf("3: %s\n", buff );
   fclose(fp);
}


Closing a File
To close a file, use the fclose( ) function. The prototype of this function is "int fclose (FILE *fp)". The fclose(-) function returns zero on success, or EOF if there is an error in closing the file.

This function actually flushes any data still pending in the buffer to the file, closes the file, and releases any memory used for the file. The EOF is a constant defined in the header file stdio.h.
#include <stdio.h>
int main () {
FILE *pfile;
   pfile = fopen ("testfile.txt", "wt");
   fprintf (pfile, "fclose example");
   fclose (pfile);
}

Tidak ada komentar:

Posting Komentar