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

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

Professor Hank Stalica

2 года назад

24,009 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@m18181jj
@m18181jj - 05.12.2023 03:10

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 :)

Ответить
@lindsaywaterman2010
@lindsaywaterman2010 - 23.03.2023 16:06

Where, exactly is the file located? I did not see the specified location in the program!

Ответить
@Iamthezackster
@Iamthezackster - 09.02.2023 22:17

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?

Ответить
@BlackAnt02
@BlackAnt02 - 24.12.2022 21:11

I want to be able to read multiple lines how can I accomplish that?

Ответить
@BoshiNagare
@BoshiNagare - 23.12.2022 23:39

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

Ответить
@legitjustioftly5610
@legitjustioftly5610 - 03.12.2022 07:33

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!

Ответить
@mr_squarehead
@mr_squarehead - 02.12.2022 17:37

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!

Ответить
@galericellokirijo6649
@galericellokirijo6649 - 05.11.2022 11:13

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

Ответить
@Confelgi
@Confelgi - 26.10.2022 07:56

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.

Ответить
@potitus8751
@potitus8751 - 05.10.2022 06:30

Good day, Prof! May know what application you are using for writing the program? Thank you in advance

Ответить
@jianyoong4567
@jianyoong4567 - 02.09.2022 09:53

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!

Ответить
@tiaanmare
@tiaanmare - 22.06.2022 17:41

Thank you for your help :)

Ответить
@shooter8742
@shooter8742 - 02.04.2022 08:54

Hey Hank! Australian uni student here, thanks heaps for the video mate, it really helped close some gaps in my knowledge. <3

Ответить
@osamaanees8406
@osamaanees8406 - 25.03.2022 11:20

Love You man! Your Whole Playlist on File handling is awesome!
Because of you I could complete my assignment.

Love From Pakistan.

Ответить
@MrFallred
@MrFallred - 26.02.2022 07:18

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

Ответить
@othnielcooke5246
@othnielcooke5246 - 18.02.2022 16:07

what if you are reading from a file with both words and numbers?

Ответить
@dunkelthegod3861
@dunkelthegod3861 - 04.02.2022 22:43

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

Ответить
@flaatheaad7544
@flaatheaad7544 - 24.01.2022 07:35

Very helpful and i love the shirt too. A very educated man.

Ответить
@jaystonks
@jaystonks - 15.12.2021 04:13

what to do if you are trying to read strings that have spaces in them, ex: first and last names in one string

Ответить
@jorgealfredocarvalho
@jorgealfredocarvalho - 11.12.2021 17:44

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!

Ответить
@connoisseurdumbass1863
@connoisseurdumbass1863 - 20.11.2021 10:38

Hi professor,

Can u tell me where you got that shirt? I want 12 of it.

Thank you.

Ответить
@philiphaynes9335
@philiphaynes9335 - 27.10.2021 03:49

ugh.... sniffing.....

Ответить
@omarsaleh6390
@omarsaleh6390 - 11.09.2021 00:32

how can i read from class please

Ответить
@gundulfguy2179
@gundulfguy2179 - 03.09.2021 02:12

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 << x << endl;
}

infile.close()

Output:
12

All my other code is identical to yours. I seem to have some problem with my while statement; no matter what I try, it only prints the first number in the file. Perhaps I am missing something. Would appreciate some help if possible.

Ответить
@saurabhtalele1537
@saurabhtalele1537 - 27.08.2021 10:05

awesome sir...

Ответить