我想在给定目录中只获取*.txt文件的名称,如下所示:
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <dirent.h>
int main(int argc, char **argv)
{
char *dirFilename = "dir";
DIR *directory = NULL;
directory = opendir (dirFilename);
if(directory == NULL)
return -1;
struct dirent *ent;
while ((ent = readdir (directory)) != NULL)
{
if(ent->d_name.extension == "txt")
printf ("%s\n", ent->d_name);
}
if(closedir(directory) < 0)
return -1;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能在纯unixs c中做到这一点?
我想编写一个简单的应用程序,它抓取 HTTPS 页面的内容:https://httpbin.org/html。
我知道如何访问它的不安全(HTTP)版本(http://httpbin.org/html),我只需要使用套接字发送一个请求:
std::stringstream ss;
ss << "GET /html HTTP/1.1" << "\r\n"
<< "Host: httpbin.org\r\n"
<< "Connection: close"
<< "\r\n\r\n";
std::string request = ss.str();
// socket creation, connect
send(sock, request.c_str(), request.size(), 0);
Run Code Online (Sandbox Code Playgroud)
它有效。但是,在使用安全套接字 ( #include <openssl/ssl.h>) 时出现以下错误:error:14094438:SSL routines:SSL3_READ_BYTES:tlsv1 alert internal error尝试时result = SSL_connect(ssl);
这是代码:
#include <sys/stat.h>
#include <netdb.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <string>
#include <iostream>
#include <sstream>
int main(int argc, char **argv)
{
int …Run Code Online (Sandbox Code Playgroud) 我想截取许多网页的截图,我写道:
from splinter.browser import Browser
import urllib2
from urllib2 import URLError
urls = ['http://ubuntu.com/', 'http://xubuntu.org/']
try :
browser = Browser('firefox')
for i in range(0, len(urls)) :
browser.visit(urls[i])
if browser.status_code.is_success() :
browser.driver.save_screenshot('your_screenshot' + str(i) + '.png')
browser.quit()
except SystemError :
print('install firefox!')
except urllib2.URLError, e:
print(e)
print('theres no such website')
except Exception, e :
print(e)
browser.quit()
Run Code Online (Sandbox Code Playgroud)
我收到了这个错误:
<urlopen error [Errno 111] Connection refused>
Run Code Online (Sandbox Code Playgroud)
如何解决?:)
编辑
当我在txt文件中有链接时,下面的代码不起作用:
from splinter import Browser
import socket
urls = []
numbers = []
with open("urls.txt", 'r') …Run Code Online (Sandbox Code Playgroud) 我有两张桌子。
i) 订单详情:
Run Code Online (Sandbox Code Playgroud)CREATE TABLE `order_details` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `content` text, `id_employee` INT(11) DEFAULT NULL, PRIMARY KEY (`id`), KEY `FK_id_employee` (`id_employee`), CONSTRAINT `FK_id_employee` FOREIGN KEY (`id_employee`) REFERENCES `employees` (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1;
ii) 员工:
CREATE TABLE `employees` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `firstname` text NOT NULL, `lastname` text NOT NULL, `salary` FLOAT DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=latin1 ;
Run Code Online (Sandbox Code Playgroud)
我想获取员工 3 负责的名字、姓氏和订单的详细信息。我用过这个:
SELECT lastname, firstname, content FROM order_details INNER JOIN …Run Code Online (Sandbox Code Playgroud) 我正在编写一个直接从用户输入读取数据的程序,并且想知道在按下键盘上的ESC按钮之前我怎样才能读取所有数据.我发现只有这样的东西:
std::string line;
while (std::getline(std::cin, line))
{
std::cout << line << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
但是需要添加一个可移植的方式(Linux/Windows)来按下ESC按钮,然后打破while循环.这该怎么做?
编辑:
我写了这个,但仍然 - 即使我按下键盘上的ESC按钮也能正常工作:
#include <iostream>
#include <string>
using namespace std;
int main()
{
const int ESC=27;
std::string line;
bool moveOn = true;
while (std::getline(std::cin, line) && moveOn)
{
std::cout << line << "\n";
for(unsigned int i = 0; i < line.length(); i++)
{
if(line.at(i) == ESC)
{
moveOn = false;
break;
}
}
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
EDIT2:
伙计们,这个洗衣液也不起作用,它从我的生产线上吃第一个炭!
#include <iostream>
#include <string>
using namespace …Run Code Online (Sandbox Code Playgroud) 我写了一个小的bash脚本来导出环境变量:
#!/bin/bash
echo "Pass a path:"
read path
echo $path
defaultPath = /home/katie/Desktop
if [ -n "$path" ]; then
echo "Path is empty! Exporting default path ..."
export my_var=$defaultPath
else
export my_var=$path
fi
Run Code Online (Sandbox Code Playgroud)
但是我得到了错误:
找不到defaultPath:命令
怎么解决?
WORKNG版本:
#!/bin/bash
echo "Pass a path:"
read path
echo $path
defaultPath=/home/user/Desktop
if [ -n "$path" ]; then
export my_var=$path
else
echo "Path is empty! Exporting default path ..."
export my_var=$defaultPath
fi
Run Code Online (Sandbox Code Playgroud)