Read A Specific Line From A File | C++ Example

  Рет қаралды 24,787

Portfolio Courses

Portfolio Courses

Күн бұрын

Пікірлер: 37
@natsue-bw1zu
@natsue-bw1zu 2 жыл бұрын
Ive spent 6+ hours and all it took as a 6 min video to solve this. Thanks for the amazing video
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You’re very welcome! :-) I’m glad to hear the video helped you out!
@Nixhekoo
@Nixhekoo 8 ай бұрын
same here!
@Nixhekoo
@Nixhekoo 8 ай бұрын
I've been searching for fstream tutorial everywhere but its so difficult to understand everywhere. You made it easy to understand! Props to you!
@PortfolioCourses
@PortfolioCourses 7 ай бұрын
I’m so glad it was helpful for you, and thank you for leaving this positive feedback too! :-)
@ct6502c
@ct6502c 2 жыл бұрын
This worked perfectly! The way you explained everything in a step by step way was a huge help. One other thing I really need help with though is how to search a text file for a string? I have looked everywhere and tried using Getline() and the search function in a loop, similar to what you did here...but I can't get it to work. All I would need it to do is tell which line in a text file the string was found.
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
I'm glad to hear it was helpful! 🙂 And that is a good question. I don't have a video on that yet, but I'm going to add it to my list of video ideas. You could check whether a string is in a text file at all by reading the entire contents of the file into a string: kzbin.info/www/bejne/p2qadmuohKuKibc. And then something like .find() should be able to help you find the string if it is there: cplusplus.com/reference/string/string/find/. But if you want to find the line number that will be a bit trickier. You could maybe use this approach here: kzbin.info/www/bejne/gWizmmOgfNOsp9k. And as you read each line, you could use .find() to try to find the string, and the counter variable that is being used for the array in that example could help you to know the line number if you find a match (you probably need to add 1 to it).
@ct6502c
@ct6502c 2 жыл бұрын
@@PortfolioCourses I got it! It turns out that it WAS working, there was just an unrelated problem with how I was using a variable. But yes, combining the method you explain here using a loop and keeping track of the line number, and using the find() function will search for a string AND tell you which line number it's on :)
@kirikor7
@kirikor7 2 жыл бұрын
Nice video, would be nicer if you kept the code a bit more zoomed out to get the big picture. Especially towards the end. Cheers
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
You can see the full code example here: github.com/portfoliocourses/cplusplus-example-code/blob/main/read_specific_line.cpp. Other people tell me I should zoom in more because they like to watch the videos on their phones, so it's always hard to tell what is best for people. 🙂
@comic-typ5919
@comic-typ5919 2 жыл бұрын
@@PortfolioCourses personally, I like it zoomed in :D
@moodymonstrosity1423
@moodymonstrosity1423 Жыл бұрын
The file.eof() function does not work correctly for reading objects from a file as it copies one extra object. Does it not do the same for reading text data?
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Great question! :-) The eof function really just tells us if we’ve reached the end of the file or not. Whether the program reads “extra” information or not is determined by how the program is written, not by the eof function. So in this example, we are not reading any “extra” text data.
@framo_official
@framo_official 10 ай бұрын
my test.txt just wont open.. i alos tried giving the whole path, did not work :(
@larenshboro3659
@larenshboro3659 Жыл бұрын
So the while loop reads the next line automatically if the current line is not same as line number and the end of file is not reached?
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Yes that's correct Larensh. :-)
@varadarajkamath404
@varadarajkamath404 2 жыл бұрын
How do I take a string (which is a substring of the file) as my user input which I want to search in the file and print out only the line containing that substring. Example: In my menu.txt file there is a line "Pizza cost: $4" In my program file I take a user input as "Pizza" and I want the entire line "Pizza cost: $4" to be displayed and not the other lines.
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
This is a great question Varadaraj, but I would almost need to make another video to answer this question. :-) You would need to read each line of the file somehow, maybe somewhat like in this video: kzbin.info/www/bejne/jpLNfHyEiNB_f6s. You don't necessarily need to store each line into a vector like in that video though. What you could do instead is use the find() function to look at each line/string, and try to find the string you are looking for in that line/string: stackoverflow.com/a/2340309. You could also store the lines into a vector or some other data structure, and then loop through the data structure elements afterwards and try to find the line/string containing the string you are looking for using the find() function. Good luck! :-)
@leythecg
@leythecg Жыл бұрын
premium content as always! Many thanks!
@ManOwaRR1
@ManOwaRR1 Жыл бұрын
Hello Sir, i've been looking everywhere for an algorithm , to find a specific word in a string on lines , example "hello world, welcome to c++, tutorial" , i want to search the file , let's say to find , "c++" , i wrote a search function , to look inside array or vector , result same it's only finding , the first element "hello" and disregarding the other elements, any tutorial about that would be appreciate it , big thank you .
@PortfolioCourses
@PortfolioCourses Жыл бұрын
So just so I'm understanding correctly... the program would read each line of the file and store it into a vector of strings. Then it would only keep those lines/strings in the vector which contain a string, like "C++"?
@ManOwaRR1
@ManOwaRR1 Жыл бұрын
@@PortfolioCourses Thanks for the reply , i worked around it yesterday , using for ranged loop , but to give u an overview of what was actually happening , i have filled with strings read from a file , was trying to iterate through it using std::vector ::iterator it , when i was iterating using it = std::find(vector.begin(),vector.,end(), match), it was only matching the first string element inside the vector , example "hello world", it matches the hello but never matches the "world" , not really sure why it's matching the first element , while it''s not matching the second element, hope that's clear , i can show the code , if needed , still would love a video about searching/replacing/deleting elements inside vectors , thank you
@PortfolioCourses
@PortfolioCourses Жыл бұрын
@@ManOwaRR1 I'm glad you figured it out, and thanks for the video ideas. 🙂
@pawemarkowski5387
@pawemarkowski5387 2 жыл бұрын
I need support. Location of the text file is: e:\tekst.txt . Program ask for the name of the file I want to read. Name of the file is written into string filename. Unfortunately I receive information " file failed to open ". How to give a path to the location of the file tekst.txt in the program line line -> plik.open(filename); ?
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Good question Pawel! :-) Maybe try this? plik.open("e:\\tekst.txt"); I think you might need to give the 'absolute path' of the file that includes e:\. But \ is a special character called the 'escape character' in C++ strings, we use it to output characters like the newline character with . To actually represent a \ character in a string we need to use \\.
@hariprasad-q6u
@hariprasad-q6u Жыл бұрын
Which editor ur using g
@PortfolioCourses
@PortfolioCourses Жыл бұрын
Visual Studio Code :-)
@zainuldin9095
@zainuldin9095 2 жыл бұрын
you could do while ( getline (file, line, ' ') ) { }
@PortfolioCourses
@PortfolioCourses 2 жыл бұрын
Thank you for sharing this approach! 🙂
@tioa.p.1058
@tioa.p.1058 Жыл бұрын
what does that syntax mean bro???
@tioa.p.1058
@tioa.p.1058 Жыл бұрын
i dont understand
@zainuldin9095
@zainuldin9095 Жыл бұрын
@@tioa.p.1058 checkout getline docs at cpp reference
@zainuldin9095
@zainuldin9095 Жыл бұрын
@@tioa.p.1058 from cpp reference docs Parameters: input - the stream to get data from str - the string to put the data into delim - the delimiter character Return value: input
@i_m_wali
@i_m_wali Жыл бұрын
You made it So freeking ez!!!
@elinesiobrito3333
@elinesiobrito3333 Жыл бұрын
Good 🤠
@ShadyOk
@ShadyOk 2 ай бұрын
life saver
Replace A Specific Line In A File | C++ Example
9:50
Portfolio Courses
Рет қаралды 12 М.
Как Ходили родители в ШКОЛУ!
0:49
Family Box
Рет қаралды 2,3 МЛН
Every team from the Bracket Buster! Who ya got? 😏
0:53
FailArmy Shorts
Рет қаралды 13 МЛН
Linear Algebra | Rotating a parabola with matrices
5:16
Simple ScTEM
Рет қаралды 336
How To Use ifstream To Read Files | C++ Tutorial
12:31
Portfolio Courses
Рет қаралды 14 М.
Read CSV File Data Into An Array Of Structs | C Programming Example
11:05
Portfolio Courses
Рет қаралды 45 М.
C++ Tutorial:  Read from Text Files  // Retrieve data from files using ifstream
21:12
How to Parse a CSV File in C++
13:38
Code Morsels
Рет қаралды 37 М.
why do header files even exist?
10:53
Low Level
Рет қаралды 442 М.
Delete A Specific Line In A File | C++ Example
8:57
Portfolio Courses
Рет қаралды 14 М.
Output File Streams in C++ (Writing to Files)
13:47
Programming with Dr. Hayes
Рет қаралды 39 М.
Making a Game With C++ and SDL2
8:14
PolyMars
Рет қаралды 1,7 МЛН
Read And Store All File Lines Into A Vector | C++ Example
6:15
Portfolio Courses
Рет қаралды 15 М.