Solved

Output file, adding suffix to input filename?

Posted on 2003-10-27
6
265 Views
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.
0
Comment
Question by:killer455
  • 3
  • 3
6 Comments
 
LVL 16

Accepted Solution

by:
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:

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
 

Author Comment

by:killer455
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? :)



0
 
LVL 16

Expert Comment

by:imladris
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.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:killer455
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.
0
 
LVL 16

Expert Comment

by:imladris
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.
0
 

Author Comment

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now