C++ Tutorial: Passing file stream objects to functions

preview_player
Показать описание

Quick video to show you how to pass file stream objects (ifstream, ofstream, or fstream) as arguments to functions.

2:18 ⇒ Coding begins

// Learn More //
Learn more about files in C++. Check out my files play list:

Learn more about functions in C++. Check out my functions play list:
Рекомендации по теме
Комментарии
Автор

currently taking a c++ course at ucla, and this man does 100X better explaining concepts than my professor.

elmoco
Автор

Never seen this guy before but he appears to most “gangster appearing” coder i’ve ever seen. I’ve become desensitized by all the whitewashed and average NPC looking coders in the world this man is a good change of pace reminds me of myself.

JESSWURDGAMING
Автор

Can you help in storing those data into arrays
Eg money, tax, name
Are the categories so store the corresponding entries to corresponding arrays.

mohammedsajid
Автор

Sir. i am a student
in this code When i pass stream in search function after while loop(displaying data on screen ) it not work. when i chech it weather file is open or close after passing into function it display close .but when i use this before while loop (before displaying data ) it properly work..
please guide me whats the problem.
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
void search(ifstream & read, string key)
{
if (!read )
{
cout<<"not open";
}
else
{
string word;
while(read>>word)
{
if(key==word)
{
cout<<"\n---->Record found\n\n";
break;
}
}
if(word!=key)
{
cout<<"\n---->Record not Found\n\n";
}
}
}
int main()
{
string reg, name, dep, key;float cgpa, sum_cgpa=0;
ifstream Read;
Read.open("NTU.txt");
if(!Read)
{
cout<<"File Not Open";
}
else
{
while( Read>>reg>>name>>dep>>cgpa)
{
cout<<left<<setw(18)<<reg;
cout<<left<<setw(18)<<name;
cout<<left<<setw(18)<<dep;
cout<<left<<setw(18)<<cgpa;
cout<<endl;
}
cout<<"Enter Key To Search = ";
cin>>key;
search(Read, key);
}
}



THIS IS TXT FILE DATA NAME=NTU.txt
21-NTU-CS-1370 ALI BSCS 2.3
21-NTU-CS-1374 Abdullah BSSE 3.2
21-NTU-CS-1323 rafay BBA 2.4
21-NTU-CS-1323 usman BSTE 3.2
21-NTU-CS-1312 ahmed BSIT 3.4
21-NTU-CS-1370 ali BSCS 3

gamezspoiler
Автор

Could you help me with a project if you have time

michaelmyers
welcome to shbcf.ru