[TriLUG] OT: String.h in g++ around Redhat 5.0
Guthrie, Joshua L CIV NAVAIRDEPOT
joshua.guthrie at navy.mil
Fri Jun 16 07:46:46 EDT 2006
Thanks for your response....
First, let me explain, I'm no programmer by any means.... I know just
enough to be dangerous...
Here's the code that I got working on a more current G++
//blaa blaa......
#include <iostream>
#include <fstream>
#include <String>
//#include <sstream> not used after much anguish... not implemented in
std C++ on
//this compiler
#include <stdlib.h>
using std::ios;
using namespace std;
//blaa blaa
int incrementSerial(int serialNumToIncrement, int lineNumber);
// will increment the rdg value of serialNumToIncrement by one
//returns the new rdg number
int main(int argc, char** argv){
int serialNum;
int lineNumber;
serialNum = atoi(argv[1]); //converts the c style string (argument)
//into an interger
if (areAugumentsValid(argc, serialNum) == false){ //check to see if
we are
//supplying correct num of
arguments
return -1; //something is screwed up -- exiting
} //otherwise, we keep on moving
lineNumber=findSerial(serialNum); //runs findSerial and puts the
line
//number into line Number
if (lineNumber == -1){ //findSeial will return -1 if the serial
number
//does not exist...
addNewSerialNumberToFile(serialNum); //add the serialNum
return 1;
}
else{ //otherwise, we have a previously entered serial number we
need to ++
return incrementSerial(serialNum, lineNumber); //returns the
new
//rdg number to
the OS
}
return -1; //something went wrong if we make it here
}
//blaa blaa....
int incrementSerial(int serialNumToIncrement, int lineNumber){
ifstream inStream;
inStream.open(PATH2FILE);
int currentLine=1;
int serialNumber =-1;
int rdgNumber =-1;
string tempFileString;
string line;
string stringSerial;
string stringRdg;
char bufSerial[32];
char bufRdg[32];
if (lineNumber < 1 ) {
cout << "Invalid Line number passed to incrementSerial() in
rdg_increment"<<endl;
}
while (currentLine < lineNumber && getline(inStream, line)){
//scrolls thru the file until we get to the current
line
//or getline returns some error
currentLine++;
tempFileString += line; //we make a temp buffer and put the
contents
//of line into it at the end..
//we are building a new file to replace
the old
//file with... nothing changes until we
get to
//the line number that contains the
serial num
tempFileString += "\n"; //we add a new line character at the
end
}
getline(inStream, line); //we now get the line with the serial
number
serialNumber = extractSerialNumberFromString(line); //we extract
the serial
rdgNumber = extractRdgNumberFromString (line); //and rdg from
the line
if (serialNumToIncrement != serialNumber ) { //we do some simple
error check
cout <<"error finding serial number to replace in
rdg_increment"<<
"\nincrementSerial()"<<endl;
return -1;
}
rdgNumber++; //we increment the rdg number
sprintf(bufSerial, "%d", serialNumber);
stringSerial = bufSerial;
sprintf(bufRdg, "%d", rdgNumber);
stringRdg = bufRdg;
tempFileString += stringSerial; //we now add to our temp buffer
tempFileString += '\t';
tempFileString += stringRdg;
tempFileString += '\n';
while (getline(inStream, line)){ //we finish putting the contents of
the
//file into our temp buffer
tempFileString += line;
tempFileString += "\n";
}
inStream.close();
ofstream outStream;
outStream.open(PATH2FILE);
outStream<<tempFileString; //we override our original file with the
//contents of our temp buffer
outStream.close();
return rdgNumber; //we return rdgNumber to calling function
}
//blaa blaa
///////////////
The above code... compiled, worked, etc...
Below is my attempt to change things to try to work with the older
compiler compiler....
///////////
#include <iostream.h>
#include <fstream.h>
#include <String.h>
//#include <sstream> not used after much anguish... not implemented in
std C++ on
//this compiler
#include <stdlib.h>
//using std::iso;
//using namespace std;
int incrementSerial(int serialNumToIncrement, int lineNumber);
// will increment the rdg value of serialNumToIncrement by one
//returns the new rdg number
int main(int argc, char** argv){
int serialNum;
int lineNumber;
serialNum = atoi(argv[1]); //converts the c style string (argument)
//into an interger
if (areAugumentsValid(argc, serialNum) == false){ //check to see if
we are
//supplying correct num of
arguments
return -1; //something is screwed up -- exiting
} //otherwise, we keep on moving
lineNumber=findSerial(serialNum); //runs findSerial and puts the
line
//number into line Number
if (lineNumber == -1){ //findSeial will return -1 if the serial
number
//does not exist...
addNewSerialNumberToFile(serialNum); //add the serialNum
return 1;
}
else{ //otherwise, we have a previously entered serial number we
need to ++
return incrementSerial(serialNum, lineNumber); //returns the
new
//rdg number to
the OS
}
return -1; //something went wrong if we make it here
}
int incrementSerial(int serialNumToIncrement, int lineNumber){
ifstream inStream;
inStream.open(PATH2FILE);
int currentLine=1;
int serialNumber =-1;
int rdgNumber =-1;
String tempFileString;
String line;
String stringSerial;
String stringRdg;
char bufSerial[32];
char bufRdg[32];
if (lineNumber < 1 ) {
cout << "Invalid Line number passed to incrementSerial() in
rdg_increment"<<endl;
}
while (currentLine < lineNumber && getline(inStream, line)){
//scrolls thru the file until we get to the current
line
//or getline returns some error
currentLine++;
tempFileString += line; //we make a temp buffer and put the
contents
//of line into it at the end..
//we are building a new file to replace
the old
//file with... nothing changes until we
get to
//the line number that contains the
serial num
tempFileString += "\n"; //we add a new line character at the
end
}
getline(inStream, line); //we now get the line with the serial
number
serialNumber = extractSerialNumberFromString(line); //we extract
the serial
rdgNumber = extractRdgNumberFromString (line); //and rdg from
the line
if (serialNumToIncrement != serialNumber ) { //we do some simple
error check
cout <<"error finding serial number to replace in
rdg_increment"<<
"\nincrementSerial()"<<endl;
return -1;
}
rdgNumber++; //we increment the rdg number
sprintf(bufSerial, "%d", serialNumber);
stringSerial = bufSerial;
sprintf(bufRdg, "%d", rdgNumber);
stringRdg = bufRdg;
tempFileString += stringSerial; //we now add to our temp buffer
tempFileString += '\t';
tempFileString += stringRdg;
tempFileString += '\n';
while (getline(inStream, line)){ //we finish putting the contents of
the
//file into our temp buffer
tempFileString += line;
tempFileString += "\n";
}
inStream.close();
ofstream outStream;
outStream.open(PATH2FILE);
outStream<<tempFileString; //we override our original file with the
//contents of our temp buffer
outStream.close();
return rdgNumber; //we return rdgNumber to calling function
}
///////////////////
This that stood out for me having to do was to put .h's behing the
#includes...
using std::iso;
using namespace std;
gave me the reassuring warning:
Warning: namespaces are mostly broken in this version of G++
So I commented them out..
I complile and get a crap load of warning:
Cannot pass objects of type 'ifstream' through '...'
And
Cannot pass objects of type 'String' through '...'
The compiler didn't like me declaring string but seemed to like
String better... so I replaced my string (s) with String...
I get errors:
No member function 'String::find(char)' defined
No member function 'String::erase(int, unsigned int)' defined
No member function 'String::c_str()' defined
These seem to be on lines that I use the data type String...
My namespace warning, and my inability to find a sstream.h, or
get String to work started to make me wonder if I had slipped into a
twilight zone of comiler history in linux...
Sorry for the long post :-(
And thanks!!
-josh
More information about the TriLUG
mailing list