Is there a way to have multiple programs without having multiple projects? - codeblocks

I'm using Codeblocks and don't want to create a new project every time i want to code something different. Is there any way to have something like a single project and then just open the files to work on them?
In other words for example: I have one CPP file with some arrays and another file to read and write data from a text file. What I'm currently doing is having another separated project for the second file I described.

In a single project (with a single call of the main function) you can have, for example, a menu that calls several functions that execute the different pieces of code that interest you.
You can only have one int main() function call.

Related

Choosing a random mp4 file from directory in processing

I have a directory with a processing script and some .mp4 files, how do i choose a random one to display?
Break your problem down into smaller steps.
Can you write a program that simply lists all of the files in a directory? The File class might help, and the Java API is your best friend.
Can you write a program that takes that list of files and creates an array or ArrayList that contains all of them?
Can you write a program that takes an array or ArrayList and chooses a random element from it? Use hard-coded String values for testing.
When you get all of these individual steps working, you can combine them into a single program that chooses a random file from a directory. If you get stuck on a specific step, you can post a MCVE of just that step, and we'll go from there.

Rules for file extensions?

Are there any rules for file extensions? For example, I wrote some code which reads and writes a byte pattern that is only understood by that specific programm. I'm assuming my anti virus programm won't be too happy if I give it the name "pleasetrustme.exe"... Is it gerally allowed to use those extensions? And what about the lesser known ones, like ".arw"?
You can use any file extension you want (or none at all). Using standard extensions that reflect the actual type of the file just makes things more convenient. On Windows, file extensions control stuff like how the files are displayed in Windows Explorer and what happens when you double click on it.
I wrote some code which reads and writes a byte pattern that is only
understood by that specific programm.
A file extension is only an indication of what type of data will be inside, never a guarantee that certain data formatted in a specific way will be inside the file.
For your own specific data structure it is of course always best to choose an extension that is not already in use for other file formats (or use a general extension like .dat or .bin maybe). This also has the advantage of being able to use an own icon without it being overwritten by other software using the same extension - or the other way around.
But maybe even more important when creating a custom (binary?) file format, is to provide a magic number as the first bytes of that file, maybe followed by a file header structure containing a version number etc. That way your own software can first check the header data to make sure it's the right type and version (for example: anyone could rename any file type to your extension, so your program needs to have a way to do some checks inside the file before reading the remaining data).

Ruby: Random selection of files/classes

I'm trying to write a little text rpg kind of game. What I'm trying to do is make it so that there are multiple little adventures, each contained in their own source file. I want to be able to select one source file at random, create an instance of its contained class, and then call a function (we'll call it Adventure#start). I'm having trouble thinking of a way to accomplish this without using a crazy array/hash and a big case tree, all of which would need to be updated with every adventure added...
I feel like there's something obvious I'm missing, but is there a practical way to go about this? The main point being that I would want to not have to update other code just to add a new adventure, but rather simply add the source for said adventure, drop the file into the appropriate folder, and be done with it.
You can do it with the following steps:
Get all the files from the folder containing adventure files
Choose a random file
Require the chosen file
Parse the content of the chosen file for the class name
Create an instance of the adventure using (Kernel.const_get class_name).new
Call #start on the newly created instance

Can we use control the source formats/layout in DMExpress using environment variables?

I am using DMExpress tasks to do taransformations on my business data. These business data come in multiple format/layout. I need to be able to use single task for transformation on multiple source layouts. Any DMExpress experts here??
One way that I found out for doing transformation on multiple source layouts with the help of single task was by using the Dmexpress SDK to write the script for the task rather than building the tasks using the GUI task editor. SDK gives lot more flexibility compared to the GUI editor.
But if you are bound to GUI then there is a way around for this specific purpose. You should define a common name for the source layout. Only the source layout name is binded to the task but not the actual layout definition. So you can alter the layout definition while keeping the layout name constant to get a generic task.
FYI- DMExpress is now called DMX (Syncsort changed the name about a year ago).
Do you have multiple different record types within a single file or is each type of record in a separate file? Your question is not clear on this.
If they are in separate files, this is very easy, but you will need to create a separate DMX task for each file. In each of these tasks, define one of the files as the source and create a record layout that matches the format of that file.
If they are in the SAME file, it is only a little more difficult. You can split them into separate files by creating multiple targets and defining a named condition for each target using the SourceName() function (this function returns the name of the file that the current record came from). Then you can process them as separate files (see above). This works UNLESS you have a parent-child relationship going on between the different types of records in that single file. If that is the case, please post some sample data and I can advise on how to handle it.

What is the best way to edit the middle of an existing flat file?

I have tool that creates variables for a simulation. The current workflow involves hand copying those variables into the simulation input file. The input file is a standard flat file, i.e. not binary or XML. I would like to automate the addition of the variables to the flat input file.
The variables copy over existing variables in the file, e.g.
New Variables:
Length 10
Height 20
Depth 30
Old Variables:
...
Weight 100
Age 20
Length 10
Height 20
Depth 30
...
Would like to have the old variables copy over the new variable. They are 200 lines into the flat input file.
Thanks for any insights.
P.S. This is on Windows.
If you're stuck using flat, then you're stuck using the old fashioned way of updating them: read from original, write to temp file, either write the original row or change the data and then write that. To add data, write it to the temp file at the appropriate point; to delete data, simply don't copy it from the original file.
Finally, close both files and rename the temp file to the original file name.
Alternatively, it might be time to think about a little database.
For something like this I'd be looking at a simple template engine. You'd have a base template with predefined marker tokens instead of variable values and then just pass the values required to your engine along with the template and it will spit out the resultant file, all present and correct. There are a number of Open Source template engines available in Java that would meet your needs, I imagine such things are also available in your language of choice. You could even roll your own without too much difficulty.
Note that under Unix, one would probably look at using mmap() because you can then use functions such as memmove() to move the data around and add new data or truncate() the result if the file is then smaller (you may also want to use truncate() to grow the file).
Under MS-Windows, you have the MapViewOfFileEx() function to do the same thing. The API is different, though,
and I'm not exactly sure what happens or how to grow/shrink the file (MSDN also includes a truncate()-like function and maybe that works).
Of course, it's important to use memcpy() or memmove() properly to not overwrite the wrong data or go outside the buffer.

Resources