load xml values and pass it into string in vc++ - c++

Am working in vc++ and trying to load an xml file and load the entire data into a string but am not getting the results
char text[700] = {""};
TiXmlDocument doc( "'demotest.xml" );
bool loadOkay = doc.LoadFile();
if ( !loadOkay )
{
printf( "Could not load test file 'demotest.xml'. Error='%s'. Exiting.\n", doc.ErrorDesc() );
system("PAUSE");
exit( 1 );
}
printf( "** Demo doc read from disk: ** \n\n" );
printf( "** Printing via doc.Print **\n" );
//doc.Print( stdout );
{
printf( "** Printing via TiXmlPrinter **\n" );
TiXmlPrinter printer;
doc.Accept( &printer );
fprintf( stdout, "%s", printer.CStr() );
//upto this line its working fine in console. but when I convert this string am getting struck
wsprintf(text, "%s", (char*)printer.CStr());
AddLOG_message(text, 0, true);
}
Last two lines I should get the entire content of the xml including header, elements and values.
Please help.

I'd do it like this, with less C code, more C++ code and deprecating the risky char array of length magic number 700:
TiXmlPrinter printer;
doc.Accept( &printer );
doc.Print(); // simpler for stdout output
std::string text = printer.CStr(); // easier, safer this way
AddLOG_message( text.c_str(), 0, true );

Related

Redirect stdout back to console

There is plenty of documentation about redirecting stdout and stderr to a file instead of the console. How do you redirect it back again? The code below shows my intention, but outputs "stdout is printed to console" only once.
I'm guessing I need to grab the console output buffer, store it somewhere, redirect stdout to file, then restore the console buffer?
#pragma warning(disable:4996)
#include <cstdio>
int main()
{
std::printf("stdout is printed to console\n");
if (std::freopen("redir.txt", "w", stdout)) {
std::printf("stdout is redirected to a file\n"); // this is written to redir.txt
std::fclose(stdout);
std::printf("stdout is printed to console\n");
}
getchar();
return 0;
}
With thanks to the articles in the comments above I found the information I needed. The dup and dup2 functions were what I needed. Note that based on info here dup and dup2 are deprecated in favour or _dup and _dup2. A working example can be found on MSDN here, but is duplicated below in case the link breaks in the future.
// crt_dup.c
// This program uses the variable old to save
// the original stdout. It then opens a new file named
// DataFile and forces stdout to refer to it. Finally, it
// restores stdout to its original state.
#include <io.h>
#include <stdlib.h>
#include <stdio.h>
int main( void )
{
int old;
FILE *DataFile;
old = _dup( 1 ); // "old" now refers to "stdout"
// Note: file descriptor 1 == "stdout"
if( old == -1 )
{
perror( "_dup( 1 ) failure" );
exit( 1 );
}
_write( old, "This goes to stdout first\n", 26 );
if( fopen_s( &DataFile, "data", "w" ) != 0 )
{
puts( "Can't open file 'data'\n" );
exit( 1 );
}
// stdout now refers to file "data"
if( -1 == _dup2( _fileno( DataFile ), 1 ) )
{
perror( "Can't _dup2 stdout" );
exit( 1 );
}
puts( "This goes to file 'data'\n" );
// Flush stdout stream buffer so it goes to correct file
fflush( stdout );
fclose( DataFile );
// Restore original stdout
_dup2( old, 1 );
puts( "This goes to stdout\n" );
puts( "The file 'data' contains:" );
_flushall();
system( "type data" );
}

Generating a simple CT DICOM image using GDCM

I need to generate a series of CT DICOM images (512x512, MONOCHROME) using the GDCM library. Because of the structure of the application that I am working on, it is ideal for me to separately prepare gdcm::Image and gdcm::File instances for each image and at the end pass them on to gdcmImageWriter using the SetFile() and SetImage() methods.
The gdcmImageWriter::write() fails to write DICOM images. I have already looked at the source code of the VTKGDCMImageWriter class and this GDCM example for hints. It appears that the problem originates from some incompatibilities between gdcm::Image and gdcm::File instances (for instance, both classes contain information about the Photometric Interpretation of a DICOM image) that I pass on to gdcmImageWriter, but I am not sure exactly what those are.
The following simplified C++ code reproduces the error I have been getting:
#include <gdcmFile.h>
#include <gdcmImage.h>
#include <gdcmTag.h>
#include "gdcmDataElement.h"
#include "gdcmByteValue.h"
#include "gdcmPixelFormat.h"
#include "gdcmImageWriter.h"
#include "gdcmAnonymizer.h"
#include <iostream>
#include <string>
InitializeGDCMImage(gdcm::Image* imagePtr);
InitializeGDCMFile( gdcm::File* filePtr );
WriteFile( gdcm::Image* imagePtr , gdcm::File* filePtr , std::string fileName );
int main()
{
std::string fileName = "./TEST.dcm";
// Will be deleted by gdcm::writer.
gdcm::Image* imagePtr = new gdcm::Image;
gdcm::File* filePtr = new gdcm::File;
InitializeGDCMImage(imagePtr);
InitializeGDCMFile( filePtr );
WriteFile( imagePtr , filePtr , fileName );
return 0;
}
where,
void WriteFile( gdcm::Image* imagePtr , gdcm::File* filePtr , std::string fileName )
{
gdcm::ImageWriter* writer = new gdcm::ImageWriter;
writer->SetFileName( fileName.c_str() );
writer->SetImage( *imagePtr );
writer->SetFile( *filePtr );
if( !writer->Write() ){
std::cerr << "ERROR: Could not write to \"" << fileName << "\"" << std::endl;
}
delete writer;
}
WriteFile() sets gdcm::File and gdcm::Image and invokes write().
InitializeGDCMFile() modifies a few DICOM tags using gdcm::Anonymizer, and InitializeGDCMImage() sets the actual pixel values (skipped here for brevity).
void InitializeGDCMFile( gdcm::File* origianlFilePtr )
{
gdcm::File* filePtr = new gdcm::File;
gdcm::Anonymizer anon;
anon.SetFile( *filePtr );
anon.Replace( gdcm::Tag(0x0028,0x0002) , "1" ); //SamplesperPixel
anon.Replace( gdcm::Tag(0x0028,0x0004) , "MONOCHROME2" ); //PhotometricInterpretation
anon.Replace( gdcm::Tag(0x0028,0x0010) , "512" ); //Rows
anon.Replace( gdcm::Tag(0x0028,0x0011) , "512" ); //Columns
anon.Replace( gdcm::Tag(0x0028,0x0030) , "0.781\\0.781" ); //PixelSpacing
anon.Replace( gdcm::Tag(0x0028,0x1050) , "0" ); //WindowCenter
anon.Replace( gdcm::Tag(0x0028,0x1051) , "100" ); //WindowWidth
anon.Replace( gdcm::Tag(0x0028,0x1052) , "0" ); //RescaleIntercept
anon.Replace( gdcm::Tag(0x0028,0x1053) , "1" ); //RescaleSlope
*origianlFilePtr = *filePtr;
}
void InitializeGDCMImage(gdcm::Image* imagePtr)
{
imagePtr->SetPhotometricInterpretation( gdcm::PhotometricInterpretation::MONOCHROME2 );
imagePtr->SetNumberOfDimensions(2);
unsigned int dims[2]={512,512};
imagePtr->SetDimensions( dims );
imagePtr->SetSpacing( 0 , 1. );
imagePtr->SetSpacing( 1 , 1. );
imagePtr->SetIntercept(0.);
imagePtr->SetSlope(1.);
double dirCos[6]={1.,0.,0.,0.,1.,0.};
imagePtr->SetDirectionCosines( dirCos );
char *buffer = new char[512*512*sizeof(int16_t)];
imagePtr->SetPixelFormat( gdcm::PixelFormat::INT16 );
imagePtr->GetDataElement().SetByteValue( buffer , 512*512*sizeof(int16_t) );
imagePtr->GetPixelFormat().SetSamplesPerPixel(1);
delete[] buffer;
}

Simple way to unzip a .zip file using zlib [duplicate]

This question already has answers here:
Unzip a zip file using zlib
(4 answers)
Closed 7 years ago.
Is there a simple example of how to unzip a .zip file and extract the files to a directory? I am currently using zlib, and while I understand that zlib does not directly deal with zip files, there seems to be several additional things in zlibs's "contrib" library. I noticed and read about "minizip", and after reading some documents and looking at some of the code, I do not see a simple example of how to unzip a .zip file and extract the files to a directory.
I would like to find a platform independent way of doing so, but if that is not possible then I need to find a way for windows and mac.
zlib handles the deflate compression/decompression algorithm, but there is more than that in a ZIP file.
You can try libzip. It is free, portable and easy to use.
UPDATE: Here I attach quick'n'dirty example of libzip, with all the error controls ommited:
#include <zip.h>
int main()
{
//Open the ZIP archive
int err = 0;
zip *z = zip_open("foo.zip", 0, &err);
//Search for the file of given name
const char *name = "file.txt";
struct zip_stat st;
zip_stat_init(&st);
zip_stat(z, name, 0, &st);
//Alloc memory for its uncompressed contents
char *contents = new char[st.size];
//Read the compressed file
zip_file *f = zip_fopen(z, name, 0);
zip_fread(f, contents, st.size);
zip_fclose(f);
//And close the archive
zip_close(z);
//Do something with the contents
//delete allocated memory
delete[] contents;
}
Minizip does have an example programs to demonstrate its usage - the files are called minizip.c and miniunz.c.
Update: I had a few minutes so I whipped up this quick, bare bones example for you. It's very smelly C, and I wouldn't use it without major improvements. Hopefully it's enough to get you going for now.
// uzip.c - Simple example of using the minizip API.
// Do not use this code as is! It is educational only, and probably
// riddled with errors and leaks!
#include <stdio.h>
#include <string.h>
#include "unzip.h"
#define dir_delimter '/'
#define MAX_FILENAME 512
#define READ_SIZE 8192
int main( int argc, char **argv )
{
if ( argc < 2 )
{
printf( "usage:\n%s {file to unzip}\n", argv[ 0 ] );
return -1;
}
// Open the zip file
unzFile *zipfile = unzOpen( argv[ 1 ] );
if ( zipfile == NULL )
{
printf( "%s: not found\n" );
return -1;
}
// Get info about the zip file
unz_global_info global_info;
if ( unzGetGlobalInfo( zipfile, &global_info ) != UNZ_OK )
{
printf( "could not read file global info\n" );
unzClose( zipfile );
return -1;
}
// Buffer to hold data read from the zip file.
char read_buffer[ READ_SIZE ];
// Loop to extract all files
uLong i;
for ( i = 0; i < global_info.number_entry; ++i )
{
// Get info about current file.
unz_file_info file_info;
char filename[ MAX_FILENAME ];
if ( unzGetCurrentFileInfo(
zipfile,
&file_info,
filename,
MAX_FILENAME,
NULL, 0, NULL, 0 ) != UNZ_OK )
{
printf( "could not read file info\n" );
unzClose( zipfile );
return -1;
}
// Check if this entry is a directory or file.
const size_t filename_length = strlen( filename );
if ( filename[ filename_length-1 ] == dir_delimter )
{
// Entry is a directory, so create it.
printf( "dir:%s\n", filename );
mkdir( filename );
}
else
{
// Entry is a file, so extract it.
printf( "file:%s\n", filename );
if ( unzOpenCurrentFile( zipfile ) != UNZ_OK )
{
printf( "could not open file\n" );
unzClose( zipfile );
return -1;
}
// Open a file to write out the data.
FILE *out = fopen( filename, "wb" );
if ( out == NULL )
{
printf( "could not open destination file\n" );
unzCloseCurrentFile( zipfile );
unzClose( zipfile );
return -1;
}
int error = UNZ_OK;
do
{
error = unzReadCurrentFile( zipfile, read_buffer, READ_SIZE );
if ( error < 0 )
{
printf( "error %d\n", error );
unzCloseCurrentFile( zipfile );
unzClose( zipfile );
return -1;
}
// Write data to file.
if ( error > 0 )
{
fwrite( read_buffer, error, 1, out ); // You should check return of fwrite...
}
} while ( error > 0 );
fclose( out );
}
unzCloseCurrentFile( zipfile );
// Go the the next entry listed in the zip file.
if ( ( i+1 ) < global_info.number_entry )
{
if ( unzGoToNextFile( zipfile ) != UNZ_OK )
{
printf( "cound not read next file\n" );
unzClose( zipfile );
return -1;
}
}
}
unzClose( zipfile );
return 0;
}
I built and tested it with MinGW/MSYS on Windows like this:
contrib/minizip/$ gcc -I../.. -o unzip uzip.c unzip.c ioapi.c ../../libz.a
contrib/minizip/$ ./unzip.exe /j/zlib-125.zip

how to capture result from system() in C/C++ [duplicate]

This question already has answers here:
Closed 13 years ago.
Possible Duplicate:
How can I run an external program from C and parse its output?
Hi,
Could someone please tell us how to capture a result when executing system() function ?
Actually I wrote a c++ program that displays the machine's IP address, called "ipdisp" and I want when a sever program executes this ipdisp program, the server captes the display IP address. So, is this possible? if yes, how?
thanks for your replies
Yes, you can do this but you can't use system(), you'll have to use popen() instead. Something like:
FILE *f = popen("ipdisp", "r");
while (!feof(f)) {
// ... read lines from f using regular stdio functions
}
pclose(f);
Greg is not entirely correct. You can use system, but it's a really bad idea. You can use system by writing the output of the command to a temporary file and then reading the file...but popen() is a much better approach. For example:
#include <stdlib.h>
#include <stdio.h>
void
die( char *msg ) {
perror( msg );
exit( EXIT_FAILURE );
}
int
main( void )
{
size_t len;
FILE *f;
int c;
char *buf;
char *cmd = "echo foo";
char *path = "/tmp/output"; /* Should really use mkstemp() */
len = (size_t) snprintf( buf, 0, "%s > %s", cmd, path ) + 1;
buf = malloc( len );
if( buf == NULL ) die( "malloc");
snprintf( buf, len, "%s > %s", cmd, path );
if( system( buf )) die( buf );
f = fopen( path, "r" );
if( f == NULL ) die( path );
printf( "output of command: %s\n", buf );
while(( c = getc( f )) != EOF )
fputc( c, stdout );
return EXIT_SUCCESS;
}
There are lots of problems with this approach...(portability of the syntax for redirection, leaving the file on the filesystem, security issues with other processes reading the temporary file, etc, etc.)

Execute a process and return its standard output in VC++

What's the easiest way to execute a process, wait for it to finish, and then return its standard output as a string?
Kinda like backtics in Perl.
Not looking for a cross platform thing. I just need the quickest solution for VC++.
Any ideas?
WinAPI solution:
You have to create process (see CreateProcess) with redirected input (hStdInput field in STARTUPINFO structure) and output (hStdOutput) to your pipes (see CreatePipe), and then just read from the pipe (see ReadFile).
hmm.. MSDN has this as an example:
int main( void )
{
char psBuffer[128];
FILE *pPipe;
/* Run DIR so that it writes its output to a pipe. Open this
* pipe with read text attribute so that we can read it
* like a text file.
*/
if( (pPipe = _popen( "dir *.c /on /p", "rt" )) == NULL )
exit( 1 );
/* Read pipe until end of file, or an error occurs. */
while(fgets(psBuffer, 128, pPipe))
{
printf(psBuffer);
}
/* Close pipe and print return value of pPipe. */
if (feof( pPipe))
{
printf( "\nProcess returned %d\n", _pclose( pPipe ) );
}
else
{
printf( "Error: Failed to read the pipe to the end.\n");
}
}
Seems simple enough. Just need to wrap it with C++ goodness.