<hi*******@gmail.comwrote in message
news:1a**********************************@c65g2000 hsa.googlegroups.com...
>i have a functions that writes information to a file.
inside that function i have a line in which i call another function.
if this line is executed, nothing is written to the file, but if i
remark that line, it is written.
i wanna understand what's wrong and fix it, but can't find what is
wrong.
also this line is mandatory in my code.
the code attached is in the link, minimized to the needed info, a bit
different then the way it's written
in my env.
i work in debian linux, IDE = code::blocks
thanks you a lot
CODE -- http://codepad.org/5DWT76mk
It is better to post the code if you can to the message so people don't have
to follow links. Your code is short enough so that can be done. I am
pasting your code here so others won't have to link to it:
struct Customer{
}Customer;
#include <string>
#include <sstream>
template <class T>
string stringify(T arg){
stringstream ss;
string result;
ss.clear(stringstream::goodbit);
if(!(ss << arg)){
}
ss>>result;
return result;
}
#include <fstream>
string readLine(){
fstream file;
file.open("filename.txt", ios::in | ios::out | ios::binary);
if(!file.is_open()){
cout << "open a file prior to reading it\n(File::readLine())" <<
endl;
return "";
}
string strA;
getline(file, strA);
return strA;
}
string readLineFromAddress(int address){
fstream file;
file.seekg(address);
return readLine();
}
int getAddressToWrite(int id){
int lineNum = id % 1000;
int address = ((lineNum-1) * (sizeof(Customer) + 1))+1;
string line = readLineFromAddress(address);
while(!line.empty()){
address += (1000-1) * (sizeof(Customer)+1) + 1;
}
return address;
}
bool writeLine(int address, string toWrite){
fstream file;
file.seekp(address);
file << toWrite << endl;
return true;
}
int id = 34500;
//int fAddress = getAddressToWrite(id);// -the problematic line;
#include <ctime>
#include <string>
void addCustomer(int id, string fName, string lName, string address, string
phone, int acctPeriod){
int fAddress = getAddressToWrite(id); //--->>PROBLEMATIC LINE
time_t rawTime;
struct tm* timeInfo;
char buffer[80];
time(&rawTime);
timeInfo = localtime(&rawTime);
strftime(buffer, 80, "%B", timeInfo);
string acctCreation(buffer);
string customerString;
customerString += stringify(id);
customerString += fName;
customerString += lName;
customerString += address;
customerString += phone;
customerString += acctCreation;
customerString += stringify(acctPeriod);
customerString += stringify(0);
fAddress = ios::beg;//to remove
writeLine(fAddress, customerString);
}
---------------------------------
Now, the issue I see you are attempting to initialize a variable with a
function call outside of a function block. I.E.
int fAddress = getAddressToWrite(id);
getAddressToWrite(id);
is a function call. I believe that is the source of your problem. I can't
quote chapter and verse, but if you put it inside of a function, such as
main, the problem might go away.
Now, the "output" you describe is:
In function `_start':
undefined reference to `main'
This is a different problem all together and should have nothing to do with
the intilaization of fAddress. The problem is, you don't have a main
function in your program. The entry point for C and C++ program is main.
I.E.
int main()
{
// My code here
}
Which is where the program starts to execute. You do not have that in your
program so it is an incomplete program and will not compile.
I'm confused as to what your actual error is, but it seems you have at least
2 you need to fix. That is with a perfunctionary examination of your
program, there may be other problems/errors.
--
Regards,
Jim Langston