Output file, adding suffix to input filename?

Here is a rundown on what I want to do.  There is an input file, in which the contents are going to be placed into an array.  The array is then going to be sorted, by being passed into a sort function.  Then the sorted array should be placed into an output file.  However the output file needs to have an added suffix pertaining to the sort function that is used.

For example:

input file: data
output file should be : data.somestring

Basically my question is, should i pass the input filename (string) to the function and create the output file through the function?  Or do it through the main()??  One problem I might run into concerns having several sorts run on the array.  If i have to run two different sorts I dont want the array from one sort to be mixed up with another if passing it back to main().

Hope I wasnt too confusing.  Thanks.
killer455Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

imladrisCommented:
There are probably, as usual in programming, half a dozen different ways of doing things.

However, given that the process of writing the output file based on an array will be the same for each sort, my inclination would be to have that called from main, with an argument indicating what suffix to use.

Also, given that you might need to call different sorts for the array, you will probably want to make a copy of it before you pass it to the sort routine. So I would envision something like this:

main
   read input file into array
   copy array
   call sort 1 for copied array
   write copied (and now sorted) array to file with suffix for sort 1
   copy array again
   call sort 2 for copied array
   write copied (and now sorted) array to file with suffix for sort 2
   etc.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
killer455Author Commented:
I understand your answer and its helping me understand some possible ways to do this, but, one of the sets of data I am probably going to be testing might have around 50,000 items.  Would making copies of such an array of this size pose any problems?

I guess I kinda saw this working by passing the array to each of the sorts and then having each seperate sort function output its own file with the suffix, but your still basically making copies anyway correct?  If so then thanks for the outline of a possible solution.

Sorry if some of what im saying is a bit simple or ignorant sounding, but i guess thats why im here right? :)



0
imladrisCommented:
If each of your 50,000 items were 1 byte (a character), it would be using up 50K. If each were 4 bytes (an integer) it would be using up 200K. If each were 10 bytes it would take up 500K (0.5 Meg). So, probably, on modern machinery (which usually sport in excess of 64Meg of RAM) not too significant.

Based on your description of reading the input file once, it would appear that the sort is occurring in memory. Thus, if you want to avoid one sort affecting another, you are going to have to make copies one way or another. The obvious alternative this suggests, is to reread the input file for each sort.

The design principle to normally follow is to do similar things at the same "level". So, if you are reading the input file once, it would have to be done in main, and so you should do the outputting in main.

If you are going to do the inputting once for each sort, it would appear to be more logically associated with the sort, and you will probably want the sort to incorporate the reading and writing of the file as well.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

killer455Author Commented:
Ok well I guess its just a matter of how I want to do it, whether keep the input/output all in the main or do it in each of the sorting functions.

 I'm a little ahead of myself asking this question but i might as well :) (points increased).  I'm planning on writing the program to accept either integer files or strings, therefore creating an array of integers or strings respectively.  Is there a simple way to determine if it is an integer array or string array and then apply a correct suffix?

For example:
Data file:  data1
Integer array for heapsort output file:  data1.intheapsort
String array for heapsort output file:  data1.stgheapsort  

I'm not quite to the point where I need to start asking about templates but i will eventually want to incorporate a template so the input can be integers or strings, without having to write seperate programs.
0
imladrisCommented:
In general it is better to open a new question, rather than increase points. If the new question doesn't get answered, for whatever reason, the original sequence becomes lost, both to the PAQ's, and it also becomes difficult to close the question and assign points fairly.

If you're asking whether there is a simple way to inspect a file and ascertain whether it contains integers or strings, then, in general, the answer is no. At bottom, to the computer, everything is a number. You only see characters on the screen here, because we have agreed to use the ASCII representation in this context. That means that we have agreed that the value decimal 65 will represent a capital A, and that the value 66 will be a B. However, if we were using EBCDIC, we would need different numbers to represent those characters. Or, if the computer thought they were numbers it would look at those two bytes, see 0x4142 (in hexadecimal) and would conclude it is the number decimal 16706.

So, either the program must be told what the file contains, or there must be some convention, or something known, about what the file contains.
0
killer455Author Commented:
Ok thanks for your help, i'll setup a new question if need be.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.