Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

File input and output

shawnlawshawnlaw Posts: 48Member
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;"

";

cout << "

First Name: " << FirstName;
cout << "
Last Name: " << LastName;
cout << "
Enter Age: " << Age;
}
[/red]
cout << "

";
getch ();
return 0;

[/code]

Comments

  • HK_MP5KPDWHK_MP5KPDW Posts: 770Member ✭✭✭
    : 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;"

    ";
    :
    : cout << "

    First Name: " << FirstName;
    : cout << "
    Last Name: " << LastName;
    : cout << "
    Enter Age: " << Age;
    : }
    : [/red]
    : cout << "

    ";
    : getch ();
    : return 0;
    :
    : [/code]
    :


    [green]The problem lies with the line in blue below:[/green]
    [code]
    for (i=0;i<3;i++)
    {
    [blue]ifstream Students(FileName);[/blue]
    Students >> FirstName >> LastName >> Age;"

    ";

    cout << "

    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;"

    ";

    cout << "

    First Name: " << FirstName;
    cout << "
    Last Name: " << LastName;
    cout << "
    Enter Age: " << Age;
    }
    [/code]
  • shawnlawshawnlaw Posts: 48Member
    : : 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;"

    ";
    : :
    : : cout << "

    First Name: " << FirstName;
    : : cout << "
    Last Name: " << LastName;
    : : cout << "
    Enter Age: " << Age;
    : : }
    : : [/red]
    : : cout << "

    ";
    : : getch ();
    : : return 0;
    : :
    : : [/code]
    : :
    :
    :
    : [green]The problem lies with the line in blue below:[/green]
    : [code]
    : for (i=0;i<3;i++)
    : {
    : [blue]ifstream Students(FileName);[/blue]
    : Students >> FirstName >> LastName >> Age;"

    ";
    :
    : cout << "

    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;"

    ";
    :
    : cout << "

    First Name: " << FirstName;
    : cout << "
    Last Name: " << LastName;
    : cout << "
    Enter Age: " << Age;
    : }
    : [/code]
    :
    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;"

    ";
    :
    : cout << "

    First Name: " << FirstName;
    : cout << "
    Last Name: " << LastName;
    : cout << "
    Enter Age: " << Age;
    : }
    : [/code]

    where i<100. letssay i have only s records in my file
    : : [code]
    : : bounty
    : : killa
    : : 28
    : :
    : : beenie
    : : man
    : : 28
    : :
    : : vibez
    : : kartel
    : : 28
    : : [/code]

    will the loop runn this three records a hundre times? i jus thought of this
  • DonotaloDonotalo Posts: 715Member
    : 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;"

    ";
    : :
    : : cout << "

    First Name: " << FirstName;
    : : cout << "
    Last Name: " << LastName;
    : : cout << "
    Enter Age: " << Age;
    : : }
    : : [/code]
    :
    : where i<100. letssay i have only s records in my file
    : : : [code]
    : : : bounty
    : : : killa
    : : : 28
    : : :
    : : : beenie
    : : : man
    : : : 28
    : : :
    : : : vibez
    : : : kartel
    : : : 28
    : : : [/code]
    :
    : will the loop runn this three records a hundre times? i jus thought of this
    :
    [purple]
    it will show the last read items since there is nothing to read when the following line will be executed after all items has been read:

    [b][black]Students >> FirstName >> LastName >> Age;[/b][/black]
    [/purple]
    [hr][purple]~Donotalo()[/purple]

  • shawnlawshawnlaw Posts: 48Member
    : : 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;"

    ";
    : : :
    : : : cout << "

    First Name: " << FirstName;
    : : : cout << "
    Last Name: " << LastName;
    : : : cout << "
    Enter Age: " << Age;
    : : : }
    : : : [/code]
    : :
    : : where i<100. letssay i have only s records in my file
    : : : : [code]
    : : : : bounty
    : : : : killa
    : : : : 28
    : : : :
    : : : : beenie
    : : : : man
    : : : : 28
    : : : :
    : : : : vibez
    : : : : kartel
    : : : : 28
    : : : : [/code]
    : :
    : : will the loop runn this three records a hundre times? i jus thought of this
    : :
    : [purple]
    : it will show the last read items since there is nothing to read when the following line will be executed after all items has been read:
    :
    : [b][black]Students >> FirstName >> LastName >> Age;[/b][/black]
    : [/purple]
    : [hr][purple]~Donotalo()[/purple]
    :
    :

    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

  • HK_MP5KPDWHK_MP5KPDW Posts: 770Member ✭✭✭
    : : : 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;"

    ";
    : : : :
    : : : : cout << "

    First Name: " << FirstName;
    : : : : cout << "
    Last Name: " << LastName;
    : : : : cout << "
    Enter Age: " << Age;
    : : : : }
    : : : : [/code]
    : : :
    : : : where i<100. letssay i have only s records in my file
    : : : : : [code]
    : : : : : bounty
    : : : : : killa
    : : : : : 28
    : : : : :
    : : : : : beenie
    : : : : : man
    : : : : : 28
    : : : : :
    : : : : : vibez
    : : : : : kartel
    : : : : : 28
    : : : : : [/code]
    : : :
    : : : will the loop runn this three records a hundre times? i jus thought of this
    : : :
    : : [purple]
    : : it will show the last read items since there is nothing to read when the following line will be executed after all items has been read:
    : :
    : : [b][black]Students >> FirstName >> LastName >> Age;[/b][/black]
    : : [/purple]
    : : [hr][purple]~Donotalo()[/purple]
    : :
    : :
    :
    : 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]
    ifstream Students(Filename);
    while( Students >> FirstName >> LastName >> Age )
    {
    cout << "

    First Name: " << FirstName;
    cout << "
    Last Name: " << LastName;
    cout << "
    Enter Age: " << Age;
    }
    [/code]
    [/blue]
  • shawnlawshawnlaw Posts: 48Member
    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 << "

    ";
    }
    cout<<"

    Details have been saved";
    cout<<"
    Press enter to continue";


    StaffRecords.close();
    getch();
    [/code]

    ok kool this works.
    lets say i enter this info
    [code]
    tracy ann smith
    16 grass green dive
    kingston 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


    [code]

    [red]
    StaffRecords.getline(fullname, 40, '
    ');
    StaffRecords.getline(Address, 40);
    StaffRecords.getline(City, 20);
    [/red]

    cout << "
    -=- Employee Information -=-";
    cout << "
    full Name: " << fullname;
    cout << "
    Address: " << address;
    cout << "
    City: " << City;

    cout << "

    ";
    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 << "

    First Name: " << FirstName;
    : cout << "
    Last Name: " << LastName;
    : cout << "
    Enter Age: " << Age;
    : }
    : [/code]
    : [/blue]

    so that i can print all the 20 data in the file with out using a for loop.

  • HK_MP5KPDWHK_MP5KPDW Posts: 770Member ✭✭✭
    : 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 << "

    ";
    : }
    : cout<<"

    Details have been saved";
    : cout<<"
    Press enter to continue";
    :
    :
    : StaffRecords.close();
    : getch();
    : [/code]
    :
    : ok kool this works.
    : lets say i enter this info
    : [code]
    : tracy ann smith
    : 16 grass green dive
    : kingston 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
    :
    :
    : [code]
    :
    : [red]
    : StaffRecords.getline(fullname, 40, '
    ');
    : StaffRecords.getline(Address, 40);
    : StaffRecords.getline(City, 20);
    : [/red]
    :
    : cout << "
    -=- Employee Information -=-";
    : cout << "
    full Name: " << fullname;
    : cout << "
    Address: " << address;
    : cout << "
    City: " << City;
    :
    : cout << "

    ";
    : 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 << "

    First Name: " << FirstName;
    : : cout << "
    Last Name: " << LastName;
    : : cout << "
    Enter Age: " << Age;
    : : }
    : : [/code]
    : : [/blue]
    :
    : so that i can print all the 20 data in the file with out using a for loop.
    :
    :

    [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.

    [code]
    struct Student
    {
    string FullName;
    string Address;
    string City;
    int Age;
    };

    // Overloaded stream extraction operator
    istream& operator>>(istream& is, Student& student)
    {
    getline(is,student.FullName);
    getline(is,student.Address);
    getline(is,student.City);
    is >> student.Age;
    is.ignore();
    return is;
    }

    // Overloaded stream insertion operator
    ostream& operator<<(ostream& os, const Student& student)
    {
    os << "Full Name: " << student.FullName;
    os << "
    Address: " << student.Address;
    os << "
    City: " << student.City;
    os << "
    Age: " << student.Age;
    os << "


    ";
    os.flush();
    return os;
    }

    ...

    ifstream StudentFile(FileName);
    if( StudentFile.is_open() )
    {
    Student temp;
    while( StudentFile >> temp ) // Read from StudentFile into temp
    cout << temp; // Write to cout from temp
    }
    [/code]

    That should work if you have a file with contents like:
    [code]tracy ann smith
    16 grass green dive
    kingston 10, jamaica
    22[/code]
    [/blue]
Sign In or Register to comment.