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.
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center


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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

820 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