Tuesday, November 2, 2010

[DSA] File Op

Data need to be saved somewhere. The reason being, if all data are stored in memory and the computer get accidentally turned off. The effect would be epidemic!
Data output using fstream are clear text by default! ENCRYPT any sensitive info before output to file!!!!!

Some text file are delimited in a certain way, for example below.
sample of comma delimited file=> aa,bb,cc
sample of semicol delimited file=>aa;bb;cc

File operations of the program, should be able to read in the lines, and "parse" the input accordingly and stored into the required variable.

This usually happens when legacy software needed an upgrade to the most-IN technology (Programmer Guru Mantra= If it ain't spoil, don't fix it). However, the data are not compatible. One common feature of software, it has the ability to "dump" data out of the system.

As as programmer, we need to write code to extract those that required to the new system. Therefore, we still get to keep our jobs, make ourself valuable.

Regarding on the issue of Out Sourcing to other country. Fret Not. Why would the company would entrust their data to a 3rd party other then their employer? The audit trail of hands dipped into the data would be clearer and easier to trace. Hence the accountability.

With Great monies, comes great responsibilities.

Talking about file IO and security, take a look at the game below.

I played the game when i was still a teenager like most of you. My friend challenged me to complete the game (eg Lvl 99) in the shortest possible time. Few of us, being kiasu (hokkien colloquial for afraid to loose), keep comparing each other's progress. Me, for being me went to my usual corner and gave my friend Lvl99 at the end of the day. Guess how I manage to get it done????

The sample of file operations are in the writescore.cpp and readscore.cpp. Compile and run it to get a feel of how File I/O is done.

Drill Questions
1. Write a program that READS IN records (eg A.txt) that are semicol delimited eg
tom 19 dcpe/2B/01 DSA B+;kelly 18 dcpe/2B/01 DSA A; .....

2. Write a program that WRITE OUT records (eg B.txt) that are semicol delimited eg
tom 19 dcpe/2B/01 DSA B+; kelly 18 dcpe/2B/01 DSA A; ....

3. write a program that reads in Q1 but modifies to comma delimited and output to a new file.

HINT: It is good to have a menu driven system to choose between the options instead of running 2 cpp projects concurrently.


Edwin - 2B05 said...


- code seems to be correct except when i run it, it adds an additional value. eg: instead of the 8 inputs in the file, it calculates the average of 9 inputs -

HY said...

Hmm, I am just guessing but the problem is caused by the fact that the pointer is not at the end of the file yet. The eof() function checks if the pointer is currently pointing at a end of file character. It doesn't check the next character, and therefore when the pointer moves at the next loop, it will cause an extra count. You might want to try peek() and check for '-1'.

HY said...


"while (ifile.peek() != -1)"

This is what I used to check for end of file.

Anonymous said...

If u wanna badmouth me pls do it to my face fatboi

yao de DCPE 2B03 said...


heng gnee dcpe/2b03 said...

Class assignment of 4.11.2010


XuZhou said...


Felix (DCPE 2b03) said...


Li Xiaofei DCPE 2B03 said...


Qi Hao(DCPE/2B/03) said...


sjteo said...

@edwin, the whille loop incr the counter ++ then chks for EOF. hence the extra count. use counter -1 after the while loop or peek which hy suggested.

@anonymous, post your name. who bad mouth what? i am confused.

@qihao, i cant read ur code. quote me the link instead.

@guys, i noticed you people hardcoded the total # of students in the class. by doing this.. the code is not robust and adapts. eg if i add or reduce the entry... the code will break giving wrong outcome.

Yi Wei said...


Han Yang said...

u are? As far as i know the time the first time i did not attend one of ur lessons in 3 weeks u had to say unpleasant things about me to the whole class. If u are unhappy about whatever just let me know. Trash talking about others when they're not around is kinda low.

Han Yang said...

typo, ignore the first "the time".

sjteo said...

@hanyang, i only broadcast to the class (my other classes too) "if any of you do not attend/keep skipping my class, good luck in lab test" quoted ad verbatim.

apparently you did not attend my class and heard it from your "friends". at the last 8am session, When I discussed D&I stuff with your friends, we did cite the example of students passing message from one to another. The message received at the last in the chain going to be very different from where it originates.

if you not happy with anything in my lesson, which i can modify, let me know. BUT NOT TO THE EXTEND of affecting the WHOLE class and other classes.

calling names in public is kinda low.

Han Yang said...

hmm are u sure? because i've pretty much confirmed with other classmates about the stuff u said. What i am talking about has nothing to do with lab test. what about the "where the 2 think they very zai one? one fat one and one skinny one" im not sure if this is what u said word for word, but i'm positive it is something along those lines. So if u don't even rmb what u say about ur own students..nuff said

Han Yang said...

Just like how u do not like to be called fat neither do we like to be distinguished by our physical features. If u do not "rmb" what u said about said about us then do not bother replying as we have nothing to discuss.

sjteo said...

@han yang,
I am in a middle of another DSA class, yet I am taking time to reply to u.

can we discuss this in the class, also include the "classmates" you asserted previously. I just want to make sure the data is consistent.

I apologies for using fat/skinny to describe. It will take me sometime to remember all of your name. For now, I do remember yours.

just to add,
"HY" that is replying to the programming questions you guys posting is my last batch DSA star programming student. His name is "Hong Yi"

We shall keep this programming blog a strictly academic ones. For the benefit of all of my programming class students.