•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,515 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,741 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 916 | Replies: 4
![]() |
•
•
Join Date: Sep 2007
Location: Colombo, Sri Lanka
Posts: 184
Reputation:
Rep Power: 2
Solved Threads: 2
Hi all,
I want to use a function to write some values to a text file. That function is called from the main function. Every time when I can that function from main, the perviously written value is overwrite. That's true, it wont work.
So what I have done is open a file globally and use it inside the function. My question is at that time in main function the calling of it not looping. Only one time write values. What can be caused to this issue. Here is the basic view of it.
I want to use a function to write some values to a text file. That function is called from the main function. Every time when I can that function from main, the perviously written value is overwrite. That's true, it wont work.
So what I have done is open a file globally and use it inside the function. My question is at that time in main function the calling of it not looping. Only one time write values. What can be caused to this issue. Here is the basic view of it.
using namespace std ;
// Globale file to record exact data
ofstream filedata ;
struct pac_cont
{
unsigned int des_list ; // Destination list
unsigned int mem_ID ; // Member ID
unsigned char dm_con ; // Data/Message control
unsigned char ser_ID ; // Service ID
unsigned short act ; // Activity
};
void dataExtract(int length, char *buffer)
{
struct pac_cont* p = (struct pac_cont*)buffer;
// processing the buffer
filedata.open( "RecordData.txt" ) ;
if(filedata.is_open())
{
filedata << p->des_list << "\t" << p->mem_ID << "\t" << p->dm_con << "\t"
<< p->ser_ID << "\t" << p->act << "\n";
filedata.close() ;
}
else
{
filedata << "Wrong\n" ;
}
}
int main ()
{
while(!fileopen.eof())
{
fileopen.read(buffer, 4); // Dummy read of 4 bytes
fileopen.read(buffer, 4) ; // Read next 4 bytes
tmp_len = *(unsigned int*)buffer ; // int value of 4 bytes
if(tmp_len <= 5000)
{
fileopen.read(buffer, tmp_len) ;
filerecord << ID << "\t\t" << tmp_len << "\t\t" << "-\n" ;
if(tmp_len > 16)
{
dataExtract(tmp_len, buffer) ;
}
else
{
cout << "Stream is in wrong length\n" ;
}
}
else
{
filerecord << ID << "\t\t" << tmp_len << "\t\t" << "Large Size\n" ;
fileopen.seekg(tmp_len,ios_base::cur);
}
ID++ ;
count_mess++ ;
}
cout << "Number of streams: " << ID << endl ;
cout << "Number of messages: " << (count_mess-1) << endl ;
fileopen.close() ;
filerecord.close() ;
cin.get() ;
return 0 ;
} Last edited by eranga262154 : Sep 27th, 2007 at 2:43 am.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
•
•
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,541
Reputation:
Rep Power: 40
Solved Threads: 972
That appears to be only part of your program because main() looks incomplete.
>>That's true, it wont work.
It will work if you use the correct flags. Add std::ate as the second argument to the open function. Read this for more information
>>Only one time write values
No idea because the code you posted does not write anything to the file in main() nor does main() ever call dataExtract().
>>That's true, it wont work.
It will work if you use the correct flags. Add std::ate as the second argument to the open function. Read this for more information
>>Only one time write values
No idea because the code you posted does not write anything to the file in main() nor does main() ever call dataExtract().
•
•
Join Date: Aug 2007
Location: South Dakota
Posts: 993
Reputation:
Rep Power: 6
Solved Threads: 97
If filedata is to be opened and closed in the dataExtract() function, it need not be a global, declare inside the function. As mentioned, use the appropriate file mode to set the writing action to occur at end of existing content (I'd use ios::app ).
Alternatively, you could open the file in main( ), pass the opened file handle to the function. Note that file handles MUST be passed by reference.
Why are you passing the data in a char* buffer? Why not just use your struct as the data type of the parameter?
Val
Alternatively, you could open the file in main( ), pass the opened file handle to the function. Note that file handles MUST be passed by reference.
Why are you passing the data in a char* buffer? Why not just use your struct as the data type of the parameter?
Val
•
•
Join Date: Sep 2007
Location: Colombo, Sri Lanka
Posts: 184
Reputation:
Rep Power: 2
Solved Threads: 2
•
•
•
•
That appears to be only part of your program because main() looks incomplete.
Yes, it is a part of the code.
•
•
•
•
It will work if you use the correct flags. Add std::ate as the second argument to the open function. Read this for more information
Thanks for the link. Actually I've refer the same page to do this. When I used std::ate it wont work. I try to append the data using std::app. It works, but I think it is not efficient, because after writing each line close the file. Isn't it?
•
•
•
•
>>Only one time write values
No idea because the code you posted does not write anything to the file in main() nor does main() ever call dataExtract().
I'm not clear you. You say that my code is nothing done. If you think that better to put full code here, I'll do it.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
•
•
Join Date: Sep 2007
Location: Colombo, Sri Lanka
Posts: 184
Reputation:
Rep Power: 2
Solved Threads: 2
•
•
•
•
If filedata is to be opened and closed in the dataExtract() function, it need not be a global, declare inside the function. As mentioned, use the appropriate file mode to set the writing action to occur at end of existing content (I'd use ios::app ).
Just go through my above post, I think it is not efficient at all.
•
•
•
•
Why are you passing the data in a char* buffer? Why not just use your struct as the data type of the parameter?
What's wrong there. Actually I've work with a binary file. Extract the streams of bytes, some of them are too short and some of them are too long. Using a buffer it is so easy, what I feel.
“victory breeds hatred, the defeated live in pain; happily the peaceful live giving up victory and defeat” - Gautama Buddha
![]() |
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
3d apple blue screen cocoa computer crash dell development drivers email enterprise eudora firefox framework games gnu graphics ibm intel internet java linux microsoft microsystems mozilla news next open open source open-source opengl openoffice operating os penelope programming red hat rhel server software source step sun super system ubuntu vista wesnoth windows xp
- Code of File open dialogue box (Visual Basic 4 / 5 / 6)
- How a doc file can be open inside browser without prompting the download dialog box (ASP.NET)
- Where is my file open notification in excel 2000? (Windows Software)
- file open dialog box control in asp.net (ASP.NET)
- opening files with read and write access (PHP)
- file open code in VB (Visual Basic 4 / 5 / 6)
- Generation html in perl (Perl)
- need help... how to file open client path text.. (PHP)
Other Threads in the C++ Forum
- Previous Thread: postfix expression evaluation
- Next Thread: constructors and arrays



Linear Mode