C++ Tutorial - 24 - Reading From a File

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

thenewboston

thenewboston

Күн бұрын

Пікірлер: 262
@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
@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.
@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
@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
@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.
@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.
@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
@FastCodeDeveloper
@FastCodeDeveloper 9 жыл бұрын
man you must be the most subscribed programmer-youtuber of all time...
@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
@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.)
@sum21chan
@sum21chan 15 жыл бұрын
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!
@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);
@Finalfox09
@Finalfox09 10 жыл бұрын
Thank you this tutorial was a life savior
@concernedviewer6909
@concernedviewer6909 4 жыл бұрын
Just wanna say again, Welcome back Buckey!!!
@CensSka
@CensSka 8 жыл бұрын
I've got an exam tommorow I'm screwed
@thinumstoneheart6713
@thinumstoneheart6713 8 жыл бұрын
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
@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.
@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.
@arraybabe8514
@arraybabe8514 12 жыл бұрын
yes, use getline and dont forget to include the string header
@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
@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.
@jasperko308
@jasperko308 5 жыл бұрын
Solution for programs that couldn't work. You need to select file on the top left hand corner in CODEBLOCK
@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)
@MozartTime
@MozartTime 12 жыл бұрын
I just needed to refresh my mind about fstream. Loved this , so clear as good.
@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.
@muhammadbinsaleem8565
@muhammadbinsaleem8565 4 жыл бұрын
Bro ! May you live happy life...
@Wingedzephir
@Wingedzephir 13 жыл бұрын
Great you just solved all my problems with this simple example...well done thanks!
@fernandinhoflor
@fernandinhoflor 13 жыл бұрын
Add in the end of while the following expression: if(!bucky.good()) { bucky >> word; cout
@r3sp3c791
@r3sp3c791 14 жыл бұрын
Couldn't get a think from this one, it was so fast!!!
@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 ?
@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
@usabnd
@usabnd 14 жыл бұрын
@linglihe ofstream is for output file. it means if you want to change the content of a file.. hope I helped
@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?
@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.
@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
@Mrlpjo
@Mrlpjo 13 жыл бұрын
i understood after watching the third time... not so confusing as i thought at first..
@dumacele6724
@dumacele6724 3 жыл бұрын
so relatable "come on baby",,,exctly how we al talk to our computers when we're in the zone
@GregVanGorp
@GregVanGorp 12 жыл бұрын
instead of using exit(EXIT_FAILURE); it would be easier to explain "return 1;". For all of you beginning programmers, this returns a value to int main() and signifies that the program did not execute correctly. Both methods work, but for beginning programmers, this is simpler.
@genjimidou
@genjimidou 13 жыл бұрын
After seeing 5 times I finally understand ...xD
@kotapaka
@kotapaka 12 жыл бұрын
Oh...OK. I merely thought of pointing out that learning/knowing C++ and the standard library is probably the easiest step in the world of programming and software (especially reading/writing files). In C++ read/write file operations are made a breeze by the library.
@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.
@meyerflyer34
@meyerflyer34 12 жыл бұрын
It could be used for storing options in video games and then outputing them with a custom GUI.
@sumittechkgp
@sumittechkgp 13 жыл бұрын
if you are using EXIT_FAILURE then use return(EXIT_SUCCESS) instead of return 0
@HarisMalik
@HarisMalik 6 жыл бұрын
instead of adding cstdlib we can use same code but exit(0) and the program must exit
@CloakLord
@CloakLord 10 жыл бұрын
I am working on a planner I've got it to read the class end line , homework end line , and then the day its due on to the file and read it from the file. Although, I have 4 classes and I don't know how to name the variables in a way where I can have it restate all the homework correctly. I have tried multiple ways and many dead end ideas such as call functions which seemed genius at the time to read it from the file and based on which it is to have the variable be set to that in that call function. I just had an idea to have it read from multiple files based on the class, but seems impractical.
@CloakLord
@CloakLord 10 жыл бұрын
This video helped a lot o.o
@PaxiKaksi
@PaxiKaksi 6 жыл бұрын
But getline eats the last word in your text so........ That is a bit inefficient ?
@Ishakislife
@Ishakislife 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!)
@andreipopescu87
@andreipopescu87 14 жыл бұрын
to repaire the problem with not showing the last word from bucky's tutorial just delete bucky >> word; from above the while loop and reverse the 2 lines inside de while loop { bucky >> word; cout
@muhammadfariezdaniel4769
@muhammadfariezdaniel4769 3 жыл бұрын
wah thankyouuu>< I am watching this in 2021
@2testtest2
@2testtest2 15 жыл бұрын
you need a compiler to make you code into a executable. If you wathc thenewboston's C++ tutorial.
@th3kid13
@th3kid13 14 жыл бұрын
hella good tutorial keep it up!
@reggaemediatv
@reggaemediatv 11 жыл бұрын
Thank you
@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?
@yougurtbeautiful9895
@yougurtbeautiful9895 5 жыл бұрын
Write a function in C++ to write the characters entered through the keyboard into the file“ myfile.txt”, until a '#' character is entered.
@Mozi111111
@Mozi111111 14 жыл бұрын
It works great! Thank you!
@mokab223
@mokab223 12 жыл бұрын
THANKS BRO!! great job !!
@kottpower
@kottpower 13 жыл бұрын
This is the most difficult tutorial yet.
@SimpleCookiee
@SimpleCookiee 12 жыл бұрын
First time I watched it i thought it was difficult then I took a break, had dinner and thought about t for a while.. Then alll cleared up. I found it a bit confusing you didn't have to specify to C++ that you want the next word.. It actually takes it automatically cuz you did the ~~~ = word.
@DRDSkidroW
@DRDSkidroW 13 жыл бұрын
@SuperiorOnRoblox make sure you placed the exclamation mark in: " if (!test.is_open()){ " that was my problem
@ChadDrakeTech
@ChadDrakeTech 8 жыл бұрын
Your videos started off well bu now you just rush through everything.
@establishment4132
@establishment4132 8 жыл бұрын
K then pay more attention
@kaushikdr
@kaushikdr 5 жыл бұрын
@@establishment4132 it is kind of fast though
@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?
@josephrowell9052
@josephrowell9052 5 жыл бұрын
When I do this, whatever actual file there is, it prints loads of zeros?
@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
@Vlaxiti
@Vlaxiti 15 жыл бұрын
nice and clear !! thnx
@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....
@chris7toronto
@chris7toronto 12 жыл бұрын
Thanks Bucky!
@EjMacarus
@EjMacarus 5 жыл бұрын
"and if you don't believe me..."
@baarbie0786
@baarbie0786 9 жыл бұрын
So what is the ideal way of reading from a file?
@awesaatyanta3106
@awesaatyanta3106 6 жыл бұрын
couldn't you just use like if (!bucky.good()){ return 0; } instead of including another library? it works for me so is there any particular for using the cstdlib for exitting my program ONLY or is it just matter of preferences?
@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) ?!?
@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
@TheMohawkNinja
@TheMohawkNinja 14 жыл бұрын
@linglihe I think that of is Output File, and if is Input File
@tarBall313
@tarBall313 15 жыл бұрын
i'm sorry but i don't understand the word 'getto'. But if it's a bad word (like shit, bad etc.), that's not what I meant. Simply visual studio has extended functionality and 'auto-coding-style' (it arranges the lines according to braces etc.)
@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 :(
@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!
@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?
@stephenreader5953
@stephenreader5953 9 жыл бұрын
Thank you!
@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??
@sabrinazeb
@sabrinazeb 13 жыл бұрын
Thanku so much awesome tutorials.. :D
@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?
@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?
@yoloop93
@yoloop93 15 жыл бұрын
mine doesn't open as text file but opens with cmd.help!
@insomeperson
@insomeperson 11 жыл бұрын
Why does it always ignore the last word?
@GNSstudios
@GNSstudios 15 жыл бұрын
you showed how to write a file but what if i wanted to write the file to a specific directory.
@Milo_1105
@Milo_1105 12 жыл бұрын
Or you can put main as a void and do not worry about returns.
@netherar
@netherar 11 жыл бұрын
great tutorial! ty
@dyrokudobane
@dyrokudobane 10 жыл бұрын
I see the program terminated once it reached the end-of-file, so the c++ part didn't print out. Probably wanna take the word from the back file then print it out right after within the while good loop ( omitting the bucky >> word line). Regardless, not bad.
@NubPaws
@NubPaws 10 жыл бұрын
Or... He could have stored the file in a string variable using the cin.getLine(strVarName); I would assume that would be simpler.
@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.
@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?
@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.
@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... !
@zeen5163
@zeen5163 7 жыл бұрын
does "bucky >> word" get the word from the user and stores it in the file? or what does that actually do?
@IvanRubinson
@IvanRubinson 15 жыл бұрын
I want to make my program read the first line and execute it (For example, if in the file it says Cout
@pranoydutta5435
@pranoydutta5435 8 жыл бұрын
instead of including the cstdlib to use exit, can we just return 124721; as this is in main
@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
@RaamAnkka
@RaamAnkka 14 жыл бұрын
wait guys only thing dont understand is that what is "word" used for... is that a variable he made up???
@davidalyakobi6857
@davidalyakobi6857 7 жыл бұрын
Hey Bucky why did you use exit(EXIT_FAILURE) instead of exit(1) ?
@Mrlpjo
@Mrlpjo 13 жыл бұрын
@TheResponsae. Is that really needed??? I think the file automatically closes once the execution is over?!
@ivaka92
@ivaka92 13 жыл бұрын
while i was watching this tutorial i sat back in my chair, adjusted my glasses and said to myself "shit just got real"....
@WikiPeoples
@WikiPeoples 14 жыл бұрын
how come it didnt read the entire line? It left out "C++!" .... mines doing the same thing
C++ Tutorial - 25 - Passing Arrays into Functions
6:19
thenewboston
Рет қаралды 128 М.
C++ Tutorial - 23 - Writing on Files
6:42
thenewboston
Рет қаралды 166 М.
Мен атып көрмегенмін ! | Qalam | 5 серия
25:41
Cat mode and a glass of water #family #humor #fun
00:22
Kotiki_Z
Рет қаралды 42 МЛН
So Cute 🥰 who is better?
00:15
dednahype
Рет қаралды 19 МЛН
AI Is Making You An Illiterate Programmer
27:22
ThePrimeTime
Рет қаралды 176 М.
C++   Tutorial - 21 - Switch Statement
5:35
thenewboston
Рет қаралды 393 М.
C++ Tutorial - 8 - User Defined Functions
7:17
thenewboston
Рет қаралды 174 М.
C++  Tutorial - 14 - Do While Loop
5:02
thenewboston
Рет қаралды 201 М.
Math graphs but they get increasingly more stunning
8:03
The Math Wizard
Рет қаралды 184
C++   Tutorial - 16 - Creating a Pointer
4:30
thenewboston
Рет қаралды 132 М.
Мен атып көрмегенмін ! | Qalam | 5 серия
25:41