searching through text files for specific line c++ - c++

So I have a txt file and I want to create a function to search through it and find a specific line. The txt file is a list of baby names and the frequency of the name being used. It looks like this:
jack,M,1456
julie,F,879
conner,M,540
ben,M,20843
Basically I want to have a function that will display the line of the name if the user inputs that name. So the user would be prompted to input a name, and I would use their input to search through the file until it finds the name. If julie were inputted by the user, the result would be the screen printing out julie,F,879.
Also, on that note, what if I had more than one txt file? For example:
file1.txt
jack,M,1456
julie,F,879
conner,M,540
ben,M,20843
file2.txt
jacob,M,15687
alex,F,5648
shannon,F,964
conner,M,760
How would I search through multiple txt files to find the line? If the user entered alex how would I get the function to search through two separate txt files?
And lastly (I know, this is dragging on..) If the user inputted conner, how would I get the program to display the lines from both txt files?
Thank you so much in advance for your help!

Just to give you a hint to start. You could use the stl ifstream class, where you can also extract formatted data. The stl gives you a lot of means to achieve what you want.

Related

How to read a specific line from a text file in c++?

C++ program that displays on the screen item codes with corresponding
item descriptions and prices. It asks the user to enter the code of the item
purchased by a customer. It looks for a match of the item code stored in items.txt.
How can I output only a specific line from a text file after the user inputs the item code?
You need to read the file line-by-line (std::getline), extract (depending on the exact format, e.g. by searching for a whitespace in the string) and compare the code and then return the corresponding line on a match.
It is not possible to access lines from a text file directly by index or content.
This is assuming that you mean the file contains lines in the form
code1 item1
code2 item2
//...
If the code is just the index of the line, then you only need to call std::getline in a loop with a loop counter for the current index of the line.
If you do this multiple times on the same file, you should probably parse the whole content first line-by-line into a std::vector<std::string> or a std::(unordered_)map<std::string, std::string> or something similar to avoid the costly repeated iteration.
Depending on the use case, maybe it would be even better to parse the data into a database first and then query the database, even if it is only e.g. sqlite or something like that.

Allow a user to not only input multliline text but move up and edit previously entered lines

Im working on a crude console based text editor in C++ kind of like nano. I've already figured out a basic way of inputting multiple lines of text and writing it to a file correctly (input ends when they enter a code: //end). However, at the moment the user is unable to move upwards (using arrow keys) and edit lines that they have entered. For some additional information I'm doing this with a getline() loop, writing files with ofstream, and am storing the users text in a string vector with each element being an entered line. How might I implement the ability to work with a body of text in such a way?
For advanced use, you need access to the console API.
For a simpler version, look at the primitive visual editors.
Editing a line consists of moving to the line, printing out the content, and then letting them insert or delete characters on that line.
Look at sed or ed or even vi.

Is there anyway redirecting (cin) input from txt file in console twice, for C++?

It is rather convenient to redirect input stream from different txt files in CMD console, since I don't need to write a file name and open it in my code. Like typing code.exe < input.txtin windows OS.
However, if I want to redirect two file like input1.txt and input2.txt to two input streams(cin>>) located in two positions of my code. How should I deal with it? I just know how to redirect once rather than multiple times. Any help would be appreciated.
Getting the two inputs in is the simple problem: TYPE file1.txt file2.txt > code.exe. But how would your first std::cin statement know which line is the last line of file1.txt ?
The normal solution is to use code.exe file1.txt file2.txt, and read both files using their own std::ifstream.

Find, move, replace and parse strings simultanuosly while building an .xml playlist file

I get many videos and I need to compile functioning .xml playlist files where they are all listed, including snapshot jpg's. Videos and snapshot images are named automatically. So I end up with lots of files like this:
hxxp://site.com/video/_5712.480p.flv
hxxp://site.com/video/_5712.480p.jpg
hxxp://site.com/video/_5713.480p.flv
hxxp://site.com/video/_5713.480p.jpg
So with these files I need to produce an .xml file looking something like this:
....
<track>
<title>5712.480p</title>
<creator>Whatever_5712.480p</creator>
<info>hxxp://site.com/video/_5712.480p.jpg</info>
<annotation>Playlist marked_480p</annotation>
<location>hxxp://site.com/video/_5712.480p.flv</location>
<image>hxxp://site.com/video/_5712.480p.jpg</image>
</track>
<track>
<title>5713.480p</title>
<creator>Whatever_5713.480p</creator>
<info>hxxp://site.com/video/_5713.480p.jpg</info>
<annotation>Playlist marked_480p</annotation>
<location>hxxp://site.com/video/_5713.480p.flv</location>
<image>hxxp://site.com/video/_5713.480p.jpg</image>
</track>
So I guess I might be looking at some advanced sed/awk procedure to copy, move and place the right strings inside the correct brackets, and to compile one whole file? I really appreciate all the help I can get on this one. Thx
With that input, you can do something like:
awk 'NR%2==1 && /\.jpg$/ {JPGFILE=$0}
NR%2==0 { print "whateverXMLtags" JPGFILE "whatanotherXMLtags" $0 "someotherXMLtags" }' INPUTFILELIST
So this assumes that jpg files are on odd numbered lines, and on that saves the name, and on every even line prints the desired output. Note that the SPACE between e.g. JPGFILE and "whatanotherXMLtags" concatenates the sring.

how to read from a text file contents file name extension in c++?

Hello I want to read from a text file full of directory contents
Here's my example:
below is my text file called MyText.txt
MyText.txt
title.txt,image.png,sound.mp3
I want to be able to read that .txt extension not the filename and I want it to be for file extensions only for example .txt or .mp3 how would I do that in c++?.
When I mean read I mean reference it in a if statement like this:
if(.mp3 exists in a text file)
{
fprintf(stderr,"sees the mp3 extensions");
}
I'm running Windows 7 32-bit.
I need a more cross platform approach.
May I suggest you to read a tutorial on C++ file handling and another one on C++ strings?
There is no a quick solution: you have to read the file using the ifstream class.
After reading the file and storing it in one or more strings, you can then use the find and substr string methods to create a queue of discrete filenames. Using the same methods, you can then split the queued elements again, in order to find the extensions and add them to a set. A set does not allow duplicates, so you are sure all the extensions will appear only once.