File inputoutput in C ++: Some useful functions

zhaozj2021-02-11  199

File input / output in C : Some useful functions

Original: Ilia Yordanov, Loobian@cpp-home.com

TELLG () - Returns an INT type value that represents the current location of the "Built-in pointer". This function is only valid when you read a file. For example: #include void main () {// If we have already in Test_File.txt, "Hello" content ifstream file ("Test_File.txt"); char Arr [10]; file. Read (Arr, 10); // Since Hello accounts for 5 characters, here will return 5 cout << file.tellg () << endl; file.close ();} tellp () - with tellg () The same function, but it is used to write files. All in all: When we read a file and know when we have the current position of the built-in pointer; when we write a file, you should use TELLP () when we write the current location of the built-in pointer. Because of this The usage of the function is exactly the same as TELLG (), I will not give an example code. Seekp () - Remember seekg ()? When I read a file and want to reach a particular location in the file, I have used it. Seekp () is also true, but it is used to write a file. For example, if I read and write in the file, and to locate the three characters of the current location, I need to call FileHandle.seekg (-3). But if I write a file, and for example I have to rewrite 5 characters, I have to jump 5 characters back, so I should use FileHandle.seekp (-5). Ignore () - is used to read the file. If you want to skip a certain number of characters, just use this function. In fact, you can also use Seekg () instead, however, using ignore () has an advantage - you can specify a specific "Delimiter Rule", which makes ignore () stop at the specified location. The function prototype is as follows: iStream & Ignore (int ncount, delimiter); Ncount means the number of characters to be skipped, and Delimiter - With its name: If you want to stop at the end of the file, you can use the EOF value into it. This function is equivalent to seekg (); however, this parameter can also use other values, such as '/ n', can be positioned in the new row while the wrap is located.

The following is an example: #include void main () {// Assume "Hello World" in Test_File.txt Ifstream File ("Test_File.txt"); Static Char Arr [10]; // If you have not encountered character "L", you will be positioned forward until 6 characters // and if you encounter "L" during the period, you will stop forward, positioned at the point.Ignore (6, 'L) '); File.read (arr, 10); cout << arr << endl; // It will display "Lo World!" File.close ();} getLine () - Although I have mentioned in front of the chapter In this function, there are some contents we have never involved: This function can not only be read on line-by-line, and it can also set to stop reading after a certain character. The method of delivering this parameter is given: getLine (array, array_size, delim); the following is sample code: #include void main () {// Assume "Hello World" in Test_File.txt This content ifstream file ("test_file.txt"); static char Arr [10]; / * Read until one of the following conditions: 1) 10 characters have been read) encountered the letter "O" 3 A new line * / file.getLine (Arr, 10, 'o'); cout << arr << endl; // will display "hell" file.close ();} peek () - This function will return Enter the next character of the stream file, but it does not move the built-in pointer. I think you should remember that the function like get () also returns the next character of the input stream file, while it will move the built-in pointer. So when you call the get () function again, it will return the next character, not the previous one. Oh, using peek () will return characters, but it doesn't move "cursor". So, if you call the PEEK () function twice, it will return the same character.

Consider the following code: #include void main () {// Assume "Hello World" in Test_File.txt Ifstream File ("Test_File.txt"); char ch; file.get CH); cout << ch << endl; // will display "h" cout << char (file.peek ()) << endl; // will display "E" cout << char (file.peek () << Endl; // will again display "e" file.get (ch); cout << ch << end1; // still display "e" file.close ();} By the way, I forgot to talk about --Pek () function substantially returns the ASCII code of the character, not the character itself. Therefore, if you want to see the character itself, you have to call like I do in the example (the translation: to be converted to a char type). _Unlink () - Delete a file. If you want to use this function, you need to include IO.h header files in your program. The following is the example code: #include #include void main () {OFStream File; file.open ("delete_test.txt"); // Create a file file.close (); _Unlink ("delete_test.txt"); // Delete this file / / attempt to open this file, but if it doesn't exist // function will return an ios :: FileBit error value file.open ("delete_test.txt", iOS: : nocreate); // Verify whether it returns to this value if (file.rdstate () == ios :: failbit) cout << "error ...! / n"; // Jeh, successful file.close () } Putback () - This function will return the last reading character while moving the built-in pointer-1 character. In other words, if you use get () to read a character and use putback (), it will return the same character for you, but at the same time, the built-in pointer will move -1 characters, so you use GET () again, It still will return the same characters for you.

转载请注明原文地址:https://www.9cbs.com/read-5158.html

New Post(0)