Remove the repeated line in the file

xiaoxiao2021-03-06  41

Remove the repeated line in the file

In fact, it is a very simple operation, which is written here because it is recently seen in Thinking In C Volume 2 about iostream chapters, as exercises, so write it out. J

Define constants first

Const int unique_lines_ok = 0;

Const int unique_lines_error = 1;

Const int file_open_error = 2;

The following functions INT ULISF (Const String & Strfilename) use the functions used, where

Parameters const string & string & string & string is the name of the file;

return value

Unique_lines_ok: Deleting the duplicate row execution is successful;

Unique_lines_ERROR: Write file error;

File_Open_ERROR: The file is open failed.

The function is as follows:

INT ULISF (Const String & Strfilename)

{

INT IRET = UNIQUE_LINES_OK;

Set setstrline; // the string set

String strreadline; // Temporary String Read from the file

// read each line from the file - begin -

ifstream fin (Strfilename.c_STR ()); // Read The File

IF (! fin) // Verify Open

{

IRET = file_open_error;

Return IRet;

}

While (getLine (Fin, Strreadline)

{

Setstrlines.insert (strreadline);

}

Fin.close ();

// r r - END - END -

// delete the file

REMOVE (Strfilename.c_str ());

// Write the strings of the set into the file - begin -

OFSTREAM Fout (Strfilename.c_str ()); // Write the file

IF (! fout) // Verify Open

{

IRET = file_open_error;

Return IRet;

}

Set :: Iterator IT;

For (it = setstrlines.begin (); it! = setstrlines.end (); it )

{

Fout << IT-> c_str () << endl;

}

Fout.close ();

// Write the strings of the set inTo the file - end -

Return IRet;

}

Due to the file in the C standard library, you need to add header files

#include

#include

#include

#include

#include

(Finish)

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

New Post(0)