simplistic file systems in c++
Posted 18 May 2005 - 11:41 PM
To be exact, the prompt asks to make a program which will read a list of employees' sex and salary.
The format of the data file is basically this:
What I need to figure out (and need help on), is how to separate the f or m from the following integers by using the blank space. I tried a few things, like an array, but it did not work. Maybe I am thinking too simply, but this is a simple class.
We have no apstring.h library also, unfortunately, and we must use the normal iostream/fstream/math/string.h files if anything, since that's basically what we learned. Or, rather, those are the libraries in which the commands to do this can be found.
Thanks in advance,
Posted 20 May 2005 - 03:32 PM
I don't use iostreams and just use plain char*s and pointer manipulation, so sorry if what I say can't be used in your problem.
strchr will take a char* and char and return the index of the char within the char*, but if you know the format of the file is
then you can take advantage of knowing that the third character will start the salary, and not even need to use strchr.
So, if you loop through each line in the file, and say that the variable "line" will contain each line in turn..
if you have
| char gender;|
|gender = line;|
will set the gender (compare with 'f' and 'm' like gender=='m', it's just a char, might wanna make it a char* though..)
will copy the rest of the line (starting at position 2) into the salary variable (can be converted to int with atoi() or something).
Well, hope that helps somewhat.
Posted 23 May 2005 - 02:31 AM
I forget the syntax, but basically you want to read the gender, read the space, then read the number until you find another, probably with while loops.
I'll try to demonstrate with some pseudocode:
| while(text.txt != EOF)|
while(ins != ' ')
while(ins != ' ')
Once again, I haven't used instreams in 2 years, sorry about the syntax. I had an assignment sorta like this that I can try to dig up.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users