I am having problems figuring out how many lines of information are in the text file I am supposed to read in. I heard that ifstream automatically skips white spaces, but I have to read in the parts of the line into an array. I have
1 2 3 4 5 6 7 8 9 10 11 12
int i;
int totalMembers = 0;
ifstream fin;
fin.open("Members.txt")
//and error check for file name
for(i=0;i<MAX_MEMBERS;i++) //MAX_MEMBERS is a constant used to define my class array (currently = 100)
{ //member is an object of a class with several parts, including strings, ints, and chars.
fin>>member[i].FirstName>>member[i].LastName; //etc
while(getline(fin,member[i].AmountPaid))
totalMembers++;
}
I need totalMembers to be equal to the number of lines of data I have in the file so that I can add lines to it in another for loop. Currently this compiles, but totalMembers just counts all the way up to MAX_MEMBERS.
What can I do so that totalMembers adds 1 ONLY if there is another line in the txt file?
So I found out there is such thing as a fin.eof();
so that fixed most of my problem. Now I don't know how to delete a single line from a file. What I'm doing now is
open data file
ifstream all data < input line#
open temp file
ofstream all data in my array
ifstream all data > input line#
ofstream data into temp file
close data file
ifstream all data from temp in
open data file
ofstream data back into data file.
but this process isn't ideal. Is there a better way to do this?
Perhaps you should just
a. read all the data into memory, close the file, at program start up.
b. do insert/update/delete operations in memory.
c. open the file, write all the data back into the file, at program exit.