Testing with .in files without changing code - c++

I'm doing some programming problems from the previous year competition and in the problem text there is only one case, which is simple so I can just rewrite it when testing.
Now, I also have a folder with a bunch of .in and .out files, in format '01.in, 01.out, 02.in, 02.out, etc'.
Is there a way to somehow take one of those .in files and automatically use all the lines of it as input without making changes inside my program but rather doing it directly from the command line?
Thanks

Assuming linux:
cat *.in | yourprogram
On Windows you'd use type instead of cat.

I assume your program takes in and processes the agruments (argv[]) passed to it already. If this is the case, one way could be to write a simple wrapper program (in Python for example) which opens the required .in files, reads the lines in it and invokes your C++ program by passing the required lines as input to them.
Then you can execute this python program or make changes to it as required.

Related

Dev-C++ Save source files with similar names?

I'm new to C++ and I'm using Dev-C++ compiler to learn my coding but I'm having some trouble saving the source files. If I save a source files with similar names to the first one, it always executes the first one whenever I compile and run it. For example, if I name the first source file Hello World.cpp and the second Hello World Anything.cpp it will always execute the code from the first source file. I either have to quit the program and create a new source file for the second or rename the file to something else like HW Anything.cpp. How do I save the files with similar names? Thanks!
Avoid using spaces in filenames/folders when dealing with Dev-CPP. instead, use underscores _ as a subsitute for spaces or CapitalWords.

How to pack multiple executable files into a single one?

I have multiple executable files. I want to write a program using cpp which will extract them to a specific location. Like unpacking a zip archive but i want it to be an executable and work in any clean install of windows. (vista or later) Is there a way to do it?
Edit: I know how to make a self extracting zip file, i want to learn how can i do it myself.
Most of archivers have option to create executable files. You don't need to write own program.
EDIT:
If you really want to do all of it by own, you should use windows resource files. (Using of them is describer in another question.) I see two possible ways to do this.
Add every single file to your program, as a resource. Program should remember name of each file and save them to directory one by one.
Pack all the files into one. (You need to write second program to pack the files.) Exemplary format of the package can be as follows:
| 4-bytes - length of 1st filename | 1st filename | 4-bytes size of 1st file | 1st file | 4-bytes - length of 2nd filename | ...
Package prepared in this way can be put into program resources. This is a more difficult method but it is also more elastic. You can modify the list of files without changing anything in the program.

Use User Input/Text File as Code

Is it possible to treat user input as lines of code in C++?
Or write it to a text file, then run that as c++ code?
I'm writing my own version of matlab, and right now it can do a lot of the numerical stuff, but only if I write the commands in like a script in main. Is there a way I could do it like matlab's command line?
Yes, there are many libraries that allow for scripting like Lua or Chaiscript which will allow you to take a file of code and execute it. There's also the advantage of being able to make changes to the code and not having to recompile each time.

Is it a good idea to include a large text variable in compiled code?

I am writing a program that produces a formatted file for the user, but it's not only producing the formatted file, it does more.
I want to distribute a single binary to the end user and when the user runs the program, it will generate the xml file for the user with appropriate data.
In order to achieve this, I want to give the file contents to a char array variable that is compiled in code. When the user runs the program, I will write out the char file to generate an xml file for the user.
char* buffers = "a xml format file contents, \
this represent many block text \
from a file,...";
I have two questions.
Q1. Do you have any other ideas for how to compile my file contents into binary, i.e, distribute as one binary file.
Q2. Is this even a good idea as I described above?
What you describe is by far the norm for C/C++. For large amounts of text data, or for arbitrary binary data (or indeed any data you can store in a file - e.g. zip file) you can write the data to a file, link it into your program directly.
An example may be found on sites like this one
I'll recommend using another file to contain data other than putting data into the binary, unless you have your own reasons. I don't know other portable ways to put strings into binary file, but your solution seems OK.
However, note that using \ at the end of line to form strings of multiple lines, the indentation should be taken care of, because they are concatenated from the begging of the next lineļ¼š
char* buffers = "a xml format file contents, \
this represent many block text \
from a file,...";
Or you can use another form:
char *buffers =
"a xml format file contents,"
"this represent many block text"
"from a file,...";
Probably, my answer provides much redundant information for topic-starter, but here are what I'm aware of:
Embedding in source code: plain C/C++ solution it is a bad idea because each time you will want to change your content, you will need:
recompile
relink
It can be acceptable only your content changes very rarely or never of if build time is not an issue (if you app is small).
Embedding in binary: Few little more flexible solutions of embedding content in executables exists, but none of them cross-platform (you've not stated your target platform):
Windows: resource files. With most IDEs it is very simple
Linux: objcopy.
MacOS: Application Bundles. Even more simple than on Windows.
You will not need recompile C++ file(s), only re-link.
Application virtualization: there are special utilities that wraps all your application resources into single executable, that runs it similar to as on virtual machine.
I'm only aware of such utilities for Windows (ThinApp, BoxedApp), but there are probably such things for other OSes too, or even cross-platform ones.
Consider distributing your application in some form of installer: when starting installer it creates all resources and unpack executable. It is similar to generating whole stuff by main executable. This can be large and complex package or even simple self-extracting archive.
Of course choice, depends on what kind of application you are creating, who are your target auditory, how you will ship package to end-users etc. If it is a game and you targeting children its not the same as Unix console utility for C++ coders =)
It depends. If you are doing some small unix style utility with no perspective on internatialization, then it's probably fine. You don't want to bloat a distributive with a file no one would ever touch anyways.
But in general it is a bad practice, because eventually someone might want to modify this data and he or she would have to rebuild the whole thing just to fix a typo or anything.
The decision is really up to you.
If you just want to keep your distributive in one piece, you might also find this thread interesting: Store data in executable
Why don't you distribute your application with an additional configuration file? e.g. package your application executable and config file together.
If you do want to make it into a single file, try embed your config file into the executable one as resources.
I see it more of an OS than C/C++ issue. You can add the text to the resource part of your binary/program. In Windows programs HTML, graphics and even movie files are often compiled into resources that make part of the final binary.
That is handy for possible future translation into another language, plus you can modify resource part of the binary without recompiling the code.

Using windows CopyFile function to copy all files with certain name format

Hello! I am updating some C code that copys files with a certain name. basically, I have a directory with a bunch of files named like so:
AAAAA.1.XYZ
AAAAA.2.ZYX
AAAAA.3.YZX
BBBBB.1.XYZ
BBBBB.2.ZYX
Now, In the old code, they just used a call to ShellExecute and used xcopy.exe. to get all the files starting with AAAAA, they just gave xcopy the name of the file as AAAAA.* and it knew to copy all of the files starting with AAAAA. now, im trying to get it to copy with out having to use the command line, and I am running into trouble. I was hoping CopyFile would be smart enough to handle AAAAA.* as the file to be copied, but it doesnt at all do what xcopy did. So, any Ideas on how to do this without the external call to xcopy.exe?
Check this out as a starting point
http://msdn.microsoft.com/en-us/library/aa364418(VS.85).aspx
or even better this full example
http://msdn.microsoft.com/en-us/library/aa365200(v=VS.85).aspx
You could also use SHFileOperation or IFileOperation (the latter being only available from Vista upwards but is now the recommended way according to MSDN). SHFileOperation supports wildcards and displays a progress by default, but there's also a flag for silent operation.
Check out the following MSDN links for more info:
http://msdn.microsoft.com/en-us/library/bb762164(v=VS.85).aspx
http://msdn.microsoft.com/en-us/library/bb775771(v=VS.85).aspx
You would basically have to write code to reproduce the functionality in xcopy. To do so, you must build a list of files by accessing the path and recursing through it. Test each found entry with your pattern and keep only those that match. Then iterate over that list with CopyFile.
See the following set of functions that can help you build the file list:
http://en.wikipedia.org/wiki/Dirent.h
It might just be easier to keep using xcopy unless you have a specific reason not to.
There are lots of ways to do it. I'd probably use a loop of FindFirstFile() / FindNextFile().
However, is there any reason you can't still use xcopy? You can launch it with CreateProcess(). It isn't pretty, but it works.