How to convert a string into a Standard C File ? - c++
Hi I have an image in my memory and I want to sent it through an external FTP library.
This FTP library accepts only and only standard C FILE and the sample codes provided by this library reads data only from hard disk. In my application I don't want to store my images in the hard disk and then read them using FILE variable, instead I want to do the conversion in my memory so it's faster and more professional.
My image is in the form of uchar * but I can change it to std::String or QByteArray or any other type of string. Now I want to know how can I have a file which is filled by my image data so I will get rid of storing it into the hard disk and read it again.
My pseudo code:
uchar * image = readImage();
FILE * New_Image = String2FileConverter(image); //I need this function
FTP_Upload(New_Image);
On Posix systems, you can use fmemopen to create a memory-backed file handle.
Related
C++ - Loading Base64 Encoded String of an image to Boost GIL image/view
I'm using Boosts Generic Image Library. I'm being given a string representation of an image. After decoding it, could I directly make an Image or View object with that data? Or would I need to write the data to the computer as example.png and use GIL's read_image functions? The documentation mentions dynamic images but still takes a filename as a parameter to the i/o functions. I would ideally be looking for a function that takes a string or byte array as a parameter rather than the image name to be loaded from disk. Something like GDI+ FromStream. I see that the documentation says "All functions take the filename or a device as the first parameter. A device could be a FILE*, std::ifstream, and TIFF*." Maybe it is possible to edit the contents of an ifstream to have the image data, not sure if this is actually possible though.
Correct way to extract array data from binary?
There is a classic way to embed resource files as a C language array into a binary file, so that we can store some external resource files such as .jpeg or .txt files into a binary. For example, in the header file we can define an array: const unsigned char xd_data[] = { 77,90,144,0,3,0,0,0,4,0,0,0,255,255,0,0,184,0,0,0,0,0,0,0,64,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,240,0,0, 0,14,31,186,14,0,180,9,205,33,184,1,76,205,33,84,104,105,115,32,112,114, 111,103,114,97,109,32,99,97,110,110,111,116,32,98,101,32,114,117,110, 32,105,110,32,68,79,83,32,109,111,100,101,46,13,13,10,36,0,0,0,0,0,0, 0,66,163,223,218,6,194,177,137,6,194,177,137,6,194,177,137,105,221,187, 137,13,194,177,137,133,222,191,137,3,194,177,137,105,221,181,137,4,194, 177,137,136,202,238,137,4,194,177,137,6,194,176,137,73,194,177,137,133, 202,236,137,13,194,177,137,48,228,187,137,11,194,177,137,193,196,183, 137,7,194,177,137,82,105,99,104,6,194,177,137,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,80,69,0,0,76,1,4,0,65,162,32,86,0,0,0,0,0,0,0, 0,224,0,47,1,11,1,6,0,0,100,0,0,0,74,0,0,0,0,0,0,228,113,0,0,0,16,0,0, 0,128,0,0,0,0,64,0,0,16,0,0,0,2,0,0,4,0,0,0,0,0,0,0,4,0,0,0,0,0,0,0,0, 224,0,0,0,4,0,0,0,0,0,0,2,0,0,0,0,0,16,0,0,16,0,0,0,0,16,0,0,16,0,0,0, 0,0,0,16,0,0,0,0,0,0,0,0,0,0,0,124,140,0,0,140,0,0,0,0,208,0,0,0,16,0 }; which contains the contents of the resource file, and it will be compile into the final binary. There are lots of tools and tutorials on the web about this old trick, such as: http://www.rowleydownload.co.uk/arm/documentation/index.htm?http://www.rowleydownload.co.uk/arm/documentation/embed.htm, https://www.fourmilab.ch/xd/ and http://gareus.org/wiki/embedding_resources_in_executables#c_include_method. However, looks like most of these pages are talking about how to embed the data into binary file using C style array. My question is, what is the correct way to find the start address of the resource files in the compiled binary in order to extract them? I.e., how can I find the start address of xd_data in the compiled binary?
If you mean finding the byte address in the file where a data block starts just like objdump does but programmatically, then you can use the Binary File Descriptor library (BFD), see here and here.
if you stored data for example an image and you want to load it (for printing or what ever you want) then if you have a function (library) that load it from memory, as example void loadResImage(void * mem); just do loadResImage(xd_data), if not but you have a function that load it from the file, in that case save it to a temp file eg: int fd=open("tmpfile"); int ret=write(fd,xd_data, sizeof(xd_data)); close(fd); loadImageFile("tmpfile"); but if you want to access the data outside the program itself (hex editor for example, or an other program), in that case you have to add a starting mark and optionally an ending mark or sizeof data. eg: const unsigned char xd_data[]={ ... 'M','A','G','I','C'}; in example above the end of the data is known, you just do a search to find it. same way, play around and find a suitable way to store the size of the data. but beware of the compiler optimizations.
Converting FILE * to CMemFile (or retrieving void* from FILE *)
I'm currently working with a function call that uses FILE *, but I need to get data from the FILE * into a buffer without letting the data touch the HDD. My first idea was to convert FILE * to a CMemFile, but I cannot find a way to do this. Any ideas? Using MFC... This question comes up from using the JPEG library by IJG whose homepage can be found here: http://www.ijg.org/
You can use setvbuf to make a FILE pointer use a predefined (and filled) buffer.
embedding a text file in an exe which can be accessed using fopen
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.
Emulate a file pointer C++?
I am trying to load a bitmap from an archive. The bitmap class I have takes a character pointer to a filename and then loads it if it is in the same directory. The bitmap loading class is well tested and I don't want to mess with it too much. Problem is it uses a file pointer to load and do all of its file manipulation. Is there any way to emulate a file pointer and actually have it read from a chunk in memory instead? Sorry if this is a bizarre question.
Refactor it and create functions that takes the exact same parameters as before : If you used fopen, fread and fseek that read from disk, create mopen, mread and mseek that read file from memory. You'll only have to fix the name of the functions. It should be easy without risk and code won't look like an dirty hack in the end.
You can also use a pipe. A pipe is a piece of memory where you can read and write using file primitives. Which is basically what you want (Assuming POSIX Operating system) create a pipe: int p[2]; pipe(p); use fdopen() to turn the pipe file descriptor into a FILE* FILE *emulated_file = fdopen(p[0], "r"); then write whatever you want to the write end of the pipe : write(p[1], 17 ,"whatevereyouwant"); Now : buf[32]; fread(&buf,1,32, emulated_file); cout<<buf<<endl; willl output "whateveryouwant".
Check out John Ratcliff's File Interface replacement for standard file I/O. It supports the feature you need. You'll still need to refactor the bitmap loading code to use the new interface. However, this interface supports loading from file on disk, or memory chunk in memory (as well as writing to file on disk, or to expandable memory chunks).