473,396 Members | 1,961 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,396 software developers and data experts.

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

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
4 1936
<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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: BoonHead, The Lost Philosopher | last post by:
I think the .NET framework is great! It's nice, clean and logical; in contradiction to the old Microsoft. It only saddens me that the new Microsoft still doesn't under stand there own...
9
by: ceo | last post by:
Hi there, I'm reffering to a text that says following: "To summarize: When a copy of an object is generated because it passed to a function, the object's constructor function is not called....
9
by: Carlis | last post by:
I want to make a program that, when you have the mouse on a word, that word is bold... and when you don't have the mouse on that word, the word is not bold. Excuse me for my poor English :-( But...
39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
2
by: None | last post by:
Hello, 1. The prototype of function signal in signal.h: void (*signal(int sig, void (*func)(int)))(int); is some complex to me. Would you please explain it to me in detail with the C...
29
by: Roy Gourgi | last post by:
Hi, I am new to C#. I have the same time scheduling program written in C++ and it is 5 times faster than my version in C#. Why is it so slow as I thought that C# was only a little slower than...
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
5
by: Coll | last post by:
Hi - I have a function that returns a value to a text field. I need to bold a portion of the text field to match up with the rest of the form. I'd rather not split it into two separate text fields...
10
by: TheSaint | last post by:
Hi, It seems to be strange that give me syntax error inside an eval statement. I'm looking at it carefully but I can't see any flaw. Here it's part of the code: for nn in stn_items: value=...
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: 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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.