473,385 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

inconsistencies when compiling

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::out | 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.

Jan 24 '08 #1
9 1486
Thanks Alf, that's a start anyhow, I will tidy things up and see if it
helps.
Jan 24 '08 #2
I have looked into this problem further and the only thing that scons
does different when compiling it is that it creates and object file
first. I don't do that and it works fine, but if I do g++ -c test.cpp
and then g++ test.o -o test.exe it no longer works. Anyone know why
this is?
Jan 24 '08 #3
On Jan 24, 6:22*am, rory <rorywa...@gmail.comwrote:
[snip]
If I then call scons to built all the
project files including the code below it doesn't work.
Since you didn't mention in what way it "doesn't work"
it's pretty difficult to diagnose.

Still, it seems unlikely to be a language issue, so you
would seem to be posting your incomplete messages in the
wrong news group.
Socks
Jan 24 '08 #4
On Jan 24, 4:39 pm, Puppet_Sock <puppet_s...@hotmail.comwrote:
On Jan 24, 6:22 am, rory <rorywa...@gmail.comwrote:
[snip]
If I then call scons to built all the
project files including the code below it doesn't work.

Since you didn't mention in what way it "doesn't work"
it's pretty difficult to diagnose.

Still, it seems unlikely to be a language issue, so you
would seem to be posting your incomplete messages in the
wrong news group.
Socks
Sorry if I wasn't clear but when it doesn't work it does not return
the correct string that follows the unique ID. Instead it returns
rubbish characters in an endless loop beeping all the way? I running
windows and MinGW. Excuse my ignorance, it's clear I'm not an
experienced programmer, but if it is not a language issue what else
could it be?

Rory.
Rory.
Jan 24 '08 #5
I've tidied up my code as Alf suggested but there is still something
not right. I've tried to narrow it down as much as possible and here
is the current situation. I am building two application. When I
compile and build the code presented above it works, i.e., it finds
the unique ID and then prints out the code that follows. But when I
then compile and build the other application which resides in the same
folder the code above no longer works, it find the unique string ok
but then it starts spitting our rubbish characters in a endless loop.
How can one application affect another when they are not linked in any
way? Can anyone spot any memory problems with the code above?

Rory.
Jan 24 '08 #6
The problem was occurring because the data file I was copying also
contains a string which matched the unique ID. When I ran my program
and searched for the ID it found it somewhere else in the file and
then started spitting out funny characters. Thanks for the help Alf.

Rory.
Jan 24 '08 #7
On Jan 24, 12:53 pm, "Alf P. Steinbach" <al...@start.nowrote:
* 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
^^^^^^

[...]
//read contents of input file
ifstream file(argv[1]);
He doubtlessly needs to specify binary mode. (In a larger
application, he' probably want to imbue the "C" locale as well.)
while(!file.eof())
And as we know, this is NOT the way to read all of a file.
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 25 '08 #8
On Jan 25, 12:01 pm, "Alf P. Steinbach" <al...@start.nowrote:
* James Kanze:
On Jan 24, 12:53 pm, "Alf P. Steinbach" <al...@start.nowrote:
* 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
^^^^^^
[...]
> //read contents of input file
ifstream file(argv[1]);
He doubtlessly needs to specify binary mode. (In a larger
application, he' probably want to imbue the "C" locale as well.)
I don't think binary mode is intended, because he treats the
file as a text file, using std::getline.
That's true, but he does SAY binary. I'm supposing that getline
is the error, but of course, we don't really know.
> while(!file.eof())
And as we know, this is NOT the way to read all of a file.
Yes, I commented on that two lines further down in the code, at the
unchecked call to std::getline.
Fixing that call would lead to fixing the loop condition as well.
If he fixes it correctly, yes. It depends on how he fixes it.

I'm really thinking that someone should write up a beginner's
guide to using iostream, with the standard idioms, and post it
somewhere so we could point to it.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jan 25 '08 #9
In article <05a2f2d6-a2a5-4a32-9830-
fa**********@s8g2000prg.googlegroups.com>, ja*********@gmail.com says...

[ ... ]
I'm really thinking that someone should write up a beginner's
guide to using iostream, with the standard idioms, and post it
somewhere so we could point to it.
Let's see. Reading input:

std::copy(std::istream_iterator<some_type>(instrea m),
std::istream_iterator<some_type>(),
std::back_inserter(your_collection));

Writing output:

std::copy(your_collection.begin(), your_collection.end(),
std::ostream_iterator<some_type>(outstream, "\t"));

Covers an amazing percentage in two statements... :-)

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jan 26 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: John L | last post by:
Thanks in advance... I have an image control into which I load a .tif image, which loads fine... And when I select a rectangle to zoom down to , and click on a cmdButton ("ZOOM IN"), I zoom to...
2
by: Anand S Bisen | last post by:
Hello I have been developing a code that works pretty well on my python 2.3 and now when i am running it on my server where it is programmed to run it's giving me errors. I have been using...
2
by: Ethel Aardvark | last post by:
I have a query which runs fine in SQL*Plus but which will not compile into a packaged procedure (claiming that the table can not be found): SELECT DISTINCT Folder_ID INTO l_RootID -- remove...
10
by: Roger Withnell | last post by:
I seem to spend far too much of my time struggling with browser inconsistencies concerning Javascript (not to mention CSS). What do you think is the most efficient development regime, including...
4
by: Aaron Queenan | last post by:
When I build a C++ library to .NET using the managed C++ compiler, I get the following error message: Linking... LINK : error LNK2020: unresolved token (0A000005) _CrtDbgReport LINK : error...
10
by: Christina N | last post by:
When compiling my ASP.Net application, VS puts the new DLL under the local cached directory 'VSWebCache' in stead of on the server. How can I make it save the DLL file on the server when compiling?...
2
by: Samuel R. Neff | last post by:
Within the past few weeks we've been getting a lot of compiler errors in two classes when no errors actually exist. The error always reports as Name '_stepResizeRelocator' is not declared. ...
8
by: Darren Dale | last post by:
I was just searching for some guidance on how to name packages and modules, and discovered some inconsistencies on the www.python.org. http://www.python.org/doc/essays/styleguide.html says "Module...
2
by: John Hanley | last post by:
I am getting some inconsistencies with mktime(). I allocate memory for my struct tm early in my program, and assign only *some* of the member variables. t->tm_sec=s; t->tm_min=m;...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.