Old Feb 21, 2004 | 01:41 AM
  #11  
mayonaise's Avatar
mayonaise
Senior Member
 
Joined: Aug 2002
Posts: 3,181
Likes: 0
From: CA
Default

i got it working with these fixes:

first i'd advise you to use separate file descriptors for groupA.txt and groupB.txt, so that they don't just share the same one, fout. this could be the reason you're only getting one file of output.

you're closing your file descriptors at the end of the program, not at the end of the while loop. in this way, you just keep opening them over and over again. the data you wrote to the file will only be commited once you close the file descriptor.

once you've fixed that, you will still probably get one line of output in each file, but that's because the open() function for an ofstream object is set to truncate by default. meaning each time you open a file, if the file exists already, all of its contents will be cleared. you have to figure out how to set an append flag, so that it'll append whatever you want to write to the file, not truncate it.

HOWEVER, if you choose to do it this way, every time you run the program you'll keep appending on to the same groupA.txt and groupB.txt files that already exist from previous executions of the program. so to get rid of this, it's probably a better idea to open the two output files at the beginning of the program (before the while loop), set to truncate, run thru the while loop, and then close the files at the end. so anything that happens in between will just get written to the output files, and you don't really have to worry about opening and closing them a bunch of times - also makes the program more efficient. make sense?

and this is just a suggestion; work on your indentation and bracket placing. your code is impossible to read
Reply