R
rory
I'm having some inconsistencies in my program that I can't seem to
debug. Below is code that makes a copy of a binary file, appends a
unique ID and string to the copy of the binary file and then checks
the string by searching for the ID which is also a string and
retrieves the text that follows. I'm building the code with scons
along with some other applications that are part of the project. Here
is the strange thing. If I set a new unique string and compile on it's
own my code below works fine. If I then call scons to built all the
project files including the code below it doesn't work. If I then try
to build it on its own again it still doesn't work. The only way to
get it to work again is to change the string ID and compile it again
on its own. I know this is a strictly c++ language list and my post
does refer to scons but all scons does is call the g++ compiler. Can
anyone help me with this problem? Here is my code, although I don't
think the problem is with the code.
////////////////////////////////////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
if(argc<2){
cout << "\nUsage: copy_append_read <csound file> <output filename>
\nExample: cabbage test.txt test.exe\n";
return 0;
}
std::string Text, str;
long begin, end;
//make a copy of binary cabbage.dat with new name
int ret = copyfileC("cabbage.dat", argv[2]);
if(!ret) cerr << "Error: Could not copy/create file, please make
sure that cabbage.dat is located in the same folder as cabbage";
//read contents of input file
ifstream file(argv[1]);
while(!file.eof())
{
getline(file, str);
Text = Text+str;
}
//write identifier and input file string to runtime
WriteData(argv[2], Text);
//test that data was written and can be retreived
ReadData(argv[2]);
}
///////////////////////////////////////////////////////////////////////////////////////////
//////// Make a copy of the runtime binary
///////////////////////////////////////////////////////////////////////////////////////////
int copyfileC(std::string oldFile, std::string newFile)
{
int c;
FILE *in, *out;
in = fopen(oldFile.c_str(), "rb");
out = fopen(newFile.c_str(), "wb");
if(!in && !out)
return 0;
while((c=fgetc(in))!=EOF)
{
fputc(c,out);
if(ferror(out)) cout << "error when writing new file";
}
fclose(in);
fclose(out);
return 1;
}
///////////////////////////////////////////////////////////////////////////////////////////
//////// Append magic marker and Text to the runtime binary
///////////////////////////////////////////////////////////////////////////////////////////
void WriteData(std::string binfile, std::string Text)
{
ofstream myFile (binfile.c_str(), ios:
ut | ios::binary |
ios::app);
if((myFile.rdstate() & ofstream::failbit | ofstream::badbit)!=0)
//write unique marker..
myFile.write ("porytest", 8);
myFile.write (Text.c_str(),Text.length());
if(!myFile) cout << "Write Error..." ;
myFile.close();
}
///////////////////////////////////////////////////////////////////////////////////////////
//////// Test function to see if data was written correctly
///////////////////////////////////////////////////////////////////////////////////////////
void ReadData(std::string binfile)
{
std::ifstream ifstr(binfile.c_str(),std::ios::binary);
std::stringstream temp;
temp << ifstr.rdbuf();
const std::string sentinel("porytest");
const std::string::size_type data_pos(temp.str().find(sentinel,
0)+sentinel.length());
const std::string myText(temp.str().substr(data_pos));
cout << myText;
ifstr.close();
}
Rory.
debug. Below is code that makes a copy of a binary file, appends a
unique ID and string to the copy of the binary file and then checks
the string by searching for the ID which is also a string and
retrieves the text that follows. I'm building the code with scons
along with some other applications that are part of the project. Here
is the strange thing. If I set a new unique string and compile on it's
own my code below works fine. If I then call scons to built all the
project files including the code below it doesn't work. If I then try
to build it on its own again it still doesn't work. The only way to
get it to work again is to change the string ID and compile it again
on its own. I know this is a strictly c++ language list and my post
does refer to scons but all scons does is call the g++ compiler. Can
anyone help me with this problem? Here is my code, although I don't
think the problem is with the code.
////////////////////////////////////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
if(argc<2){
cout << "\nUsage: copy_append_read <csound file> <output filename>
\nExample: cabbage test.txt test.exe\n";
return 0;
}
std::string Text, str;
long begin, end;
//make a copy of binary cabbage.dat with new name
int ret = copyfileC("cabbage.dat", argv[2]);
if(!ret) cerr << "Error: Could not copy/create file, please make
sure that cabbage.dat is located in the same folder as cabbage";
//read contents of input file
ifstream file(argv[1]);
while(!file.eof())
{
getline(file, str);
Text = Text+str;
}
//write identifier and input file string to runtime
WriteData(argv[2], Text);
//test that data was written and can be retreived
ReadData(argv[2]);
}
///////////////////////////////////////////////////////////////////////////////////////////
//////// Make a copy of the runtime binary
///////////////////////////////////////////////////////////////////////////////////////////
int copyfileC(std::string oldFile, std::string newFile)
{
int c;
FILE *in, *out;
in = fopen(oldFile.c_str(), "rb");
out = fopen(newFile.c_str(), "wb");
if(!in && !out)
return 0;
while((c=fgetc(in))!=EOF)
{
fputc(c,out);
if(ferror(out)) cout << "error when writing new file";
}
fclose(in);
fclose(out);
return 1;
}
///////////////////////////////////////////////////////////////////////////////////////////
//////// Append magic marker and Text to the runtime binary
///////////////////////////////////////////////////////////////////////////////////////////
void WriteData(std::string binfile, std::string Text)
{
ofstream myFile (binfile.c_str(), ios:
ios::app);
if((myFile.rdstate() & ofstream::failbit | ofstream::badbit)!=0)
//write unique marker..
myFile.write ("porytest", 8);
myFile.write (Text.c_str(),Text.length());
if(!myFile) cout << "Write Error..." ;
myFile.close();
}
///////////////////////////////////////////////////////////////////////////////////////////
//////// Test function to see if data was written correctly
///////////////////////////////////////////////////////////////////////////////////////////
void ReadData(std::string binfile)
{
std::ifstream ifstr(binfile.c_str(),std::ios::binary);
std::stringstream temp;
temp << ifstr.rdbuf();
const std::string sentinel("porytest");
const std::string::size_type data_pos(temp.str().find(sentinel,
0)+sentinel.length());
const std::string myText(temp.str().substr(data_pos));
cout << myText;
ifstr.close();
}
Rory.