例如需要这个文本来写一个文件:
This is some simple text "it's quote", test test
我writeFile :: FilePath -> String -> IO ()用来写文件,但是当我尝试在文件中写这个字符串时,在文件中我看到:
This is some simple text \"it\'s quote\", test test
如何删除\符号,我只需要在我的文本文件中"没有\".
谢谢.
我在.bin\Debug文件夹中添加了一个.txt文件,我试图打开它并像这样读取它:
using (StreamReader reader = File.OpenText("Credentials.txt")) {
string line = null;
do {
line = reader.ReadLine();
if (line.Contains("host=")) {
. . .
Run Code Online (Sandbox Code Playgroud)
但是,虽然文件在那里,当我到达"ReadLine()"行时,它会停止在其轨道中:
System.NullReferenceException未处理Message = Object引用未设置为对象的实例.
我不得不将它从" do...while (line != null);" 更改为" while (! reader.EndOfStream)"
我使用a StringBuilder来创建一个File对象,但我也用它来查看该文件所在的目录是否存在:
StringBuilder sbFile = new StringBuilder();
sbFile.append("/home/logs/");
File oFile = new File(sbFile.toString());
if(!oFile.exists())
oFile.mkdir();
sbFile.append("MyLogFile.log");
oFile = new File(sbFile.toString());
Run Code Online (Sandbox Code Playgroud)
但我担心oFile在字符串生成器(/home/logs/vs /home/logs/MyLogFile.log)的两个不同"版本"上重用相同的引用会产生内存泄漏.如果是这样,我该怎么写这个呢?
我不知道为什么,但是当我尝试用file.read()读取文件时,Python无法识别文件的第一行..这是一个解释器错误,或者这是我的错?
你有一份程序副本(显示阅读结果):http://pastebin.ubuntu.com/1032832/
这是导致问题的代码:
if wfile.readline() != "#! /usr/bin/env python\n":
before = wfile.read()
wfile.seek(0)
wfile.write('#! /usr/bin/env python\n' + before)
wfile.close()
os.chmod(file, 777)
Run Code Online (Sandbox Code Playgroud)
我用于测试的Python版本是用于iOS的Python 2.5.1(Cydia端口).我的设备是iPad 2.
我怎么能改变它来确定其目录本身的文本文件?
string[] file = new string[] {
"abc",
"def",
"file3"};
var files = (UInt16)file.Length;
for (UInt16 n = 0; n < files; n++)
{
var streamReader =
new System.IO.StreamReader(
file[n] + ".txt");
...
streamReader.Close();
...
}
Run Code Online (Sandbox Code Playgroud) 我有许多文本文件,它们是固定的,重复的格式,如:
Q 32,0 16
q 27
b 21
I 0
P 1
d 0
m 31,0
Q 48,0 16
q 27
b 2
I 2
P 1
d 0
m 31,0
.
.
.
Run Code Online (Sandbox Code Playgroud)
我想用Java解析它们.我想知道的是解析这样一个文本文件的最快方法.如果这有助于提高性能,我可以更改文本文件的输出格式,因为这里唯一的要求是解析速度.我也可以使用外部库.
我想要一个解决方案,我想删除一个文件,使用asp.net c#驻留在我的桌面,我使用下面的代码:
try
{
FileInfo TheFile = new FileInfo(MapPath(".") + "\\" + FileNameTextBox.Text);
if (TheFile.Exists)
{
File.Delete(MapPath(".") + "\\" + FileNameTextBox.Text);
}
else
{
throw new FileNotFoundException();
}
}
catch (FileNotFoundException ex)
{
lblStatus.Text += ex.Message;
}
catch (Exception ex)
{
lblStatus.Text += ex.Message;
}
Run Code Online (Sandbox Code Playgroud)
但它总是说无法找到文件位置,请提前帮助我谢谢
我正在尝试从文本文件中读取最后一行.每行以数字开头,因此下次插入某些内容时,新数字将增加1.
例如,这将是一个典型的文件
1. Something here date
2. Something else here date
#next entry would be "3. something date"
Run Code Online (Sandbox Code Playgroud)
如果文件为空,我可以输入一个没有问题的条目.但是,当已经有条目时,我收到以下错误
LastItemNum = lineList[-1][0:1] +1 #finds the last item's number
TypeError: cannon concatenate 'str' and 'int objects
Run Code Online (Sandbox Code Playgroud)
这是我的功能代码
def AddToDo(self):
FILE = open(ToDo.filename,"a+") #open file for appending and reading
FileLines = FILE.readlines() #read the lines in the file
if os.path.getsize("EnteredInfo.dat") == 0: #if there is nothing, set the number to 1
LastItemNum = "1"
else:
LastItemNum = FileLines[-1][0:1] + 1 #finds the …Run Code Online (Sandbox Code Playgroud) 我正在使用C打开文件进行阅读.我有这个代码:
fp = fopen("./settings.cfg","r");
if (fp != NULL)
printf("OK");
else
printf("ERROR");
Run Code Online (Sandbox Code Playgroud)
但我总是得到一个错误.
该文件位于可执行文件所在的文件夹中.我试过只写"settings.cfg".可能是什么问题?
我有一个矢量,其大小可能非常大(100万个元素).我将向量的内容写为文件作为字节值.我无法弄清楚如何将字节值读回到向量中.
这是代码:
#include <fstream>
#include <vector>
#include <iterator>
#include <iostream>
using namespace std;
int main()
{
// Filling a vector with values
std::vector<bool> ve;
ve.push_back(true);
ve.push_back(false);
ve.push_back(true);
ve.push_back(false);
ve.push_back(true);
// Printing the values of the vector
for(unsigned int i = 0; i < ve.size(); i++)
cout << ve.at(i) << ".";
cout << endl;
// Writing the vector contents to a file
const char* file_name = "abc.txt";
ofstream outfile(file_name, ios::out | ios::binary);
outfile.write((const char*)&(ve[0]), ve.size());
outfile.close();
// Reading the file and …Run Code Online (Sandbox Code Playgroud)