Full Member
Покинул форум
Сообщений всего: 160
Дата рег-ции: Дек. 2017
Репутация: 0
Карма 0
|
Added functions for working with text files. Reading and writing.
================================================== ==
fopen - opens the file. It takes two parameters. The first is string (path to the file), the second is string or char :
w - open the file for writing. If there is no such file, it is created. If there is, it will be overwritten.
a - open the file for recording. If there is no such file, it is created. If there is, it will be added.
r - open the file for reading.
Syntax:
fopen ("c:\5.txt", "w");
Returns a non-0 when the opening is successful.
================================================== ==
fclose - closes an open file.
Syntax:
fclose ();
================================================== ==
fwrite - writes to the open file. It takes as the parameter string , char , double , int or ENDL - instructions to translate a string.
Syntax:
fwrite ("Hello");
fwrite (5.25);
fwrite (ENDL); // translate the string
fwrite ("new line");
================================================== ==
fread - reads from an open file. It takes as a parameter string , char , double , int .
For string , double and int the boundaries of the read block from the file are the beginning, end of the file, space and line feed.
char reads all characters one by one, counting spaces.
Syntax:
int a;
fread (a);
Returns 1 if not the end of the file, 0 if the end of the file, -1 if the number is not written to the variable double or int .
================================================== ==
freadline - reads a line from an open file. It takes as a parameter string .
Syntax:
string a;
freadline (a);
Returns 1 if not the end of the file, 0 if the end of the file, -1 if the parameter is not string .
================================================== ==
Example of writing and reading from a file
CODE: // Initialize mixed types of variables
string a [8];
char b='';
double e=2.02;
a [0]="Demonstration";
a [1]="records";
a [2]="and";
a [3]="reading";
a [4]="in";
a [5]="file.";
a [6]="Cyborg";
a [7]="version";
// ================================================ ========
// open the file for writing "w". If the file exists it will be overwritten.
// To add to the end, open with "a"
if (fopen ("c:\5.txt", "w")!=0)
{// if opened
fwrite (a [0]); fwrite (b); // write to file
fwrite (a [1]); fwrite (b);
fwrite (a [2]); fwrite (b);
fwrite (a [3]); fwrite (b);
fwrite (a [4]); fwrite (b);
fwrite (a [5]);
fwrite (ENDL); // write a line feed to the file
fwrite (a [6]); fwrite (b);
fwrite (a [7]); fwrite (b);
fwrite (e);
fclose (); // close the file
}
// ================================================ ========
string preiemnik_a [8];
double preiemnik_e;
// Reading mixed types
if (fopen ("c:\5.txt", "r")!=0) // open the file for reading "r"
{// if opened
fread (preiemnik_a [0]); // reading one word
fread (preiemnik_a [1]);
fread (preiemnik_a [2]);
fread (preiemnik_a [3]);
fread (preiemnik_a [4]);
fread (preiemnik_a [5]);
fread (preiemnik_a [6]);
fread (preiemnik_a [7]);
fread (preiemnik_e); // read the number double
// Output of the molded string
messagebox (preiemnik_a [0] + "" + preiemnik_a [1] + "" +
preiemnik_a [2] + "" + preiemnik_a [3] + "" +
preiemnik_a [4] + "" + preiemnik_a [5] + "" + preiemnik_a [6] + "" + preiemnik_a [7] + "" + format (preiemnik_e));
fclose (); // close the file
}
// ================================================ ========
string preiemnik;
// Read the whole line
if (fopen ("c:\5.txt", "r")!=0) // open the file for reading "r"
{// if opened
l: if (freadline (preiemnik)!=0) // We read on the whole line until the end of the file
{
messagebox (preiemnik);
goto l;
}
fclose (); // close the file
}
// ================================================ ========
// Reading by word (spaces are skipped)
if (fopen ("c:\5.txt", "r")!=0) // open the file for reading "r"
{// if opened
l1: if (fread (preiemnik)!=0)
{
messagebox (preiemnik);
goto l1;
}
fclose (); // close the file
}
// ================================================ ========
char pr_с;
// Read by character
if (fopen ("c:\5.txt", "r")!=0) // open the file for reading "r"
{// if opened
l2: if (fread (pr_c)!=0)
{
messagebox (pr_c);
goto l2;
}
fclose (); // close the file
}
----- The Visual Code Editor is Kibor. Creating bots without knowledge of programming.
Learning function for recognizing text.
----- |