Merge Files , in c++

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2008
Posts: 3
Reputation: bindiK is an unknown quantity at this point 
Solved Threads: 0
bindiK bindiK is offline Offline
Newbie Poster

Merge Files , in c++

 
0
  #1
May 21st, 2008
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 <fileNames>
Synopsis:
Dynamically creates an array of <count> ifstream objects then uses
them to open the files named in <fileNames>, 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 <count> 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 <count> 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 <count>
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 <files>
Synopsis:
Proceeding in order from the first file specified in <files>, 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,
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Merge Files , in c++

 
0
  #2
May 21st, 2008
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 3
Reputation: bindiK is an unknown quantity at this point 
Solved Threads: 0
bindiK bindiK is offline Offline
Newbie Poster

Re: Merge Files , in c++

 
0
  #3
May 22nd, 2008
Originally Posted by Ancient Dragon View Post
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.
  1. ifstream *OpenFiles(char * const fileNames[], size_t count)
  2. {
  3. char *first, *last, *ptr;
  4. fileNames[] = new char * const [count];
  5. int cnt = 0;
  6. for(first = fileNames, last = &first[count - 1]; first <= last; ++first)
  7. {
  8. OpenFiles (first, ios::in);
  9. cnt++;
  10. if(!OpenFiles.is_open())
  11. {
  12. while(cnt == 0)
  13. {
  14. OpenFiles.close();
  15. delete first;
  16. cerr << "Can't open input file " << FILENAME << endl; // If file is not opened,display an error message
  17. exit (1);
  18. --cnt;
  19. }
  20. else if( count == 0)
  21. {
  22. cerr << "Can't open input file " << FILENAME << endl; // If file is not opened,display an error message
  23. exit (1);
  24. }
  25. else
  26. {
  27. cout<< "Success in opening file " << FILENAME << endl;
  28. }
  29. }
  30. }
  31. return fileNames;
  32. }
Last edited by Narue; May 22nd, 2008 at 3:22 pm. Reason: Added code tags
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC