Quantcast

Jump to content

» «
Photo

simplistic file systems in c++

2 replies to this topic
pops capo
  • pops capo

    Punk-ass Bitch

  • Awaiting Authorisation
  • Joined: 03 Jul 2002

#1

Posted 18 May 2005 - 11:41 PM

Okay, I am taking a C++ class this semester. We are assigned packets of problems which we must solve through programming in the aforementioned language. I am stuck on this one particular program. I understand how to fetch and echo a simple plaintext file, but I do not understand how to separate one column of character data from another column of integer (3-digit int, to be exact) data. If I could figure out how to read the 3-digit int then I would be fine.

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:

f 120
m 200
f 150
m 110

et al.

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,
pops

segosa
  • segosa

    Ghetto Star

  • BUSTED!
  • Joined: 15 May 2002

#2

Posted 20 May 2005 - 03:32 PM

Well that's fairly easy, even using low-level pointers (and not iostreams).

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

<m|f><space><salary>

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

CODE
char gender;
char salary[16];


then

CODE
gender = line[0];


will set the gender (compare with 'f' and 'm' like gender=='m', it's just a char, might wanna make it a char* though..)

and

CODE
lstrcpy(salary, line+2);


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. dontgetit.gif

Knightmare
  • Knightmare

    man tongues

  • $outh $ide Hoodz
  • Joined: 18 Nov 2001

#3

Posted 23 May 2005 - 02:31 AM

Haha, although Segosa's solution works, I think the guy may get into some trouble if he busts out pointers in a C++ intro class.

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:

CODE
while(text.txt != EOF)
{
while(ins != ' ')
get gender;
while(ins != ' ')
get salary;
next line
}


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