简体   繁体   中英

Accessing text data files from a static library function

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM