C++ Filter Content from Text File - c++

I have been wanting to extract a line of text once [1],[2] ... [n] is found. But it seems like I couldn't get my thinking out to store a line into a char starting with [1].
void ExtractWebContent::filterContent(){
char str [10];
ifstream reading;
reading.open("file_Currency.txt");
while (!reading.eof()){
reading.get(str,10,'[1]');
cout << str << endl;
}
cout << str;
reading.close();
}
This is the file that I want to extract from..
CAPTION: Currencies
Name Price Change % Chg
[80]USD/SGD
1.2606 -0.00 -0.13%
USD/SGD [81]USDSGD=X
[82]EUR/SGD
1.5242 0.00 +0.11%
EUR/SGD [83]EURSGD=X
I am using linux, C++ programming. This is meant to filter figures obtained from HTML text file.
Any help would be very much appreciated. Thank you!

The big error you have is that you treat a single character as a string. The third argument is supposed to be a single character delimiter, i.e. a character that separates records in the file. If you add the compiler option -Wall when compiling you will get a warning about having more than one character in the single-character literal.
One way of doing what you want, is to use regular expressions.

Related

sscanf input not working

I have tab seperated records like this
1000 Muhammad Aashir 0213-4211685 123456 0
first I have read the line by using fgets and now i am trying to extract contents by using sscanf, but there is an unexpected problem... please help I am beginner
here is the code
char buffer[SIZE];
Account req;
while(fgets(buffer,SIZE,fptr))
{
cout<<endl<<buffer<<endl;
sscanf(buffer,"%d\t%s\t%s\t%s\t%ld\n",&req.acc_num,req.name,req.mobileno,req.pass,&req.acc_bal);
cout<<endl<<req.pass;
}
output of BUFFER is same as the record line
but after extracting values, when I am displaying the 'req.pass' the value is incorrect
req.pass is displaying '0213-4211685' but it has to display '123456'
sscanf will capture until reaching any kind of whitespace. In your case, req.name only contains Muhammad. This will cause the rest of your variables to contain the wrong info.
If you need to use sscanf(), you'll have to replace instances of " " in your name with an escape character, like "_" for example.

How to dismiss characters/integers from input text from file?

I have managed to write some code that can read from a .txt file, however I want my program to only read in important data.
For example, if my text file had the following data:
Name= Samuel
Favourite colour= Green
Age= 24
Gender= Male
I want my program to just read, and ignore everything before the "="
Samuel
Green
24
Male
I looked into the .substr() method, however, you need to know the exact position of the = sign.
This is my code, and it does not work
while ( getline (open_file,line) ){
for (int i=0; i<line.length(); i++){
if (line == "="){
cout << " " + (rest of the line;
}
I would really appreciate it if someone could help me out.
The most efficient way to read in data files is to read in a line at a time into a string variable. Next, extract the important parts.
Your data file looks like it is of the format:
<name> = <value>
I suggest you extract both name and value as strings (e.g. substrings), then you can pass the original data, in its original form, to other functions. Let the other functions worry about converting into integers or other data types.
The name field can be found by searching for the '=' and remembering the position of the '='. Next use the substring method and extract from the beginning of the string to the position before the '='.
The value is the substring that starts after the position of the '=' to the end of the string.
I'll let you look up the std::string functions and how to use them. I don't want to give you the code because you won't learn as much (such as how to look up functions).
See also std::getline.

C++ - Replace spaces with %20

I am looking for a way to prepare a string for use as a URL.
The basics of the code is you type in what you are looking for and it opens a browser with what you typed in. I am learning C++, so this is a learning program. And please be as specific as possible for I am new to C++.
Here is what I am trying to do:
cin >> s_input;
transform(s_input.begin(), s_input.end(), s_input.begin(), tolower);
s_input = "start http://website.com/" + s_input + "/0/7/0";
system(s_input.c_str());
But I am trying to replace all the spaces the user enter with a '%20'. I have found one method this way but it only works with one letter at a time, and I am needing to do it with a full string and not an array of chars. Here is the method I have tried:
cin >> s_input;
transform(s_input.begin(), s_input.end(), s_input.begin(), tolower);
using std::string;
using std::cout;
using std::endl;
using std::replace;
replace(s_input.begin(), s_input.end(), ' ', '%20');
s_input = "start http://website.com/" + s_input + "/0/7/0";
system(s_input.c_str());
Thanks for your help!
If you have Visual Studio 2010 or later you should be able to use regular expressions to search/replace:
std::regex space("[[:space:]]");
s_input = std::regex_replace(s_input, space, "%20");
Edit: How to use the six-argument version of std::regex_replace:
std::regex space("[[:space:]]");
std::string s_output;
std::regex_replace(s_output.begin(), s_input.begin(), s_input.end(), space, "%20");
The string s_output now contains the changed string.
You might have to change the replacement string to std::string("%20").
As you see I have only five arguments, that's because the sixth should have a default value.
std::replace is only able to replace single elements (chars in this case) with single elements. You are trying to replace a single element with three. You will need a special function to do that. Boost has one, called replace_all, you can use it like this:
boost::replace_all(s_input, " ", "%20");
If you google: C++ UrlEncode, you will find many hits. Here's one:
http://www.zedwood.com/article/111/cpp-urlencode-function

How do I parse a txt file which erases all the lines except what I need?

I want to parse a text file to get what I want and create another txt file in c++.
I have a text file which looks something like this.
User :
Group :
Comment1 :
Comment2 :
*** Label ***
ID : Nick
PASS : sky123
Number ID : 9402
*** End of Label ***
######################################
And goes on.
I basically want to create a new txt file which leaves all lines which contains colon(:) and erase the rest such as "* Label *", and save the result in a new txt file.
The answer to that txt file would be
User :
Group :
Comment1 :
Comment2 :
ID : Nick
PASS : sky123
Number ID : 9402
How do I do this in a simple way?
Thank you very much.
In C++ with fstreams:
ifstream input("input.txt");
ofstream output("output.txt");
string line;
while (getline(input, line)) {
if (line.find(":") != string::npos) {
output << line << "\n";
}
}
I have not tested this, but you get the idea.
void foo(ifstream& ifs, ofstream& ofs)
{
while ( !ifs.eof() )
{
string line;
getline(ifs, line);
if (line.find(":") != string::npos)
ofs << line;
}
}
The simplist approach I can think of would be to read in the text file line by line storing only the lines you want. Once the read is complete, open a separate file for writing and write the stored lines. It's not C++ but I've written out some psuedo code to illustrate.
while(line in source file)
{
if(wantline)
store the line
}
for(stored lines)
write line to destination file
In a big way, all you have to do is in a loop, check for each line if it have the char (:), if it does have it, add it to a string. In the end just save that string into a new text file.
Here's a tutorial of how to manage files
Well I don't write C++ and I'm not sure if you're looking for help with the language, but here's a simple approach in the abstract:
Load the text file into a string and then split the string into an array on the newline character so that you have 1 line of the text file per array value. Then, iterate over each element of the array. Using this method you can examine the contents of an individual line and use string comparison, regex matching, etc. to determine whether you want to keep/modify that line. Once you're done you can simply save the new string to a text file.
Alternately, you can use global find/replace with string comparison, regex etc. to apply changes to the entire document at once, but that requires more advanced knowledge and application of regex, and may not be practical given the size of the document.

GNU readline whitespace quoting

i´m working on an application which uses readline to read commands from stdin.
It accepts "cd", and other commands which require a path as an argument. I'm having troubles with paths that include whitespaces. My objective is to somehow make readline quote the whitespaces, and autocomplete the path name after this character appears(actually, when a space is encountered, it is just skipped, and autocompletion starts from the next word).
I've been trying to achieve this, but i keep trying things and none of them work. I've managed to quote a " " into a "\ ", which is what i want. But then readline doesn't interpret this as part of the path, it just skips it, and autocompletes the next word as if there was nothing before that. Basically, i'm expecting the same behaviour as bash's autocompletion.
Any help is appreciated.
Thanks in advance!
Edit:
Alright, so i've managed to somehow accomplish what i was looking for. What i did was:
During initialization:
rl_attempted_completion_function = completition;
rl_completer_quote_characters = "\"";
rl_filename_quote_characters = " ";
completition should return a char** containing every command that matches what "text" as so far. I've ommitted that part, since it doesn't have to do with what i was asking. The important part is the rl_filename_quoting_desired = 1; which tells readline that you want your filenames to be quoted.
char **completition(const char *text, int start, int end) {
rl_filename_quoting_desired = 1;
return 0;
}
Note that what i ended up doing is what BuHHu-nyx said, just adding double quotes(") to filenames.
Try to escape not spaces but the whole path. For example:
cd "/path/to/some where"