@@bob-ny6kn most professors explain most basic shit and when it comes to laboratories/exercises they ask you to do something that they didn't even say a fucking word about
Dude, your tutorial helped me so much, it's hard to find good, to the point information on this topic. I looked for a while before arriving here and you made it ridiculously simple. Thanks a lot, keep up the awesome work, I'll subscribe for this.
You can use the string stream class and getline to fallow a string until the '-' occurs string str = "a-b-c-d"; string wrd; stringstream ss (str) while ( getline ( ss, word, '-' ) ); cout
you'd create a struct, then assign it to an array, then loop through the array and use a line like "inputfile>>MyArray[counter].ID>>MyArray[counter].Mark;" depending on what your struct is called and for, then you can loop through the array to display it to the user.
Hey Bucky! thanks for your great videos. Ok I have a question: what if the separator in our file is not just a simple space? for instance its like this: 1 - Bucky - 18.45 (space dash space) in python it's pretty easy, but what about C++?
We can use Global variables for both writing and reading from a file. And then use switch statement to choose from writing or reading a file by putting read and write code in different functions.
say it bucky SAY it!!!!!! the "," sign when working with Exel sheets means insert in a new cell you could have saved 1 hour of my precious time searching for it!! :(:( Anyways you are still the best by far :P:P:P
@@sm4849 you can create another variable char to store the comma and put it between the main data structure. ex) int a; string b; double k; char x; while(theFile >> a >> x >> b >> x >> k){ bla2; }
@@tsatsralt9112 I found a better solution to it actually , I used the getline function and added the delimiter "," so it kept reading the values and output them this allowed me to put every data object into exclusive vectors for later as well
On Mac OS, you need to enter the folder names too. So if your username is MCFan and your project name is MCProject, you need to replace Bucky's line 7 with ifstream theFile("/Users/MCFan/CodeBlocks/MCProject/players.txt"); Your Codeblocks path may be different and you may need to change that too though...
GYW In this case, I guess he used " *filename* >>" to get an input from the file (been a long time since I watched it, sorry). The " *filename* >>" works just like "cin>>" which only inputs one word at a time. If you want to input entire sentences as a single string or an object of a class or struct, you use *filename*.getline command. PS. I'm not an expert in C++ and my knowledge has become slightly rusty over time. You might want to ask your professor or a friend. I don't recommend going to stackexchange. It's full of judgemental people who are more concerned with the originality and quality of your questions than answering to the content itself.
Here is a valid-ish answer: I believe it has to do with the fact that whenever you use 'cin' to input something in the console and type 2 words with a space between them, the program treats these words as if they are 2 separate inputs. Basically, it is just how c++ is.
Do you know how , c++ recognize a newline ? (I wanna loop through all lines , one after another , and each time do a specific computation on the strings(words) found in that line )
Hi Zectroy, It may be due to the non-existence of the file (players.txt) in your working directory. If indeed, this is the case, go ahead and open a notepad and just type (or copy and paste from the previous video file) what was originally in the text file. Proceed to Saving the text file in your working directory and close it. Re-Run the program, it should have the display.
why is it that when i run my program nothing is appearing other than the press any key to continue button. I am doing everything like you and it still is not working almost like it can't locate the file or something.
i don't understand the whole thing that is written in the while condition. the file is being read, but how does c++ know which is id, which is name and which is money? i mean, isn't it all just a bunch of strings in a text file for c++?
It is reading the file but also we are storing its content inside variables. The same way it knows when you cin them in order with the spaces separating each element.
he forgot to say that the deconstructor close the file at the end of the program. If you want to do something with this file before the end it's not gonna open it since you close it way at the end of the program.
Apostolis Kennedy hello, its a simple alternative. Both way are OK to go. To everyone else, eof means END OF FILE, which means in human language, the loop continues up till the end of file is reached ;)
+Apostolis Kennedy when i use this while loop i get an infinite amount of numbers printing, but when i use the bucky example its working just fine...why?
I have a problem.I used the ofstream in main just as we did in tutorial 66 but I kept it in main and used the ifstream code in a separate function and invoked it with a if statement in main.My problem is it deletes the content in the players text file everytime I access the ifstream function.
How about if you wanted to print into another txt. file rather that printing onto the console? how to read a certain column onto a txt. file and onto the console as well? thanks
@@kimhakkol7199 I guess an option would be creating a class or struct containing all the players information and then when you do the loop reading the file, you instance a new object or struct (depends on what you created before), and store it in a vector of its type with all the information, so later you can access the position of the vector you want (maybe searching by player id or something like that) and obtain the specific data you want to show. Or just looping through the vector and showing only the "name" information of each object, or whatever the information you want to show.
hey, everyone!!! so in line 13, the values are stored? so if I want to call the information to find the average of the bank account numbers I can use "double money"
hey bucky! if we want to read some text from the file and not the rest. like if we are marking attendance and the student wants o view only his attendance from the file. How do we do that?
okey, i have some problem, i made software for read and write. every time i write text to file.txt, the old text that i have create is lost, and changed with new text. anyone know how to keep the old text ???
+Farras Fauzan for that we need to use extra file parameters... ex- fstream file ; file.open("sample.txt" , ios::app) now this ios::app means to append the file which means its pointer will be placed at the end of the pre existing file and will allow you to contine your work... ios::app also allows to place your pointer anywhere in your file to edit it with help of pointers placer(seekp, seekg) ..hope this olves your problem... a little late but i recently completed my cs programming..ehehe
It is 2020 now and Man !! You did explain to me what none of my professors where able to show in lectures
Na. You were not paying attention.
Raoul, yes, I agree many professors can't explain this correctly.
about 2023 :).
@@bob-ny6kn most professors explain most basic shit and when it comes to laboratories/exercises they ask you to do something that they didn't even say a fucking word about
2024
Dude, your tutorial helped me so much, it's hard to find good, to the point information on this topic. I looked for a while before arriving here and you made it ridiculously simple.
Thanks a lot, keep up the awesome work, I'll subscribe for this.
Nice lesson. I used to do it with other more intricate code lines but this just surprised me with simplicity! Thanks!
I love your tutorials, Bucky! They've helped me so much in my C++ class this semester. Thank you!
This is so useful. Thanks so much! Now I can write and parse my own custom file structures!
9 years later and you still the goat
BUCKY .. WE ALL
Wow 10 years later i need this in college u are a life saver
It's 2023 and this is literally an amazing explanation
You can use the string stream class and getline to fallow a string until the '-' occurs
string str = "a-b-c-d";
string wrd;
stringstream ss (str)
while ( getline ( ss, word, '-' ) );
cout
you'd create a struct, then assign it to an array, then loop through the array and use a line like "inputfile>>MyArray[counter].ID>>MyArray[counter].Mark;" depending on what your struct is called and for, then you can loop through the array to display it to the user.
Increible. Has explciado en 10 minutos lo que no me han podido explicar en 30 horas
goddammit thank you once again bucky. I adjusted this program to process a .csv spreadsheet!
Your series rock. Keep up the great lessons!
how do you do that? please share
Nice Tutorial. You explain it in simple terms. Thank you.
Can't find words how helpfull your video was man, so usefull for filling classes, thanks a lot m8
Your C++ videos are lifesavers
watching your video is much bettern than any other books,thanks
Thank you soooo much for this video!!!! It really helped me with a problem I was stuck on for one of my programming labs.
Hey Bucky! thanks for your great videos.
Ok I have a question: what if the separator in our file is not just a simple space? for instance its like this: 1 - Bucky - 18.45 (space dash space)
in python it's pretty easy, but what about C++?
This was super helpful
We can use Global variables for both writing and reading from a file.
And then use switch statement to choose from writing or reading a file by putting read and write code in different functions.
saw the lord of the rings online damn bucky you a true master
Thank You Very Very much
You made me learn file handling very easily
Thank you for this very useful video!
Brilliant!
Still the best in 2024.
Amazing! I can actually do things with C++ after watching this.
ur tutorials r justttt Perfect
If you having trouble like me try
1. spelling txt not text.
2. put the text file you previously created in the same folder of your reading code.
thank you! i tried the second option and finally it worked!
You the goat for real
say it bucky SAY it!!!!!! the "," sign when working with Exel sheets means insert in a new cell
you could have saved 1 hour of my precious time searching for it!! :(:(
Anyways you are still the best by far :P:P:P
You really do a damn god job at your tutorials!
thanks a lot. it was very helpful for me and I got my answer which I couldn't find the solution. I really appreciate it.
It would just be crazy for Bucky to store his C++ Tutorial files in his "C++ Tutorials" folder, rather than his "Watermelon" folder.
how can we read comma separated values????
did you find a solution to this ?
@@sm4849 you can create another variable char to store the comma and put it between the main data structure.
ex)
int a;
string b;
double k;
char x;
while(theFile >> a >> x >> b >> x >> k){
bla2;
}
@@tsatsralt9112 I found a better solution to it actually , I used the getline function and added the delimiter "," so it kept reading the values and output them this allowed me to put every data object into exclusive vectors for later as well
@@sm4849 Please show me how you did this
@@kawaiionion1022 which part ?
Helped me so much thank you for this
This is like magic how does it know how to break each line down into the fields
wow, what a great video. I learned a lot here, thank you.
I think you might wanna add fstream on the title of this video. :) great tutorial, thanks bucky
"so you know not making that up" 😂
Hey bucky you do not know How Much I love you !!
woohoo so simple. thanks for the video😍
beautiful. thanks for making my life easier. god bless you
Thank you so much BUCKY
This is what I want to learn. Thank you!
Bucky saves me again.......... Thanks!
On Mac OS, you need to enter the folder names too. So if your username is MCFan and your project name is MCProject, you need to replace Bucky's line 7 with ifstream theFile("/Users/MCFan/CodeBlocks/MCProject/players.txt");
Your Codeblocks path may be different and you may need to change that too though...
@TheGgamer21 this is the first one that made me laugh, thumbs up if u agree
How does the program know that there is a space between id, name and money?
+Tanner Hibberd
The space is in the file. Each block of data is seperated from the other by a space or a newline.
+Mohammed Zaid Do you mean C++ omits all spaces and treat strings separated by space as independent strings?
GYW
In this case, I guess he used " *filename* >>" to get an input from the file (been a long time since I watched it, sorry).
The " *filename* >>" works just like "cin>>" which only inputs one word at a time. If you want to input entire sentences as a single string or an object of a class or struct, you use *filename*.getline command.
PS. I'm not an expert in C++ and my knowledge has become slightly rusty over time. You might want to ask your professor or a friend. I don't recommend going to stackexchange. It's full of judgemental people who are more concerned with the originality and quality of your questions than answering to the content itself.
Here is a valid-ish answer:
I believe it has to do with the fact that whenever you use 'cin' to input something in the console and type 2 words with a space between them, the program treats these words as if they are 2 separate inputs. Basically, it is just how c++ is.
Do you know how , c++ recognize a newline ? (I wanna loop through all lines , one after another , and each time do a specific computation on the strings(words) found in that line )
old is Gold
thank you !!. this helped a lot.
one word to describe you.
AWESOME :)
what if i have to read a string that includes a space??
great bunch of videos, even just showing how the layout of the data file should be was really useful... not something they show us in uni..
This was so so useful , ty!
@wh33l0fd00m That's when you usually use XML or another type where you can find the data using algorithms.
Excelente, me sirvio de mucho gracias!!!
Hi Zectroy,
It may be due to the non-existence of the file (players.txt) in your working directory. If indeed, this is the case, go ahead and open a notepad and just type (or copy and paste from the previous video file) what was originally in the text file. Proceed to Saving the text file in your working directory and close it. Re-Run the program, it should have the display.
Skillet: I need a hero to save me now !
Mr. Bucky,: here i am!
Bucky, is the variable name (eg id, name, money) stored in the .txt file? Or else how can it know the first group of text is the id, etc??
My program will only read the first line of the file, I have done it exactly how you demo. help?
Dude!, you rule.. the only tutorial I've found.
why is it that when i run my program nothing is appearing other than the press any key to continue button. I am doing everything like you and it still is not working almost like it can't locate the file or something.
Same. Did you ever figure it out?
Thanks a lot really helpful
Subbed for this! Lifesaver!!!
amazing tutorial! I need to do something similar but in java, does it work in java the same way?
i don't understand the whole thing that is written in the while condition. the file is being read, but how does c++ know which is id, which is name and which is money? i mean, isn't it all just a bunch of strings in a text file for c++?
it remembers the order
It is reading the file but also we are storing its content inside variables. The same way it knows when you cin them in order with the spaces separating each element.
I have to say it, my friend you are the best. Thank you SO much
u r the best, lov u
Why does mine doesn't show anything if I run it?
Gregor Bučar
Nope, my error is that my text file data have a comma. :D
Ah I forgot to make the file with ID player and money so mine was blank lol
How did that string compile without using the string header?
Edit: watch part 71, it will most likely answer your question.
he forgot to say that the deconstructor close the file at the end of the program. If you want to do something with this file before the end it's not gonna open it since you close it way at the end of the program.
wouldn't it be better to use ".eof" like:
...
while (!theFile.eof()) {
theFile >> id >> name >> money;
....
}
Apostolis Kennedy
hello, its a simple alternative. Both way are OK to go.
To everyone else, eof means END OF FILE, which means in human language, the loop continues up till the end of file is reached ;)
+Apostolis Kennedy when i use this while loop i get an infinite amount of numbers printing, but when i use the bucky example its working just fine...why?
Yes it is better, i might even suggest doing this:
while (!theFile.eof()){
if(theFile >> id >> name >> money;)
{...}
else
{cout
thanks man, helped a lot :)
I have a problem.I used the ofstream in main just as we did in tutorial 66 but I kept it in main and used the ifstream code in a separate function and invoked it with a if statement in main.My problem is it deletes the content in the players text file everytime I access the ifstream function.
@SofiaShapferOfficial It seems you only need those for modifying the file, not reading it.
How about if you wanted to print into another txt. file rather that printing onto the console? how to read a certain column onto a txt. file and onto the console as well?
thanks
can you make SDL tutorials if you can SDL?
Hey Bucky. How do you paste a screenshot from the clipboard into a file? Please help
what if you only wanted read all of the variables from the text file, but only display one of the players information?
have you got the answer? im having this problem now..
@@kimhakkol7199 I guess an option would be creating a class or struct containing all the players information and then when you do the loop reading the file, you instance a new object or struct (depends on what you created before), and store it in a vector of its type with all the information, so later you can access the position of the vector you want (maybe searching by player id or something like that) and obtain the specific data you want to show. Or just looping through the vector and showing only the "name" information of each object, or whatever the information you want to show.
Stacker Coding thank you so much! Your answer is a great help. ;)
Thank you
Could you use this to write a list of infix equations, then read it into an infix to post-fix conversion algorithm?
Hi, can you please tell me how to skip first few lines while reading from a file?
how do u take random numbers( about 200 of them) from a text file and read them after opening the text file using while, for or do while loops??
awesome video:)
Thank you!
how do you do the same when the text file contains commas (,) instead of space characters ( ) ??
hey, everyone!!! so in line 13, the values are stored?
so if I want to call the information to find the average of the bank account numbers I can use "double money"
What are you saying, that with ifstream obj we dont need to call the deconstructor, but with ostream we have to? Best regards
Cool tutorial, ty
hey bucky!
if we want to read some text from the file and not the rest.
like if we are marking attendance and the student wants o view only his attendance from the file. How do we do that?
it ran in error for the operator ">>" and "
@LaughingShinoo Watch the earlier tutorials, if the file does not exist it will automatically create it.
Can I manipulate File Pointer to move it to a new location in the file, as in start from the third row, in your case.
How to read an adjacency list from a text file?.The problem i have is each line has a variable input size.
how do you skip a line and then loop through the rest? xyz files have the annoying property of starting off with the amount of atoms.
I need help! My function is assigned to read first and last name but is also reading numbers as names and last names.
Bucky make game tutorials with SFML or Allegro! Its not hard at all once you learn C++
Thankyou sooooo Much man
okey, i have some problem, i made software for read and write. every time i write text to file.txt, the old text that i have create is lost, and changed with new text. anyone know how to keep the old text ???
+Farras Fauzan This is normal since it starts writing from the beginning of the file. Watch the tutorial after that and he explains it better.
+Farras Fauzan I have the same problem... :D
+Farras Fauzan for that we need to use extra file parameters...
ex-
fstream file ;
file.open("sample.txt" , ios::app)
now this ios::app means to append the file which means its pointer will be placed at the end of the pre existing file and will allow you to contine your work...
ios::app also allows to place your pointer anywhere in your file to edit it with help of pointers placer(seekp, seekg)
..hope this olves your problem... a little late but i recently completed my cs programming..ehehe
pranav anand If you know that so fucking well, why do you even watch c++ tutorials?? :D
Is destructor in filehandling for object of file in c++ , automatically close the file ?please reply as soon as possible.
I have problem when display the data. Someone can you help me? I need help. When display the data , the data display twice .