Hello, I am a newbie in C++ programming. I try to write a C++ program to first read and split a hugh text file (that containing multiple small files inside it) into multiple small text files, then I need to write the first line of each small text file into a log and also delete the last 3 lines of each small text file.

I manage to split the hugh text file into small text files and also write the first line of each small text file into a log file. I also able to delete the last 3 lines of the first small text file but I can open nor alter subsequent small files.:confused:

Some example:
1) First, I got this hugh text file containing, let say (it has > 1000 files in the text):

100 123
101 123

100 100
103 50

50 12
51 500

2) Then I split them into 3 small text file called temp_1.txt, temp_2.txt, temp_3.txt. I also read the first line of each text into a log file:)
temp_1.txt temp_2.txt temp_3.txt
\File_A \File_B \File_C
100 123 100 100 50 12
101 123 103 50 51 500


temp_1.txt \File_A
temp_2.txt \File_B
temp_3.txt \File_C

3) Finally, I want to delete the last 3 lines of the text from each text file, meaning that I don't want 'C' and empty lines in the file. I got stuck here.:?:

I figure it may due to the ifstream function which not allow me to re-use same ifstream variable for multiple text files. Can anyone help me??:'(

Here is what I have done so far:

#include <iostream>
#include <string>  // string support
#include <fstream> // I/O file support
#include <sstream> // stringstream functions

using namespace std;

// convert number into string for auto-numbering text filenames
string Convert_To_String(int s)
	string character;
  ostringstream outputs;      // using <sstream> dictionary
 	outputs << s;               // convert value into a string
 	character = outputs.str();
 	return character;

const string inputFileName = "inputFile.txt";
const string outputFileHeader = "OUTPUT_";
const string outputFileExt = ".txt";

const string tempFileHeader = "Temp_";

int main()
     declare Input File Reader
    // temporary store information of the inputFile.txt
    string  tempArray[5000];

    int i = 0, totFiles = 0;	
    string outputFileName, tempFileName;
    // find number of files
          getline(FILE_READER, tempArray[i++], ':');
    // write total number of files into 'totFiles'
    totFiles = i;
    // close Input File
    // write temp output text files using auto-generated number
    for (i = 1; i < totFiles; i++)
            tempFileName = tempFileHeader + Convert_To_String(i) + outputFileExt;
            FILE_WRITER << tempArray[i] << endl;            

    // clear array
    for (i = 1; i < totFiles; i++)
	tempArray[i] = "";
    // display total number of output files
    cout << endl << "Total files created > " << totFiles - 1 << endl; 

    // create a log file

    int count;	// count number of lines in each file

    // read one file at a time into tempArray, write first line of the file into log
    // file and write each line of the file (excepting the last 3 lines) into 
    // with different output filename
    for (i = 1; i < totFiles; i++)
       tempFileName = tempFileHeader + Convert_To_String(i) + outputFileExt;
       FILE_READERS.open(tempFileName.c_str(), ios::in);
       outputFileName = outputFileHeader + Convert_To_String(i) + outputFileExt;
       // check if the file is available
       if (FILE_READERS)          
       	 for (count = 0; !FILE_READER.eof(); count++)
               getline(FILE_READER, tempArray[count], '\n');

         LOG_WRITER << outputFileName << "\t" << tempArray[0] << endl;
         cout << endl << "#Lines in " << tempFileName << " > " << count;

	for (int j = 0; j < count - 3; j++)
	   FILE_WRITER << tempArray[j] << endl;

	// clear array
	for (int j = 0; j < count - 3; j++)
	   tempArray[j] = "";

	cout << "File " << tempFileName << " Not Found." << endl;
    return 0;

Recommended Answers

All 4 Replies

You can reuse an ifstream object, but take note that opening and closing files does not affect the stream state. If you open a file, read to EOF, close the file, then open another file, the eofbit will still be set. So do something like this:

ifstream in("first file");

// Read first file

in.open("second file");

// Read second file

>for (count = 0; !FILE_READER.eof(); count++)
On a side note, the eof member function generally shouldn't be used as a loop condition because it may introduce a fencepost bug. You can use the result of getline instead, and the bug goes away:

count = 0;

while (getline(FILE_READER, tempArray[count], '\n'))

To properly use eof as a loop condition, you still need to perform subsequent tests to explicitly avoid processing the last record twice:

for (count = 0; !FILE_READER.eof(); count++) {
  if (!getline(FILE_READER, tempArray[count], '\n'))

Thanks, Narue. You save my day!!!:icon_cheesygrin::icon_cheesygrin:

i got another question. How can I make the first line of the text as the filename of the text??
Such as the first line of the text (File_A) in the temp_1.txt as the fileName for new text file?? Do you have any idea on this one??

>How can I make the first line of the text as the filename of the text??
The filename is just a string:

ifstream in("filenames");
string line;

if (getline(in, line)) {

    // ...

Many thanks, Narue. The code works brilliantly for me!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.