我正在创建日间服务器的引用.我正在从INI文件中读取选项,其文本如下:
[Server]
host =
port = 17
[Quotes]
file=quotes.txt
Run Code Online (Sandbox Code Playgroud)
但是,当我使用ConfigParser时,它会给我这个错误:
Traceback (most recent call last):
File "server.py", line 59, in <module>
Start()
File "server.py", line 55, in Start
configOptions = parseConfig(filename)
File "server.py", line 33, in parseConfig
server = config['Server']
AttributeError: ConfigParser instance has no attribute '__getitem__'
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
#!/usr/bin/python
from socket import *
from ConfigParser import *
import sys
class serverConf:
port = 17
host = ""
quotefile = ""
def initConfig(filename):
config = ConfigParser()
config['Server'] = {'port': '17', 'host': …Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个带字符串的函数,并分割每X个字符:
std::vector<std::string> DIFSplitStringByNumber(std::string s, int l)
{
const char *c = s.c_str();
char buffer[l];
std::vector<std::string> entries;
entries.reserve(int(s.length() / l) + 1);
int d = 0;
for(int i = 0; i < s.length() - 1;)
{
if(d != l)
{
buffer[d] = c[i];
d++;
i++;
}
else
{
entries.push_back(std::string(buffer, l));
//Clear array
memset(buffer, 0, l);
d = 0;
}
}
return entries;
}
Run Code Online (Sandbox Code Playgroud)
例如,如果我打电话DIFSplitStringByNumber("hello!", 2),我应该得到一个包含以下内容的向量:
[0] he
[1] ll
[2] o!
Run Code Online (Sandbox Code Playgroud)
但是,它似乎只得到前两个结果(向量大小为2),当我做类似的事情时DIFSplitStringByNumber("hello", 2),它崩溃了,大概是因为它试图访问一个不存在的数组索引(它需要6个字符,但是只有5).有更简单的方法吗?
我正在使用c#创建一个Windows窗体应用程序,如果传递包含该文件路径的参数,则可以在启动时打开该文件.
但是,它无法准确确定参数是否已通过.即使我没有传递任何参数,它仍然会尝试打开一个文件.
这是我的代码:
string[] args = Environment.GetCommandLineArgs();
if (args == null || args.Length == 0)
{
}
else
{
try
{
ListData ld = new LmReader.LmReader().readLmList(args[0]);
listItemsList.Items.Clear();
foreach (ListItemList li in ld.lil)
{
ListViewItem lvi = new ListViewItem(li.text);
lvi.Font = li.itemFont;
listItemsList.Items.Add(lvi);
}
filenameOpen = selectOpenLocation.FileName;
this.Text = "List Maker - " + Path.GetFileNameWithoutExtension(args[0]);
}
catch (Exception ex)
{
new Error().doError("Your list could not be opened.", ex);
}
}
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我正在为我的libspellcheck拼写检查库创建一个函数来检查文件的拼写.它的功能是读取文本文件并将其内容发送到拼写检查功能.为了使拼写检查功能正确处理文本,必须用空格替换所有换行符.我决定为此使用boost.这是我的功能:
spelling check_spelling_file(char *filename, char *dict, string sepChar)
{
string line;
string fileContents = "";
ifstream fileCheck (filename);
if (fileCheck.is_open())
{
while (fileCheck.good())
{
getline (fileCheck,line);
fileContents = fileContents + line;
}
fileCheck.close();
}
else
{
throw 1;
}
boost::replace_all(fileContents, "\r\n", " ");
boost::replace_all(fileContents, "\n", " ");
cout << fileContents;
spelling s;
s = check_spelling_string(dict, fileContents, sepChar);
return s;
}
Run Code Online (Sandbox Code Playgroud)
在编译库之后,我创建了一个带有示例文件的测试应用程序.
测试应用代码:
#include "spellcheck.h"
using namespace std;
int main(void)
{
spelling s;
s = check_spelling_file("test", "english.dict", "\n");
cout << "Misspelled …Run Code Online (Sandbox Code Playgroud) 我正在用C++创建加密/解密程序,我使用三个用户提供的数字来自定义加密.我在cplusplus.com上读到了关于isdigit()的内容,并根据它做了一个函数:
bool is_numeric(char *string)
{
int sizeOfString = sizeof(string);
int iteration = 0;
bool isNumeric = true;
while(iteration < sizeOfString)
{
if(!isdigit(string[iteration]))
{
isNumeric = false;
break;
}
iteration++;
}
return isNumeric;
}
Run Code Online (Sandbox Code Playgroud)
但是,它似乎不起作用.无论我给它一个数字,还是一个非数字字符,它仍然返回false.我的方法出了什么问题.
我正在尝试创建一个解析XML文件的函数,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<list name="Grocery List" author="Ian" desc="Saturday grocery list">
<item color="black" done="false">Milk</item>
<item color="black" done="false">Eggs</item>
<item color="blue" done="false">Water</item>
</list>
Run Code Online (Sandbox Code Playgroud)
它正确解析属性,但无法返回列表项的值.这是它使用的函数和类:
class List
{
public string[] listItems;
public string[] colorArray;
public string[] doneArray;
public string listName;
public string listAuthor;
public string listDesc;
public string err;
}
Run Code Online (Sandbox Code Playgroud)
读者定义:
class ListReader
{
public List doListParse(string filename)
{
List l = new List();
int arrayCount = 0;
try
{
XmlReader r = XmlReader.Create(filename);
while (r.Read())
{
if (r.NodeType == XmlNodeType.Element && r.Name …Run Code Online (Sandbox Code Playgroud) 我正在创建一个名为WebSpace的网站,我希望标题背景颜色从头到尾延伸.目前,它看起来像这样:

我想摆脱顶部,左侧和右侧的额外空白,尽可能使用干净的解决方案.
这是HTML:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="/assets/style.css"/>
<link rel="stylesheet" href="/assets/layout.css"/>
<title>WebSpace</title>
</head>
<body>
<div class="row title">
<span class="title">WebSpace</span>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
style.css文件:
@import url(http://fonts.googleapis.com/css?family=Roboto:400,700,100&subset=cyrillic,cyrillic-ext,latin-ext,latin);
body {
font-family: 'Roboto', sans-serif;
}
span.title {
font-weight: 100;
font-size: 66px;
width: 100%;
float: left;
}
Run Code Online (Sandbox Code Playgroud)
和我的layout.css文件:
body { overflow-x:hidden; !important}
html { width: 100%; }
div.row { width: 100%; }
div.span10 { width: 100%; }
div.span9 { width: 90%; }
div.span8 { width: 80%; }
div.span7 { width: 70%; }
div.span6 …Run Code Online (Sandbox Code Playgroud) 我有一个C++结构
typedef struct DIFColor
{
uint8_t r;
uint8_t g;
uint8_t b;
} DIFColor;
Run Code Online (Sandbox Code Playgroud)
我试图像这样初始化它:
DIFColor d = new DIFColor();
d.r = r;
d.g = g;
d.b = b;
Run Code Online (Sandbox Code Playgroud)
但编译器抱怨无法将指针转换为常规变量.什么指针?从这个问题我认为你可以简单地创建一个新的结构,而不必处理指针和动态内存分配new.它是否与我在C中初始化结构这一事实有关,而不是使用构造函数?
error: conversion from 'DIFColor*' to non-scalar type 'DIFColor' requested
Run Code Online (Sandbox Code Playgroud)