我想知道是否有人能告诉我如何将StreamReader指向程序当前工作目录中的文件.
EG说我有程序Prog,保存在目录C:\ ProgDir中.我将\ ProgDir提交到共享文件夹.里面的ProgDir是另一个包含我要导入Prog的文件的目录(例如\ ProgDir\TestDir\TestFile.txt)我想这样做,以便StreamReader可以读取那些TestFiles,即使目录的路径有改变;
(EG,在我的电脑上,Testfiles的路径是
C:\ ProgDir\TESTDIR\TESTFILE.TXT
但是在另一个人的计算机上,目录是
C:\ dev_code\ProgDir\TESTDIR\TESTFILE.TXT
).
如何从另一个人的计算机上的TestFile.txt中读取StreamReader?(澄清一下,文件名不会改变,唯一的变化就是路径ProgDir)
我尝试了以下方法:
string currentDir = Environment.CurrentDirectory;
DirectoryInfo directory = new DirectoryInfo(currentDir);
FileInfo file = new FileInfo("TestFile.txt");
string fullDirectory = directory.FullName;
string fullFile = file.FullName;
StreamReader sr = new StreamReader(@fullDirectory + fullFile);
Run Code Online (Sandbox Code Playgroud)
(从中获取:获取相对于当前工作目录的路径?)
但我得到"当前上下文中不存在TestFile".任何人都知道如何处理这个问题?
谢谢.
我正在寻找一种在C ...(100000000 - 999999999)中生成大约2 ^ 64的大随机数的方法,用于公钥加密算法(如p和q).
我不想生成小于2 ^ 64的数字(即小于100000000).
有什么能帮我做到这一点吗?
所以我知道如何使用htmlagilitypack选择一个节点:
HtmlNode.SelectNodes(".//div[@class='description']")
Run Code Online (Sandbox Code Playgroud)
等...但是说我有一个以下列方式设置的网站:
<a href="/link1/">This is Link 1</a>
<a href="/link2/">This is information i want to get to</a>
<a href="/link3/">This is Link 3</a>
<a href="/link4/">This is information i want to get to</a>
<a href="/link5/">This is Link 5</a>
<a href="/link6/">This is Link 6</a>
Run Code Online (Sandbox Code Playgroud)
等等...
现在,代码片段很短,但基本上,链接是不对称的,我只想访问具有文本值的链接
"这是我想要的信息"
(我对hmtl不熟悉,在这里使用适当的术语,抱歉).在htmlagilitypack中有一个方法可以检查这个文本值吗?
谢谢!
所以,我试图找到一种方法来fgets()C中的文本文件中的特定行,以将该行的内容复制到更永久的缓冲区:
基本上,我想知道是否有一种方法可以做到这一点,没有类似于以下代码:
FILE *fp;
fp = fopen(filename, "r");
char line[256];
char * buffer;
int targetline = 10;
while( targetline > 0)
{
fgets(line, 256, fp)
}
buffer =(char*)malloc(sizeof(char) * strlen(line));
strcpy(buffer, line);
Run Code Online (Sandbox Code Playgroud)
所以基本上我不想遍历文件n-1次只是为了到达第n行...它只是看起来效率不高(而且,这是作业,我需要得到100%哈哈) .
任何帮助,将不胜感激.
如此快速,奇怪的问题:我参加了考试,问题是:
char c = 'A'
char *p =&c
char **p2 = &p
void *v = &p2
Run Code Online (Sandbox Code Playgroud)
写下面表达式的类型(即int,void*等):
&vp2 + 1v[0]我回答了:
void **char**并失去了全部功劳.在我试图获得积分之前,我只想得到某人的第二意见.
我不太确定,但我认为,因为v是一个void指向内存地址的指针,地址v将是一个void**.v然而,如果被解除引用,那将是一个char****.
p2是一个char**,所以加1它仍然会成为一个char**.
v[0] 不存在.
如果有人可以帮我解决这种愚蠢的问题,我将非常感激.谢谢.
我正在研究java的一个极小的代码,我必须创建一个程序:1)大写输入句子的第一个单词,2)将单词"I"大写,3)如果有的话,标点句子没有正确的标点符号.我很容易编写代码,但它有点乱.具体来说,我想知道如何使用特殊字符作为条件.
例如,
String sentence = IO.readString(); /* IO.readstring is irrelevant here, it's just a scanning class that reads a user input*/
int length = sentence.length();
char punctuation = sentence.charAt(length - 1);
if (punctuation != "." || punctuation != "?" || punctuation != "!")
{
sentence = sentence + ".";
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译它时,这给了我一个不兼容的类型错误(不兼容的类型:char和java.lang.string)
我将如何写这个有条件的?
首先,我不是要求任何人为我编写程序或为我做作业......我有一个错误,我无法弄清楚是什么导致它,以及在哪里寻找解决它.
所以我试图创建一个程序,它将从命令行获取命令和输入文件,并根据命令选择执行以下几个函数之一:输入文件的行,计算单词输入文件,或检查输入文件的回文,然后将回文(如果有的话)与字典文件进行比较.
这是代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
void usage(char *prog_name) //usage file if input format is wrong
{
printf("Usage: %s, <-h>|<-l>|<-w>|<-p>, <filename>\n", prog_name);
exit(0);
}
void help(char *prog_name, char *filename) //help file for -h option
{
printf("Welcome to the help file\n");
printf("For number of lines, enter: <%s>, <-l>, <%s>.\n", prog_name, filename);
printf("For number of words, enter: <%s>, <-w>, <%s>.\n", prog_name, filename);
printf("To list the palindromes in alphabetical order, print the …Run Code Online (Sandbox Code Playgroud) 这是一个超级基本的问题(我今天脑死了):
如何使用正则表达式验证输入,以查看:1)输入是否为某种形式2)如果输入是全部大写(只是将输入转换为大写不可行)
我想要确保我的输入格式为XX_XX.这就是我所拥有的:
public bool IsKosher(string input)
{
Regex r = new Regex(input);
if(r.Matches([A-Z]_[A-Z]))
{
return true;
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
任何想法为什么它不编译?
谢谢!
所以我试图在python的列表中找到一个项目.这是我的功能:
def operator(input):
operatorlist = ['+', '-', '*', '/', '^', 'sin', 'cos']
for i in operatorlist:
if input is operatorlist[i]:
return True
Run Code Online (Sandbox Code Playgroud)
我的代码破了,我无法弄清楚为什么......任何想法?
我改变了我的代码:
def operator(input):
if input is '+' or input is '-' or input is '*' or input is '/' or input is '^' or input is 'sin' or input is 'cos':
return True
Run Code Online (Sandbox Code Playgroud)
因为我被告知,基本上,风格上是愚蠢的,以这种方式写它.
所以我试图创建一个表格的大型XML文件:
<xml>
<element id ="1">1</element>
<element id ="2">2</element>
<element id ="3">3</element>
...
<element id ="100000000">100000000</element>
</xml>
Run Code Online (Sandbox Code Playgroud)
使用C#.我似乎无法找到一种方法来格式化元素以在声明中包含id(我根本不熟悉XML).
有谁知道我怎么能在C#中做到这一点?
这是我的尝试:
using System;
using System.Xml;
using System.Linq;
using System.Text;
namespace BigXML
{
class Class1
{
static void Main(string[] args)
{
// Create a new file in C:\\ dir
XmlTextWriter textWriter = new XmlTextWriter("C:\\Users\\username\\Desktop\\testBigXmFile.xml", null);
textWriter.Formatting = Formatting.Indented;
textWriter.Indentation = 3;
textWriter.IndentChar = ' ';
// Opens the document
textWriter.WriteStartDocument(true);
textWriter.WriteStartElement("xml");
// Write comments
for (int i = 0; i < 100000000; i++) …Run Code Online (Sandbox Code Playgroud)