C++ Tutorial: Read from Text Files // Retrieve data from files using ifstream

  Рет қаралды 26,442

Professor Hank Stalica

Professor Hank Stalica

Күн бұрын

In this video, I'm going to show you how to do some simple input from text files.
You already know how! Reading from a text file is just like reading input from the screen using cin, just a little different. Let's see how to do it!
2:34 ⇒ Get to the code
See my video on how to write to text files here:
• C++ Tutorial: How to ...
// Learn More //
Check out my complete C++ videos playlist:
• Playlist
Check out all my C++ videos for file i/o in my files playlist:
• Playlist
// Consider supporting this channel in multiple ways
paypal.me/hank...
/ @professorhankstalica
#programming #C++ #tutorial

Пікірлер: 69
@Confelgi
@Confelgi 2 жыл бұрын
Dude you are an actual life saver I've spent the good part of two hours trying to figure out how fstream iterates. Now that I get that it works kinda like the console I feel kinda dumb for overcomplicating it for myself.
@ProfessorHankStalica
@ProfessorHankStalica 2 жыл бұрын
I always get a similar reaction from students. It's just cout/cin in a different context and works exactly the same. So many people have a hard time to believing that. Glad you got it.
@adedejiemmanuel1
@adedejiemmanuel1 2 ай бұрын
Hi Professor, i noticed this is an old video yet its evergreen. I just took some notes. Thanks.
@ProfessorHankStalica
@ProfessorHankStalica 2 ай бұрын
You are welcome!
@shooter8742
@shooter8742 2 жыл бұрын
Hey Hank! Australian uni student here, thanks heaps for the video mate, it really helped close some gaps in my knowledge.
@ProfessorHankStalica
@ProfessorHankStalica 2 жыл бұрын
Glad that it helped you out! Thanks for the comment and the support!
@geek_on_coffee
@geek_on_coffee 2 жыл бұрын
That's amazing. You are way more coherent than my professor. I do have a question (if you still take any) how would I read both an int and a string piece of information such as "123 abc"? Something like that. Can you read them separately? Thank you!
@ProfessorHankStalica
@ProfessorHankStalica 2 жыл бұрын
string x; int i; ifstream fin; fin >> x >> i; or fin >> x; fin >> i; Depending on how they appear in the text file. Exact same way you use cin.
@osamaanees8406
@osamaanees8406 2 жыл бұрын
Love You man! Your Whole Playlist on File handling is awesome! Because of you I could complete my assignment. Love From Pakistan.
@ProfessorHankStalica
@ProfessorHankStalica 2 жыл бұрын
Thanks for the kind words, brother. Glad you find them useful! Would love to have you as a subscriber or a member if you are so inclined.
@osamaanees8406
@osamaanees8406 2 жыл бұрын
@@ProfessorHankStalica unfortunately member feature is blocked in my country. I already subscribed :)
@ProfessorHankStalica
@ProfessorHankStalica 2 жыл бұрын
@@osamaanees8406 Glad you're here, brother! Thanks for the sub!
@jaystonks
@jaystonks 3 жыл бұрын
what to do if you are trying to read strings that have spaces in them, ex: first and last names in one string
@ProfessorHankStalica
@ProfessorHankStalica 3 жыл бұрын
Use getline()
@BoshiNagare
@BoshiNagare 2 жыл бұрын
It is difficult to make a program that can unpack the content of a data container file that is less then 1 MB ? How to start writing such a program? Thank you in advance
@ProfessorHankStalica
@ProfessorHankStalica 2 жыл бұрын
Assuming you are talking about a container such as an array or a vector or a list, not a problem at all. You can see an example here: kzbin.info/www/bejne/iYHJe2tra8p4hJY
@BoshiNagare
@BoshiNagare 2 жыл бұрын
@@ProfessorHankStalica Thank you very much, but I also have another question which is not topic-related question. I have a ListBox named to Vion. Is it possible to use it with FindWindowA? HWND hwnd = FindWindowA(0,""); FindWindowA = Vion->SelectedIndex; How to use ListBox Item with lpWindowName? Thank you very much in advance
@DeathMetalJedi
@DeathMetalJedi Жыл бұрын
What if I wanted to read the entire text file with a while loop but instead of storing the entirety of a file in a single variable I wanted to make each line, or each thing separated by white space be stored in individual variables. Say you have a text file with a list of names separated by white space (not worrying about the first and last name being separated by white space). I want to read the entire file and have string name1 be the first name, string name2 be the next name and so on how would you accomplish this?
@ProfessorHankStalica
@ProfessorHankStalica Жыл бұрын
Use a vector: kzbin.info/www/bejne/ap-vi5ywfJefeM0
@gundulfguy2179
@gundulfguy2179 3 жыл бұрын
Great tutorial so far, but I have a problem: My file: 12 13 14 15 16 Code: (inside if statement) int x while (infile >> x) { cout
@gundulfguy2179
@gundulfguy2179 3 жыл бұрын
I am aware I forgot a semicolon here or there
@gundulfguy2179
@gundulfguy2179 3 жыл бұрын
Nevermind I figured it out, It was because I added some noskipws function I saw on stackexchange. I have another question however, how do I read line by line, instead of space by space?
@ProfessorHankStalica
@ProfessorHankStalica 3 жыл бұрын
Use the function getline(), but that works only with strings. You could also use cin.getline().
@lindsaywaterman2010
@lindsaywaterman2010 Жыл бұрын
Where, exactly is the file located? I did not see the specified location in the program!
@ProfessorHankStalica
@ProfessorHankStalica Жыл бұрын
Depends on what IDE you are using, but Visual Studio places the file by default in the same location as the source file. If you are running the program itself by double-clicking on it, then it'll be in the same folder as the executable.
@lindsaywaterman2010
@lindsaywaterman2010 Жыл бұрын
@@ProfessorHankStalica Thank you sir.
@ProfessorHankStalica
@ProfessorHankStalica Жыл бұрын
@@lindsaywaterman2010 You're welcome and hope it helps. Good luck to you in your project.
@galericellokirijo6649
@galericellokirijo6649 2 жыл бұрын
hi professor, what should I do if I want to read the text file with different data type. for example the file contains the data of student number (char type), student name (string), and test score (integers) in each row. thank you
@ProfessorHankStalica
@ProfessorHankStalica 2 жыл бұрын
You do the exact same thing, you just use a char variable, a string variable, and an int variable.
@jacksonveeh6767
@jacksonveeh6767 10 ай бұрын
I love your keyboard, your sockets make me rocket
@potitus8751
@potitus8751 2 жыл бұрын
Good day, Prof! May know what application you are using for writing the program? Thank you in advance
@ProfessorHankStalica
@ProfessorHankStalica 2 жыл бұрын
Hi ya, I'm using Visual Studio Community: kzbin.info/www/bejne/rZaTpnSHbdSngrM
@potitus8751
@potitus8751 2 жыл бұрын
@@ProfessorHankStalica Thank you very much!
@dunkelthegod3861
@dunkelthegod3861 2 жыл бұрын
What should I do if I'm reading from a file that is a list of ints separated by commas? while(inFile >> x) only prints first int rn
@dunkelthegod3861
@dunkelthegod3861 2 жыл бұрын
a classmate helped resolve this issue w/ use of peek and ignore
@ProfessorHankStalica
@ProfessorHankStalica 2 жыл бұрын
That works. You could also read in the entire line and use strtok() to split it up.
@jorgealfredocarvalho
@jorgealfredocarvalho 3 жыл бұрын
HI Professor Hank Stalica I want to congratulate you on your work. You are a great teacher. Good scientific knowledge and very good pedagog. I really appreciate your video. Thank you very much. Jorge Carvalho (Portugal) PS: I like your shirt!
@ProfessorHankStalica
@ProfessorHankStalica 3 жыл бұрын
Obrigado! Desculpe, nao falo Portugues! Bom dia! I'll be visiting your country looking for a neighborhood to live in Porto in about 3 weeks. So nice of you to leave a kind comment. Thanks for the support!
@othnielcooke5246
@othnielcooke5246 2 жыл бұрын
what if you are reading from a file with both words and numbers?
@ProfessorHankStalica
@ProfessorHankStalica 2 жыл бұрын
Then, you would need to do something else. For example, read it in as a string and then test characters in the string. You could also test stream error bits.
@MrFallred
@MrFallred 2 жыл бұрын
You created the text file, but how did you make it so the program was able to access the text file? Sorry if it's a super basic question but I'm new to this. Is there a good video that explains what I'm asking (I can't find one).
@ProfessorHankStalica
@ProfessorHankStalica 2 жыл бұрын
Sure. Here you go: kzbin.info/www/bejne/pp29iaysmt6MoLc
@BlackAnt02
@BlackAnt02 2 жыл бұрын
I want to be able to read multiple lines how can I accomplish that?
@ProfessorHankStalica
@ProfessorHankStalica Жыл бұрын
ifstream fin; . . . fin >> x; fin >> y; fin >> z; . . . etc
@jianyoong4567
@jianyoong4567 2 жыл бұрын
Good day Professor! it is a great tutorial video. However, i have some question to ask about. How can I detect the number entered by the user in the txt file and display the name that i wanted to display? For example: User key in: 5 I should have displayed: Professor Hank Stalica (Below are the data in the txt file) [1] Titan [2] Ryan [3] Philip [4] Sam [5] Professor Hank Stalica Hope to get your reply, and thank you Professor!
@ProfessorHankStalica
@ProfessorHankStalica 2 жыл бұрын
You could read the contents of the file into a vector and then use the number entered by the user as an index for the vector. That could be one way to do it.
@jianyoong4567
@jianyoong4567 2 жыл бұрын
@@ProfessorHankStalica Thank you for your reply, Professor! Unfortunately I did not see it earlier, but I had used a similar way to solve the problem. Still feeling thank and surprised to get your reply, btw your tutorial video is useful and it helped me a lot!
@omarsaleh6390
@omarsaleh6390 3 жыл бұрын
how can i read from class please
@ProfessorHankStalica
@ProfessorHankStalica 3 жыл бұрын
Step 1: Get vaccinated Step 2: Drive to school. Step 3: Put on a mask. Step 4: Walk to classroom. Step 5: Open textbook. Step 6: Read textbook in class.
@omarsaleh6390
@omarsaleh6390 3 жыл бұрын
@@ProfessorHankStalica class obj professor
@ProfessorHankStalica
@ProfessorHankStalica 3 жыл бұрын
@@omarsaleh6390 You'd have to process each attribute of the class, one at a time. C++ doesn't natively support object serialization.
@swordofallah8716
@swordofallah8716 10 ай бұрын
life saver, good stuff
@Ioftly
@Ioftly 2 жыл бұрын
Omg, I’m doing my 1st semester cpp final and had some ideas that’re way past my expertise but thanks to this I think I can actually get it! Tysm 10000/10 channel def the best YT Prof I’ve ever seen! ❤❤❤ Edit: Def just got my sub!
@ProfessorHankStalica
@ProfessorHankStalica 2 жыл бұрын
Thanks for the kind words and glad it helped! 😉
@Ioftly
@Ioftly 2 жыл бұрын
@@ProfessorHankStalica words can’t describe how thrilled I am to update my code with these new methods, my thanks goes beyond words 🙏
@mmp7337s
@mmp7337s Жыл бұрын
I copied your code and it didn't work, I found out the problem was that I did not give the program the specific path to the file. So if you are stuck just change "input.txt" to the full path name :)
@ProfessorHankStalica
@ProfessorHankStalica Жыл бұрын
Visual Studio looks for the file, by default, in the same location as the source file. If you didn't put it there, that explains why you had to include the full path name.
@tiaanmare
@tiaanmare 2 жыл бұрын
Thank you for your help :)
@ProfessorHankStalica
@ProfessorHankStalica 2 жыл бұрын
Any time. Thanks for stopping by!
@saurabhtalele1537
@saurabhtalele1537 3 жыл бұрын
awesome sir...
@ProfessorHankStalica
@ProfessorHankStalica 3 жыл бұрын
Thanks a ton!
@saurabhtalele1537
@saurabhtalele1537 3 жыл бұрын
yes teacher exponentially reduce time to learn by what that is call self learn..
@Altern8Paths
@Altern8Paths 2 жыл бұрын
Very helpful and i love the shirt too. A very educated man.
@ProfessorHankStalica
@ProfessorHankStalica 2 жыл бұрын
Glad you liked it!
@philiphaynes9335
@philiphaynes9335 3 жыл бұрын
ugh.... sniffing.....
@ProfessorHankStalica
@ProfessorHankStalica 3 жыл бұрын
Sorry, brother!
Une nouvelle voiture pour Noël 🥹
00:28
Nicocapone
Рет қаралды 9 МЛН
黑天使被操控了#short #angel #clown
00:40
Super Beauty team
Рет қаралды 61 МЛН
Support each other🤝
00:31
ISSEI / いっせい
Рет қаралды 81 МЛН
IL'HAN - Qalqam | Official Music Video
03:17
Ilhan Ihsanov
Рет қаралды 700 М.
Output File Streams in C++ (Writing to Files)
13:47
Programming with Dr. Hayes
Рет қаралды 39 М.
why do header files even exist?
10:53
Low Level
Рет қаралды 442 М.
Mini Project: How to program a GPU? | CUDA C/C++
12:53
0Mean1Sigma
Рет қаралды 31 М.
How to use binary files [an updated C++ tutorial]
19:18
Professor Hank Stalica
Рет қаралды 20 М.
How to Parse a CSV File in C++
13:38
Code Morsels
Рет қаралды 37 М.
Une nouvelle voiture pour Noël 🥹
00:28
Nicocapone
Рет қаралды 9 МЛН