| | |
Problem when generate ID from txt file
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
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 29 Days Ago
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; 29 Days Ago at 1:11 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
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
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






