By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,310 Members | 1,430 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,310 IT Pros & Developers. It's quick & easy.

can't understand what's wrong with this function...

P: n/a
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
Aug 2 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
<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
Aug 2 '08 #2

P: n/a
LR
hi*******@gmail.com wrote:
On Aug 2, 10:38 am, hirsh....@gmail.com wrote:
>On Aug 2, 10:21 am, "Jim Langston" <tazmas...@rocketmail.comwrote:
>><hirsh....@gmail.comwrote in message
news:1a**********************************@c65g20 00hsa.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.
Mandatory? Why is this mandatory?

[snip]
>string readLine(){
Open it up. Positioned where?
> 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);
It looks like you're trying to position file (whatever that might be at
this point) to a particular spot, and then.....
> return readLine();

}
I'm not able to really look your code over carefully, but offhand, I'd
say there is a problem with readLineFromAddress. With file. And then...

Before you fix that problem, you may want to consider what will happen
when you invoke readLine().
>int getAddressToWrite(int id){

int lineNum = id % 1000;
1000 is a magic number. What is it?
> int address = ((lineNum-1) * (sizeof(Customer) + 1))+1;
> string line = readLineFromAddress(address);
What happens to line?

> while(!line.empty()){
address += (1000-1) * (sizeof(Customer)+1) + 1;
}
return address;
[snip]
> int fAddress = getAddressToWrite(id); //--->>PROBLEMATIC LINE
[snip]

I think there are other problems with the code. Just for starters, you
may want to try to split the problem up into simpler chunks and see if
you can solve each of them before you put the whole thing together.

LR
Aug 2 '08 #3

P: n/a
Jim Langston wrote:
<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:
Note that none of this code should compile. OP does not qualify
entities in std:: nor does he have a using statemet or declaration.

struct Customer{
}Customer;

#include <string>
#include <sstream>
template <class T>
string stringify(T arg){
error. string not in global namespace
stringstream ss;
error. stringstream not in global namespace

[remainder redacted]
Aug 2 '08 #4

P: n/a
In article <HV*****************@newsfe07.iad>,
"Jim Langston" <ta*******@rocketmail.comwrote:
<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>
missing "using namespace std" or "using std::string" here.
template <class T>
string stringify(T arg){
stringstream ss;
string result;
ss.clear(stringstream::goodbit);
if(!(ss << arg)){

}
ss>>result;
return result;
}
The above is a little odd. If writing to the stringstream fails, then
you shouldn't be reading from it. How about something like this:

if ( ss << arg && ss >result ) {
return result;
}
else
throw something here

#include <fstream>
Put all your includes at the top of the file before any code.
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 "";
Odd output, you just tried to open it, how would telling the user to
open it help?
}
string strA;
getline(file, strA);
What if getline fails? Also 'getline' isn't appropriate for a file that
was opened with the "binary" flag. Lastly, this function will always
return the first line of the file, is that what you wanted?
return strA;
Here the file closes. Makes me wonder why you opened it with the "out"
specifier when you aren't writing anything to it.
}

string readLineFromAddress(int address){
fstream file;
file.seekg(address);
The above will fail. Your 'file' variable hasn't been attached
to a file and opened.
return readLine();
}
I didn't have time to check out the rest of the code, but the above
should be good for starters.

You have too many fstream objects...
Aug 3 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.