| | |
Problem when generate ID from txt file
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Nov 2009
Posts: 2
Reputation:
Solved Threads: 0
I would like to automatically generated staff number before generated the staff number i would like to check the staff.txt is the staff number exist. If exist then it will get the last's staff number and continue to adding. But the code that i creating it wont work. Please help to me rewrite.
C++ Syntax (Toggle Plain Text)
void staff::setstaffid() { int count = 0; ifstream instaffile("staff.txt", ios::in); if(!instaffile) { ofstream outstaffile("staff.txt", ios::out); outstaffile.close(); ifstream instaffile("staff.txt", ios::in); } instaffile.read(reinterpret_cast<char *>(this), sizeof(staff)); if(instaffile.eof()) //if the staff id is 0 then it will start from 20000 { staffid = 1; return; } else { while(instaffile && !instaffile.eof()) { count++; instaffile.read(reinterpret_cast<char *>(this),sizeof(staff)); } } staffid = 1 + (count); //increment the staff id instaffile.close(); return; }
-7
#2 Nov 3rd, 2009
line 17 is probably the wrong way to determine if the file is empty or not. What you should do is seek to end of file then get the file position.
The loop at lines 24-28 is also wrong
C++ Syntax (Toggle Plain Text)
instaffile.seekg(0, ios::end); size_t sz = instaffile.tellg(); if( sz == 0) { staffid = 1; return; }
The loop at lines 24-28 is also wrong
C++ Syntax (Toggle Plain Text)
instaffile.seekp(0, ios::beg); // go to beginning of tile count = 0; while( instaffile.read(reinterpret_cast<char *>(this),sizeof(staff)) { ++count; }
Last edited by Ancient Dragon; Nov 3rd, 2009 at 1:11 am.
I told Santa what I wanted for Christmas and he washed my mouth out with soap.
![]() |
Similar Threads
- Reading txt file and display record in Data Grid (VB.NET)
- problem in converting txt to binary (C++)
- How to generate a temp file until finish selected then replace to another file. (Shell Scripting)
- help! generate random word function from fiveLetter.txt file (Python)
- excel VBA macro to generate .rtf file (Visual Basic 4 / 5 / 6)
- Txt File (Visual Basic 4 / 5 / 6)
- Writing text from textbox to file... (C++)
- uploading .txt file via phpmyadmin to Mysql (MySQL)
Other Threads in the C++ Forum
- Previous Thread: Computing factorial
- Next Thread: Make C++ Compiler
Views: 191 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






