Struct and File

On 5th December 2018, we continued to learn Algorithm and Programming course. We got informations about Structure and Files.

STRUCT

Structure is a data type to store various kind of variable's data types.
syntax:
struct name_of_structure{
      dataType1 name1;
      dataType2 name2;
     ...
};

Typedef is an alias to simplify the naming of structures.
example :
typedef struct BINUSIAN{
     int ID;
     int age;
     char name[101];
     ...
}mhs;


FILE

Data File is a place to save data from keyboard. While stream is all input and output data.
There are 3 standard streams activated when opening C program:
1. Standard Input stream
2. Standard Output stream
3. Standard Error stream

We can define File into 2 definition : Text file and Binary file.
Text File saved in text format / ASCII File.
Binary File saved in affixed format in line with micro-processor format.

The syntax:
FILE *variable_of_file;
     var_of_file = fopen ("Location of file\\filename.txt","w");

fclose (var_of_file);

There are many file modes (as I stated before as an example, with 'w' alphabet)
w: write or create a new file.
r: read a file.
a: append, to add data into the file.
rb: read binary file.
wb: write binary file.

How to "write" new data into the File? use fprintf, fputs, fputc
How to "read" an existing file? use fscanf, etc.

Usually, we are able to use IF condition, to check whether the file exist or no. Syntax:
if (var_of_file !=NULL){
     statements;
} else {
     printf ("File does not exist!\n");
}

Data file


asdasd

No comments:

Post a Comment