954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Merge Files , in c++

I need help immediately to solve the following question:
can somebody suggest how to code the following?


Write functions OpenFiles and MergeAndDisplay:

OpenFiles syntax:
ifstream *OpenFiles(char * const fileNames[], size_t count);
Parameters:
fileNames - a pointer to the first element in an array representing
the names of text files to be opened. The array has
the underlying format:
char *fileNames[] = { "fileA", "fileB", etc. };
count - the number of elements in
Synopsis:
Dynamically creates an array of ifstream objects then uses
them to open the files named in , in order. All opens
are in the read-only text mode. If any open fails all previously
opened files are closed, the dynamic allocation is deleted, an
error message is output to cerr, and the program is terminated with
an error exit code. If is zero an error message is output
to cerr and the program is terminated with an error exit code.
Return:
a pointer to the first entry in the ifstream array if is
non-zero and all opens succeed; otherwise, the function does not
return.


MergeAndDisplay syntax:
void MergeAndDisplay(ifstream files[], size_t count);
Parameters:
files - a pointer to the first element in an array of
ifstream objects, where each object represents a text
file open in the text mode for reading.
count - the number of elements in the array in
Synopsis:
Proceeding in order from the first file specified in , the
first line in each is read and displayed, followed by the second
line in each, follow by the third, etc. When the end of any file
is reached that file is closed and the process continues using only
the remaining open files, until all files have finally been closed.
Empty lines are displayed as empty lines. Empty files are simply
closed and ignored.
Return:
void


• Specify the file name(s) on the command line.
• Both functions must handle any arbitrary number of files.
• Assume no line will contain more than 511 characters.
• Do not attempt to store the entire contents of any file.
• Do not attempt to read a file after reaching its EOF.


Please suggest ,its urgent

Thanks,

bindiK
Newbie Poster
3 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 

The first OpenFile() should be pretty easy. Use the new operator to allocate menory for an array of ifstream objects, open each of the files, then return the array.

The second function is a little more complicated. You will need a loop to read one line from each file in each loop iteration.

Would I write the program for you? I could, but I won't. You give it a try and post the code you have written, then ask questions about what you do not understand.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

The first OpenFile() should be pretty easy. Use the new operator to allocate menory for an array of ifstream objects, open each of the files, then return the array.

The second function is a little more complicated. You will need a loop to read one line from each file in each loop iteration.

Would I write the program for you? I could, but I won't. You give it a try and post the code you have written, then ask questions about what you do not understand.

Below is my code .just for the open files.Could you please review and suggest for any corrections that are needed to make it working.

ifstream *OpenFiles(char * const fileNames[], size_t count)
{
   char *first, *last, *ptr;
   fileNames[] = new char * const [count];
   int cnt = 0;
   for(first = fileNames, last = &first[count - 1]; first <= last; ++first)
   {
      OpenFiles (first, ios::in);
      cnt++;
      if(!OpenFiles.is_open())
      {
         while(cnt == 0)
         {
         OpenFiles.close();
         delete first;
        	cerr << "Can't open input file " << FILENAME << endl; // If file is not opened,display an error message
		   exit (1);
         --cnt;
         }
       else if( count == 0)
       {
          cerr << "Can't open input file " << FILENAME << endl; // If file is not opened,display an error message
		    exit (1);
       }
       else
       {
         cout<< "Success in opening file " << FILENAME << endl; 	
       }
      }
   }
   return fileNames;
}
bindiK
Newbie Poster
3 posts since May 2008
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You