Extract HTML source code linux library [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
i'm using Linux, i need my program to Extract the HTML source code and put it into a string using C++ language , can you give me a library that can do this

Well the easy solution is:
#include <string>
#include <iostream>
#include <stdio.h>
std::string execu(char* cmd) {
FILE* pipe = popen(cmd, "r");
if (!pipe) return "ERROR";
char buffer[128];
std::string result = "";
while(!feof(pipe)) {
if(fgets(buffer, 128, pipe) != NULL)
result += buffer;
}
pclose(pipe);
return result;
}
std::string result = execu("curl http://www.facebook.com");
But this is not considered safe unless you know the string passed is not going to blow anything up.

Related

How to write string to .txt file in C++ using FILE [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
I have string str = "6.5.1"
I want to write str to file .txt, but the result is ��j
Here my code
FILE *outfile = fopen("solution.txt", "w");
string test = "6.5.1";
fprintf(outfile, "%s\n", test);
I use string, FILE because I want to pass FILE as an argument, and convert string from another file to method.
How can I fix this problem?
Thanks.
p\s: sorry, tag is c++ not c
You clarified that you needed a c++ solution and required to use FILE *:
#include <cstdio>
#include <string>
int main(void) {
FILE *outfile = std::fopen("solution.txt", "w");
std::string test = "6.5.1";
std::fprintf(outfile, "%s\n", test.c_str());
std::fclose(outfile);
}

Just input a number in cpp [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm need to create id just number. For example: 12345678
If user inputs fail( contain char), delete char immediately.
For example: 123a =>input gain!
Please help me!
I think this is what you are looking for:
#include <iostream>
#include <string>
#include <cctype>
int main () {
std::string input;
bool valid;
do {
valid = true;
std::cin >> input;
for (char c : input)
if (! std::isdigit( static_cast<unsigned char>(c) ) )
valid = false;
} while (! valid);
// Here the string is guaranteed to be valid
}
Be aware though, that whatever you are trying to do, this does not look like the proper way to do it. There are ways to read numbers in c++, and this is not what I would recommend.

How can I use regex to determine the existence and size of groups of files in C/C++ on Linux? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a program on a Linux system that generates data log files with predictable file names that I need to access, but I have to do so through a C/C++ interface. The program accessing the data needs to be able to take a file path with a regex to specify certain file name ranges and do the following:
Determine if there are files matching the regex that exist
Determine the total size of all of the matching files
I'm using these as checks before I compress and transfer the files. How can I do this in C/C++?
You could do something like this:
#include <regex>
#include <string>
#include <experimental/filesystem>
// make using it sane
namespace fs = std::experimental::filesystem;
int main(int argc, char** argv)
{
// default to current directory
fs::path dir = argv[1] ? argv[1] : ".";
fs::directory_iterator dir_ent{dir};
fs::directory_iterator dir_end;
std::regex e{R"~(.*\.txt)~"};
std::smatch m;
decltype(fs::file_size("")) total_size = 0;
for(; dir_ent != dir_end; ++dir_ent)
{
// for some reason regex_match won't accept temporaries
std::string s = dir_ent->path().string();
if(!std::regex_match(s, m, e))
continue;
// deal with filepath here
total_size += fs::file_size(dir_ent->path());
// etc...
}
}

C++ command line argument verification [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
If provided with command line argument argv[], what is a way to determine whether or not that input is a file name.
E.g. If we are entering ex.txt into the command line, and printing out the contents, how can I write a conditional statement to determine whether the input for argv[1] is correct?
Thanks. Let me know if I was too vague. This is my first post, and english is not my first language.
You probably don't want to know only whether the file exists but also if you can open and read it. The only reliable way (And, by the way, the only way currently supported by standard C++.) to do this is to try opening the file and see if you succeed.
#include <fstream>
#include <iostream>
int
main(const int argc, const char *const *const argv)
{
for (int i = 1; i < argc; ++i)
{
std::ifstream istr {argv[i]};
if (!istr)
{
std::cerr << "error: " << argv[i] << ": cannot read file\n";
continue;
}
// Do something with the stream...
}
}
Be aware that if you close the file after verifying that it is good, there is no guarantee that it will still be good if you try to open it again later. Some other process could have deleted it or taken your permissions to read it.

Accessing data in a text file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How could I access a text file and go through word by word. I understand how to open the file but just not how to pull out each word one by one. I think it has something to do with arrays?
Simply:
#include <fstream>
#include <iostream>
int main()
{
std::fstream file("table1.txt");
std::string word;
while (file >> word)
{
// do whatever you want, e.g. print:
std::cout << word << std::endl;
}
file.close();
return 0;
}
word variable will contain every single word from a text file (words should be separated by space in your file).