I want to load multiple file names from a dictionary(for example "Data/lua_files") into a C++ string, without adding them manually. For example whenever I add a new file I should be able to use it when I start my program without adding any code. Currently I use Sol2.0.
Can I save all file names into a new .txt file?(with a lua script?)
Is there any way to archive that?
I checked Google but didnt find anything
Thanks!
If by a "dictionary" you mean a Lua table:
fileNames = {
"file1.txt",
"file2.txt",
"file3.txt"
}
Then it's as easy as table.concat(fileNames, ","). It will return a string which you can then e.g. save into a global variable:
fileNamesString = table.concat(fileNames, ",")
And then use Sol to read it from the C++ side. I wonder if it's necessary to go through that extra step, though; I thought that the library supported direct table access. With that in mind, it'd be enough to just:
sol::lua_state lua;
// read your script file here
for (std::string const& fileName : lua["fileNames"]) {
// do your operation
}
Related
I am familiar with Ruby and am trying to write a program in Crystal.
I have a file called special_file.txt that I want to read in my Crystal program, how do I do that?
Crystal is inspired by Ruby syntax and so you can often read and perform File operations in a similar manner. For example, Crystal has a File classclass which is an instance of the IO class containing a read method.
To read a file's contents on your filesystem you can instantiate a File object and invoke the gets_to_end method coming from the IO super class:
file = File.new("path/to/file")
content = file.gets_to_end
file.close
The gets_to_end method reads an entire IO objects data to a String variable.
You can also use a block invocation to achieve a similar result:
# Implicit close with `open`
content = File.open("path/to/file") do |file|
file.gets_to_end
end
Finally, the most idiomatic way to read the contents of an entire file would be the one line:
# Shortcut:
content = File.read("path/to/file")
I have a code in this format:
srcSAXController control(input_filename.c_str());
std::string output_filename = input_filename;
output_filename = "c-" + output_filename.erase(input_filename.rfind(XML_STR));
std:: ofstream myfile(output_filename.c_str());
coverage_handler handler(i == MAIN_POS ? true : false, output_filename);
control.parse(&handler);
myfile.write((char *)&control, sizeof(control));
myfile.close();
I want the content of object 'control' to be written into my file. How to fix the code above, so that content of the control object is written to the file.
In general you need much more than just writing the bytes of the object to be able to save and reload it.
The problem is named "serialization" and depending on a lot of factors there are several strategies.
For example it's important to know if you need to save and reload the object on the same system or if you may need to reload it on a different system; it's also fundamental to know if the object contains links to other objects, if the link graph is a simple tree or if there are possibly loops, if you need to support versioning etc. etc.
Writing the bytes to disk like the code is doing is not going to work even for something as simple as an object containing an std::string.
I would like to ask about inputing more than one file or about the easiest way how to put filenames to some queue.
This files are about edit.
I have, let me say, 100 txt files and for each one I wanted to open it, find something and and save it.
I have functions/methods for each operation.
But I run into problem loading files into program. I made it for five or less files.
Process is. Program asked me for file name in program root directory or full path to file
for example C/myfile.txt
after pressing enter executes.
unfortunately bad thing happened and I have to do 100 files per day. So I know in C# is possible to make open file dialog - multiple file load...
In this program, I was thinking about doing static array of strings and for each make a for cycle with iterration.
but I have no idea what is the easier way how to load this strings (filenames) into this array.
I read something on msdn but it looks so complicated for me.
Can someone help? Program is just for me and I don't want to set too many things. It is possible?
what is the less or minimalistic part of code I must use - add to my program?
example of result I want
array[0] = C/text. txt
array[1] = C/texta. txt
...
array[50] = C/textdhsjfk. txt
maybe it is not so easy as I think, maybe yes.
But I have no support in this major... So I only tried to find something on the interent and I am not sure about the result I have found.
Thank you for time and willing to help.
A loop and a container look like good things to use:
static const char filenames[] =
{
"C:\\Fred\\file1.txt", "c:\\Fred\\file2.txt", "c:\\Barney\\file1.txt",
};
static const unsigned int quantity_of_files =
sizeof(filenames) / sizeof(filenames[0]);
//...
for (unsigned int i = 0u; i < quantity_of_files; ++i)
{
std::ifstream input_stream(filenames[i]);
Process_Input_Stream(input_stream);
input_stream.close();
}
The task is to get the code working correctly and robust, then worry about performance.
Note: The program is set up so that file names can be added to the array without having to change or retest the code.
How to I enable a static library to pull in data available in ascii data files?
I am trying to add a model to a simulation as a library which contains functions that read data from data files. I am able to compile and run the functions from a main program outside the actual full simulation, but once I put the functions as a library on the host for the simulation the data no longer gets read.
As the path to the data is changing depending on the user, I cannot provide an absolute data path to the ascii data files. Is there a way to use objcopy to make the data files into object code in the library or how can I best access the data from my static library?
There are several solutions to open a file that has an unknown location at compile time. Prompt the user for the name of the file, including directory. Use an environment variable to designate the directory containing the file ... Fortran 2003 has an intrinsic to obtain the value of an environment variable. Obtain the information from a command line argument ... again Fortran 2003 has an intrinsic for this purpose. With all of these, construct the filename as a string variable and provide that variable to the FILE keyword of the OPEN statement.
I don't know why you inclouded the Fortran tag, but in Fortran you:
tell the code to open a file you want using a character string
to read from it
and to close it
There is no difference between a main program or a library.
If you have a function like, say:
void read_data_from_files() { ... }
You'll need to change it in the DLL to be more like:
DataObject read_data_from_file(const char* file_path) { ... }
And then call it appropriately.
You'll need to design DataObject.
I would like to embed a text file with some data into my program.
let's call it "data.txt".
This text file is usually loaded with a function which requires the text file's file name as input and is eventually opened using a fopen() call... some something to the lines of
FILE* name = fopen("data.txt");
I can't really change this function and I would like the routine to open this same file every time it runs. I've seen people ask about embedding the file as a header but it seems that I wouldn't be able to call fopen() on a file that I embed into the header.
So my question is: is there a way to embed a text file as a callable file/variable to fopen()?
I am using VS2008.
Yes and No. The easiest way is to transform the content of the text file into an initialized array.
char data_txt[] = {
'd','a','t','a',' ','g','o','e','s',' ','h','e','r','e', //....
};
This transformation is easily done with a small perl script or even a small C program. You then compile and link the resulting module into your program.
An old trick to make this easier to manage with a Makefile is to make the script transform its data into the body of the initializer and write it to a file without the surrounding variable declaration or even the curly braces. If data.txt is transformed to data.inc, then it is used like so:
char data_txt[] = {
#include "data.inc"
};
Update
On many platforms, it is possible to append arbitrary data to the executable file itself. The trick then is to find it at run time. On platforms where this is possible, there will be file header information for the executable that indicates the length of the executable image. That can be used to compute an offset to use with fseek() after you have opened the executable file for reading. That is harder to do in a portable way, since it may not even be possible to learn the actual file name of your executable image at run time in a portable way. (Hint, argv[0] is not required to point to the actual program.)
If you cannot avoid the call to fopen(), then you can still use this trick to keep a copy of the content of data.txt, and put it back in a file at run time. You could even be clever and only write the file if it is missing....
If you can drop the call to fopen() but still need a FILE * pointing at the data, then this is likely possible if you are willing to play fast and loose with your C runtime library's implementation of stdio. In the GNU version of libc, functions like sprintf() and sscanf() are actually implemented by creating a "real enough" FILE * that can be passed to a common implementation (vfprintf() and vfscanf(), IIRC). That faked FILE is marked as buffered, and points its buffer to the users's buffer. Some magic is used to make sure the rest of stdio doesn't do anything stupid.
For any kind of file, base on RBerteig anwser you could do something simple as this with python:
This program will generate a text.txt.c file that can be compiled and linked to your code, to embed any text or binary file directly to your exe and read it directly from a variable:
import struct; # Needed to convert string to byte
f = open("text.txt","rb") # Open the file in read binary mode
s = "unsigned char text_txt_data[] = {"
b = f.read(1) # Read one byte from the stream
db = struct.unpack("b",b)[0] # Transform it to byte
h = hex(db) # Generate hexadecimal string
s = s + h; # Add it to the final code
b = f.read(1) # Read one byte from the stream
while b != "":
s = s + "," # Add a coma to separate the array
db = struct.unpack("b",b)[0] # Transform it to byte
h = hex(db) # Generate hexadecimal string
s = s + h; # Add it to the final code
b = f.read(1) # Read one byte from the stream
s = s + "};" # Close the bracktes
f.close() # Close the file
# Write the resultan code to a file that can be compiled
fw = open("text.txt.c","w");
fw.write(s);
fw.close();
Will generate something like
unsigned char text_txt_data[] = {0x52,0x61,0x6e,0x64,0x6f,0x6d,0x20,0x6e,0x75...
You can latter use your data in another c file using the variable with a code like this:
extern unsigned char text_txt_data[];
Right now I cant think of two ways to converting it to readable text. Using memory streams or converting it to a c-string.