Comments

: sup programmer dudes. anyway i recently learned about File Input and Output. i understand it pretty much. however i have a problem. look at this code. highlight the part in red where i need help with. : : lets say in the file it has these names: [code]: bounty: killa: 28: : beenie: man: 28: : vibez: kartel: 28: [/code]: : how can i display all of them in the program in the part in red, what it does is it displays : [code]: bounty: killa: 28: [/code]: : three time. how can i do it so that it display all the names after each other. (is it possible).: : : : : [code]: : char FirstName[30], LastName[30];: int Age, i;: char FileName[20];: : cout << "Enter the name of the file you want to open: ";: cin >> FileName;: : : [red]: for (i=0;i<3;i++): {: ifstream Students(FileName);: Students >> FirstName >> LastName >> Age;"

First Name: " << FirstName; cout << "Last Name: " << LastName; cout << "Enter Age: " << Age;}[/code][green]Because the file stream object is local to the block of code containing the loop, it gets destroyed at the end of each loop and reopened at the beginning of each loop. Each time the file is reopened, the "get" pointer is set to point to the beginning of the file and your input operations just end up getting the first "record" from the file. The solution to this is to open up the file only once and this can be done by moving the instantiation of the Students file stream object outside of (and before) the [italic]for[/italic] loop:[/green]

First Name: " << FirstName;: cout << "Last Name: " << LastName;: cout << "Enter Age: " << Age;: }: [/code]: [green]Because the file stream object is local to the block of code containing the loop, it gets destroyed at the end of each loop and reopened at the beginning of each loop. Each time the file is reopened, the "get" pointer is set to point to the beginning of the file and your input operations just end up getting the first "record" from the file. The solution to this is to open up the file only once and this can be done by moving the instantiation of the Students file stream object outside of (and before) the [italic]for[/italic] loop:[/green]: : [code]: [blue]ifstream Students(FileName);[/blue]: for (i=0;i<3;i++): {: Students >> FirstName >> LastName >> Age;"

: ok thanks. the pc iam using doesnt have a complier so i couldnt check it, but i trust ur right. i have a next question: : what if i have this: : : [code]: : [blue]ifstream Students(FileName);[/blue]: : for (i=0;i<100;i++): : {: : Students >> FirstName >> LastName >> Age;"

i jus got the chance to test it. wat it does is it runs the first three record the it shows[b][black]Students >> FirstName >> LastName >> Age;[/b][/black]97 times.. that is jus wack. wat can i do to edit it so that the loop only show the available record. what i want is that wen the file is called to be open it only show the records in it. can u give me some advice

[blue]I would suggest making the read operation a part of the loop condition and changing the loop to a [italic]while[/italic] loop. Then you can basically say "loop while there is still data to be read from the file":

[code]tracy annsmith 22[/code]i notice that because of the space between tracy ann the program will not print out the line as the fist name. it will print tracy as the first name and ann as the last name and then it will print smith as the age

i found a solution to this.[code]The following technique can be used to save complete but separate lines of text using the "ws" feature on Microsoft Windows. [/code]

ok kool this works. lets say i enter this info[code]tracy ann smith16 grass green divekingston 10, jamaica[/code]you notice that each line has spave bettewn the words. if i never use the ws format i would be able to print it correctly as i staed above. this is how it is printed in this format

kool i can appreciate that. but again i ask a question what if i had 20 data of staff in a file there is no way i can put a while loop on the part i highlighted in red. is there a way i could do it jus as how this loop is

: yeah thanks. That work. dont know why i never thought about that...: i was reading a couple web sites about files and i found some interesting ways of saving files..: lets say i use this examaple: : : [code]: : ifstream Students(Filename);: : while( Students >> FirstName >> LastName >> Age ): : {: : cout << "

First Name: " << FirstName;: : cout << "Last Name: " << LastName;: : cout << "Enter Age: " << Age;: : }: : [/code]: : [/blue]: : what if when i was entering the fist name of a person i did this.: : [code]: tracy ann: smith : 22: [/code]: i notice that because of the space between tracy ann the program will not print out the line as the fist name. it will print tracy as the first name and ann as the last name and then it will print smith as the age: : i found a solution to this.: [code]: The following technique can be used to save complete but separate lines of text using the "ws" feature on Microsoft Windows. : [/code]: : : here my example: : [code] cout<<":::--> Full Name: ";cin >> ws;: cin.getline(fullname, 40);: : cout<<":::--> Address: ";cin >> ws;: cin.getline(address, 40);: : cout<<":::--> City: ";: cin.getline(city, 20);: : : {: : StaffRecords << fullname << "" << address << "" << city << "

";: getch ();: [/code]: : kool i can appreciate that. but again i ask a question what if i had 20 data of staff in a file there is no way i can put a while loop on the part i highlighted in red. : is there a way i could do it jus as how this loop is: : : [code]: : ifstream Students(Filename);: : while( Students >> FirstName >> LastName >> Age ): : {: : cout << "

[blue]1. Create a struct/class to hold your data.2. Overload the stream extraction operator (>>), put the [italic]getline[/italic] commands within this overloaded operator.3. Use the overloaded stream extraction operator (>>) to extract from the file into an instance of the struct/class.4. You might even want to overload the stream insertion operator (<<)to simplfy output as well.