C++ Tutorial - 24 - Reading From a File

  Рет қаралды 477,043

thenewboston

thenewboston

Күн бұрын

Пікірлер: 262
@cmommsen1
@cmommsen1 13 жыл бұрын
@rockydasha Re:Basically, bucky is a variable of type ifstream, due to this statement: ifstream bucky; And after this statement: bucky.open(filename); bucky is now "connected" to the user-defined file name contained in filename - in this tutorial, "beefy.txt". Therefore, when you use it with >> (known as the extraction operator), the stream looks in beefy.txt, finds the letters 't', 'h', 'i', and 's', and "streams" them into word. The next letter is a space, so the stream operator terminates.
@InSpmindss
@InSpmindss 14 жыл бұрын
you are the best teacher i ever have seen in my life, in real sense ,you teach in the best way , a lot lot more thanks to you for providing such materials
@CensSka
@CensSka 8 жыл бұрын
I've got an exam tommorow I'm screwed
@thinumstoneheart6713
@thinumstoneheart6713 7 жыл бұрын
LMAO same af man
@informativecontent4778
@informativecontent4778 7 жыл бұрын
mine is tommorrow wish me luck
@jimmychan9282
@jimmychan9282 6 жыл бұрын
mine is tomorrow
@ZainiverseCreations
@ZainiverseCreations 6 жыл бұрын
Mine is tomorrow LOL .. Everyone comes to bucky a day before exam ...
@Mustafalllica
@Mustafalllica 6 жыл бұрын
mine is tomorrow
@zarigull274
@zarigull274 5 жыл бұрын
This link is best to learn programming because before this i had no idea about file handling but know i can describe this frequently thanks
@colinrickels201
@colinrickels201 11 жыл бұрын
i have gone to hell and back looking for a tutorial such as this thank you so much
@Haris0423lhr
@Haris0423lhr 4 жыл бұрын
you look like jesus so no problem for you
@johnnyc130
@johnnyc130 15 жыл бұрын
Great tutorials. You are very clear in all of them and dont leave me confused like some other tutorials. I made a program that can open files, and create files to write on.
@Finalfox09
@Finalfox09 10 жыл бұрын
Thank you this tutorial was a life savior
@jgray110
@jgray110 12 жыл бұрын
Thanks Bucky, finally got a file to read after dozens of other search code attempts because I was afraid I couldn't do this, but I tried it and walla, don't forget to bucky.close after while { }. system{"pause") is operating system dependent so eventually we need to learn OS independent code. These tutorials get an A++
@rybread5718
@rybread5718 6 жыл бұрын
Thank you my good man. I used #include and it worked the same.
@christophergaspar6520
@christophergaspar6520 3 жыл бұрын
anybody who sees this please help, i wrote a code similar to this in my visual studio I then noticed that, when the program is displaying the text file, the last word is not included. even in bucky's program, notice at 6:05 the last word ('C++') is not included. is there a way to fix this I would greatly appreciate your help, thank you
@Qazqi
@Qazqi 12 жыл бұрын
Not sure why you'd prefer character arrays over std::string. Anyway, just read it with while (std::cin >> word) {std::cout
@concernedviewer6909
@concernedviewer6909 4 жыл бұрын
Just wanna say again, Welcome back Buckey!!!
@OnlineMediaCollection
@OnlineMediaCollection 7 жыл бұрын
Just needed this line, *while (iMyFile.good())* LOL. Thanks!
@thetruereality2
@thetruereality2 5 жыл бұрын
That wouldn't print anything, it would only work in case of an eof function. I think you meant while(MyFile.good)
@naimcool36
@naimcool36 4 жыл бұрын
@@thetruereality2 hes right "good" is a function
@RazRikimaru122
@RazRikimaru122 10 жыл бұрын
Hey is there any possible way to make the number from each line in the text file output into an array grid of variables ?
@usabnd
@usabnd 14 жыл бұрын
@linglihe ofstream is for output file. it means if you want to change the content of a file.. hope I helped
@yomguioim
@yomguioim 9 жыл бұрын
you can get an open file box with tiny file dialogs on sourceforge is a single C C++ cross-platform file offering many native dialogs.
@Boeing737Channel
@Boeing737Channel 13 жыл бұрын
@Yikak4 cin.getline = just like cin >>, just can manipulate it more. It gets the line you enter. (filename,50) is to store what you inputed in filename, and you need to include 50, the char character size.
@keykeyine
@keykeyine 12 жыл бұрын
Just as a side note, the way you set this up it's never going to print off the last word in the text file you read in. Not a big deal but I thought it might confuse some people. You just need another cout
@FastCodeDeveloper
@FastCodeDeveloper 9 жыл бұрын
man you must be the most subscribed programmer-youtuber of all time...
@reggaemediatv
@reggaemediatv 11 жыл бұрын
Thank you
@octav3k
@octav3k 13 жыл бұрын
@Duodecillian 50 is the maximum number of characters that variable can hold. So if you had a filename or word (the variables he defined inside the program) that's longer then 50 characters, only the first 50 would be read by the program.
@MozartTime
@MozartTime 12 жыл бұрын
I just needed to refresh my mind about fstream. Loved this , so clear as good.
@Wingedzephir
@Wingedzephir 12 жыл бұрын
Great you just solved all my problems with this simple example...well done thanks!
@Jonko192
@Jonko192 14 жыл бұрын
good for beginners. only a few tips to improve this program: change system("pause"); to cin.get(); and why using cstdlib? the stuff is already in the iostream library. the best thing to do is put the load stuff into a function and inside the function you put a return statement if the file is not open. dont use exit(exit_failure);
@PaxiKaksi
@PaxiKaksi 6 жыл бұрын
But getline eats the last word in your text so........ That is a bit inefficient ?
@h8errage
@h8errage 13 жыл бұрын
@shebotnov ofstream enters stuff into a file, and ifstream gets stuff from a file(read a file) i think u can use an fstream object that does both so u never have to ask yourself that question.
@bwneyeschik
@bwneyeschik 15 жыл бұрын
Do you do this often? If you do I may continue with programming. Otherwise, I think I may change my Major!! (this online class thing sucks!)
@sum21chan
@sum21chan 14 жыл бұрын
I like ur video!! just wanna remind you, you could press the tab button for go in 5 space (I think) instead of pressing the space 5 times!
@arraybabe8514
@arraybabe8514 12 жыл бұрын
yes, use getline and dont forget to include the string header
@helpfullprogrammer
@helpfullprogrammer 15 жыл бұрын
how do you output the whole con-tense of the file? I can only get one word. thanks in advanced! from helpfullprogrammer.
@Digiscat
@Digiscat 13 жыл бұрын
If you ARE confused, just think a bit and mess around with the code. It'll help you both understand and remember. If you're like me, cin.getline and the array threw you off. cin.getline is like a more flexible cin >>---- So, in other words, its like he put "cin >> filename[50]"....get it? And if you make the number for the array smaller, that just limit how many characters it can hold. So, -- char file[50] -- can hold up to 51...but if you put file[1], itll only USE 2. (+1 since 0 counts.)
@r3sp3c791
@r3sp3c791 14 жыл бұрын
Couldn't get a think from this one, it was so fast!!!
@muhammadbinsaleem8565
@muhammadbinsaleem8565 4 жыл бұрын
Bro ! May you live happy life...
@razeal113
@razeal113 12 жыл бұрын
its saying that the char string can be up to 50 characters long. ie, he doesn't know the length of the filename ahead of time so he allows for up size 50. if he was worried about extensions (blah/blah2/.../file) might have made it larger, but 50 should do for any file
@Mrlpjo
@Mrlpjo 13 жыл бұрын
@TheResponsae. Is that really needed??? I think the file automatically closes once the execution is over?!
@GNSstudios
@GNSstudios 15 жыл бұрын
you showed how to write a file but what if i wanted to write the file to a specific directory.
@josephrowell9052
@josephrowell9052 4 жыл бұрын
When I do this, whatever actual file there is, it prints loads of zeros?
@Mowteng
@Mowteng 14 жыл бұрын
i dont understand how you can go through each loop, where in the code you told C++ that you want the file content separated by space? why is the first cout in the while not the complete content of the file (with a max of 10 chars ofc) ?!?
@yasirwisal8081
@yasirwisal8081 9 жыл бұрын
Hi Bucky, How do you end line if there is a large text file with the string data type and do some calculation from data which is in text file?
@reggaemediatv
@reggaemediatv 11 жыл бұрын
what part of it dont work?
@octav3k
@octav3k 13 жыл бұрын
@Silvertide82 You can create a file with Notepad (he did so before running the tutorial) inside the directory where he saved his cpp file. For example, if he had "tutorial.cpp" inside "C:\Users\Greg\C Toturials\", the text file would have to be in the same directory: "C:\Users\Greg\C Tutorials\beefy.txt". I hope that clears it up for you.
@mynewlife210
@mynewlife210 11 жыл бұрын
Where should the file be located??
@HarisMalik
@HarisMalik 6 жыл бұрын
instead of adding cstdlib we can use same code but exit(0) and the program must exit
@insomeperson
@insomeperson 11 жыл бұрын
Why does it always ignore the last word?
@octav3k
@octav3k 13 жыл бұрын
Tou can't see the last word because the condition on the while function is put before the body of the function (he used while, not do-while) and each time the program reads a word with "bucky >> word", it moves to the end of that word. After it reads the last word, the function "bucky.good()" becomes false and it doesn't enter the loop again in order to post the variable (word) to the screen. He could have avoided this either by using do-while or through the method described by andreipopescu87.
@TheHeadmanmr
@TheHeadmanmr 12 жыл бұрын
What is this used for?? Would be much easier for my brain if I knew how this would be used in a real situation... !
@euktalinoo
@euktalinoo 13 жыл бұрын
i want to read an array and i want to covent a string in to an integer.can you help me please?
@genjimidou
@genjimidou 13 жыл бұрын
After seeing 5 times I finally understand ...xD
@Lightscribe225
@Lightscribe225 13 жыл бұрын
Say I wanted to take a list of random numbers from a file, and find their sum and average. How do I go about doing that? Do you have a tutorial going over that somewhere?
@chuserio2
@chuserio2 12 жыл бұрын
hey can u explain, why the 50 on the char file name? what it represents
@stereojos86
@stereojos86 14 жыл бұрын
you save the txt doc in the same folder that the program? My program dont read the txt, go directly to exit(EXIT_FAILURE); I copy and paste your program.
@dumacele6724
@dumacele6724 3 жыл бұрын
so relatable "come on baby",,,exctly how we al talk to our computers when we're in the zone
@LPSlasher
@LPSlasher 14 жыл бұрын
3:30, People: keep in mind that using SPACEBAR in an empty line may wreck the code sometimes, don't try it. (in empty lines you should use either TAB or ENTER... Of course you CAN use SPACEBAR after a codeline)
@adeepnair
@adeepnair 10 жыл бұрын
how do i do this with a text file with numbers in it?
@TheSheniceJ
@TheSheniceJ 9 жыл бұрын
+Adeep Nair ik its like a year u posted this but same thing i wanna know :(
@TheHeba66
@TheHeba66 13 жыл бұрын
how can I create a file in the same directory? where exactly should I save it?
@Humbertorgr
@Humbertorgr 12 жыл бұрын
Ok it worked for me... But I made a txt file with two lines and is only showing up one :s... is there a way to use it with string instead of char?
@m4unot
@m4unot 13 жыл бұрын
Can you open a "makefile" and use command: "make flash" to run it?
@baarbie0786
@baarbie0786 9 жыл бұрын
So what is the ideal way of reading from a file?
@WikiPeoples
@WikiPeoples 14 жыл бұрын
how come it didnt read the entire line? It left out "C++!" .... mines doing the same thing
@SimpleCookiee
@SimpleCookiee 12 жыл бұрын
I wonder one thing tho... Let's say I recieve a input as value into a char[50].. Assume it got the value "people".. Now here's my question.. How do I add text to that variable? I want to add ".txt" at the end so I becomes "people.txt", how to do that?
@chernobila
@chernobila 15 жыл бұрын
hello sir are you going to post more C++ tutorials? Please do Thank you
@anikatahir6146
@anikatahir6146 3 жыл бұрын
Im having this problem that i have to construct a program to read my file where i have to provide choice for the user to select. Like i saved names and roll numbers in my file and now i have to provide option to user to select which roll number's name should be displayed/read....
@shekhardk
@shekhardk 12 жыл бұрын
Well this code doesn't read and print the last word in the file. If the file has just one word then it won't print anything.
@ivaka92
@ivaka92 12 жыл бұрын
20 y.o. here :) it was an honest reaction really - stuff until here was easy to comprehend and suddenly lots of commands and info! When i watched it 2-3 more times i got the hang of it... almost :)
@shidono6786
@shidono6786 5 жыл бұрын
are you working now?
@IvanRubinson
@IvanRubinson 15 жыл бұрын
how do i made it so i decide wich file to open (as a programmer)... I mean pre-define that filename = [something]
@tarBall313
@tarBall313 15 жыл бұрын
Why don't you use Visual Studio?:)
@meyerflyer34
@meyerflyer34 12 жыл бұрын
It could be used for storing options in video games and then outputing them with a custom GUI.
@sarahbiebah
@sarahbiebah 6 жыл бұрын
Why doesn't line 6 CREATE the file like we have done before? How come this time it needs the file already created? What's different?
@MrBrightSide622
@MrBrightSide622 14 жыл бұрын
ugh. Do you need to be admin like the last one?
@DRDSkidroW
@DRDSkidroW 13 жыл бұрын
@SuperiorOnRoblox make sure you placed the exclamation mark in: " if (!test.is_open()){ " that was my problem
@Yizak
@Yizak 13 жыл бұрын
What is cin.getline(filename, 50) ?
@muhammadmohaiminulislam7189
@muhammadmohaiminulislam7189 7 жыл бұрын
Can i write or read a character in a file at a selected position?Like adding 'a' at 2nd line , third position at file? Or read from a selected position?
@blacksven1
@blacksven1 14 жыл бұрын
What is the [50] for???
@doerparol
@doerparol 11 жыл бұрын
How can I make the program to store single characters instead of words? Afterwards I would like to process that stored information and count each letter so number of a= 50 b = 46 and so on
@davidalyakobi6857
@davidalyakobi6857 7 жыл бұрын
Hey Bucky why did you use exit(EXIT_FAILURE) instead of exit(1) ?
@m4unot
@m4unot 15 жыл бұрын
why do you say: char filename[50]; why 50 ? do you limit char with?? so if i say 5 .. i can only type 5 char??
@SuperFireEagle
@SuperFireEagle 3 жыл бұрын
how would you code it to where it can automatically detect the input file name without hardcoding the name of the file into the program?
@yoloop93
@yoloop93 15 жыл бұрын
mine doesn't open as text file but opens with cmd.help!
@muhammadfariezdaniel4769
@muhammadfariezdaniel4769 3 жыл бұрын
wah thankyouuu>< I am watching this in 2021
@chaouchsamir3635
@chaouchsamir3635 7 жыл бұрын
what do you mean by using [ 50 ] ?
@dkblade27
@dkblade27 11 жыл бұрын
I thought when we open a file that we don't have, C++ will create it for us. So how come tunafeesh.txt didn't open??
@TheMohawkNinja
@TheMohawkNinja 14 жыл бұрын
@linglihe I think that of is Output File, and if is Input File
@zeen5163
@zeen5163 7 жыл бұрын
does "bucky >> word" get the word from the user and stores it in the file? or what does that actually do?
@durraiz_ahmad18
@durraiz_ahmad18 8 жыл бұрын
Can you tell how can I skip/ignore line or row while reading a csv file and storing it in a 2d array?
@oumie93
@oumie93 5 жыл бұрын
Please help: do you know how can I input a ply file via C++ language ....?????
@TheAl2kas
@TheAl2kas 9 жыл бұрын
Hey, thanks for a useful tutorial, but I have a little problem. Program won't print last word. How do I fix it? :l
@Pcatalin66
@Pcatalin66 9 жыл бұрын
+Aldis Petrauskas That is because the text is in that "char word[50]" and word[50] have only 50 slots. If you enter an text longer that 50 caracters (space included) than your program will cut the rest and stop at the 50 caracter. :D make sure that you give more than [50] ... maybe word[100] will help you .
@RexGalilae
@RexGalilae 9 жыл бұрын
+Aldis Petrauskas The reason is because he types *bucky>>word* before *cout
@RexGalilae
@RexGalilae 9 жыл бұрын
Lily Rathbun You first open the file and give it an alias (i.e. a placeholder) name, in this case, bucky. If you opened the file as , say "Lily", then you simply swap out all the "bucky"s from the code with "Lily"s. Pls. tell me if I misunderstood your question. I think I have misunderstood it.
@RexGalilae
@RexGalilae 9 жыл бұрын
Lily Rathbun If you're talking about the location of the file in the directory, then it's automatically created in the same folder as your program.
@15sunny1
@15sunny1 8 жыл бұрын
+Mohammed Zaid Thank you!
@AndToeKnee1
@AndToeKnee1 11 жыл бұрын
what if you want to copy codes from a website and paste it back like the ones to prove your not a computer ? lol
@pranoydutta5435
@pranoydutta5435 8 жыл бұрын
instead of including the cstdlib to use exit, can we just return 124721; as this is in main
@Milo_1105
@Milo_1105 12 жыл бұрын
Is "People" a class?
@leoking938
@leoking938 11 жыл бұрын
Why is filename a char insted of a string?
@moazelsawaf2000
@moazelsawaf2000 5 жыл бұрын
F
@nilufargh
@nilufargh 10 жыл бұрын
why did you use "of stream" in the other video for writing a file ?
@randohinn6691
@randohinn6691 10 жыл бұрын
o -output i -input
@nilufargh
@nilufargh 10 жыл бұрын
***** Thanks, if you don't mind, I just have a question about reading a data from a file and store it in a matrix. We have some data in a file that shows the amount of precipitation in specific time (like month and day). So our Prof built a matrix by getting the # rows and columns from the file. Then He set all the elements in that matrix= 0???!!!! ( In vectors and matrices isn't it like not having a value means zero so why do we have to set zero again?!) . Anyways and then in order to put those numbers into the matrix he said m.at(rows-1).at(columns-1)= num, we read the num from the file with rows and columns but why rows-1 and columns-1????
@randohinn6691
@randohinn6691 10 жыл бұрын
matrixes and vectors are 0 indexed so, the first element actually is at position 0 in the vector and so forth
@nilufargh
@nilufargh 10 жыл бұрын
***** True, but when you resize a matrix. let's say typedef vector< vector< int> > matrix; matrix m; m.resize(rows); for ( int i= 0; i< m.size(); i++) { m.at(i).resize(column); }// So now we built out matrix which is empty that means now all the elements are zero, right? we don't have to set all the values=0 again, it's not like arrays that elements are Not Defined unless we put a value in it. Right?
@leezhijiang
@leezhijiang 10 жыл бұрын
What if that is an integer value ?
@hexmedi
@hexmedi 13 жыл бұрын
@HamishWare when you don't specify the directory, it searches for the given file inside of your project folder. at least this is how it works for me
@Mrlpjo
@Mrlpjo 13 жыл бұрын
i understood after watching the third time... not so confusing as i thought at first..
@abdulz1
@abdulz1 15 жыл бұрын
hm... my file do not open ???
@fernandinhoflor
@fernandinhoflor 13 жыл бұрын
Add in the end of while the following expression: if(!bucky.good()) { bucky >> word; cout
@Johnlolmann
@Johnlolmann 6 жыл бұрын
why do it open the file in cmd and not in notepad
@JosePimenta_JP
@JosePimenta_JP 10 жыл бұрын
hi, how do i read a code from a file? like instead of just opening a file and read the text. i want to open the file and read code that is inside that file. how it is done? thanks in advance
@josephchotard
@josephchotard 8 жыл бұрын
I know it's been a year but it would actually be impossible unless you implemented a compiler into your script to compile the code as C++ must be precompiled. If you really want to do this you should use Python
@kurekvision
@kurekvision 15 жыл бұрын
why do you make some programs in console by using c++ (very hard way) if you can do the same stuff with just few lines in notepad (batch file) and its very easy. I dont get you guys.
@Niffisa
@Niffisa 14 жыл бұрын
There is a problem with this program. It wont output the last word in your .txt file ;-)
C++ Tutorial - 25 - Passing Arrays into Functions
6:19
thenewboston
Рет қаралды 128 М.
"BEST C++ CODE ever written" // Code Review
27:38
The Cherno
Рет қаралды 95 М.
人是不能做到吗?#火影忍者 #家人  #佐助
00:20
火影忍者一家
Рет қаралды 20 МЛН
It works #beatbox #tiktok
00:34
BeatboxJCOP
Рет қаралды 41 МЛН
why do header files even exist?
10:53
Low Level
Рет қаралды 450 М.
How to use binary files [an updated C++ tutorial]
19:18
Professor Hank Stalica
Рет қаралды 20 М.
ChatGPT vs Stockfish: ABSURD CHESS
25:01
GothamChess
Рет қаралды 783 М.
Input File Streams in C++ (Reading from Files)
10:17
Programming with Dr. Hayes
Рет қаралды 38 М.
Exception handling in C++ (How to handle errors in your program?)
24:46
C++ user defined functions (#15) 📬
17:14
Bro Code
Рет қаралды 39 М.
人是不能做到吗?#火影忍者 #家人  #佐助
00:20
火影忍者一家
Рет қаралды 20 МЛН