Output file, adding suffix to input filename?

Posted on 2003-10-27
Last Modified: 2010-04-02
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.
Question by:killer455
  • 3
  • 3
LVL 16

Accepted Solution

imladris earned 45 total points
ID: 9629911
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:

   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


Author Comment

ID: 9630108
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? :)

LVL 16

Expert Comment

ID: 9630210
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.
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.


Author Comment

ID: 9630287
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.
LVL 16

Expert Comment

ID: 9630540
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.

Author Comment

ID: 9630881
Ok thanks for your help, i'll setup a new question if need be.

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question