我出于性能原因使用Android NDK主要在C中创建应用程序,但似乎fopen等文件操作在Android中无法正常运行.每当我尝试使用这些功能时,应用程序崩溃.
如何使用Android NDK创建/写入文件?
我试图使用此代码建议(http://www.daniweb.com/forums/thread23883.html#)将控制台输出写入txt文件但是我没有成功.怎么了?
try {
//create a buffered reader that connects to the console, we use it so we can read lines
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
//read a line from the console
String lineFromInput = in.readLine();
//create an print writer for writing to a file
PrintWriter out = new PrintWriter(new FileWriter("output.txt"));
//output to the file a line
out.println(lineFromInput);
//close the file (VERY IMPORTANT!)
out.close();
}
catch(IOException e1) {
System.out.println("Error during reading/writing");
}
Run Code Online (Sandbox Code Playgroud) 我有一个包含十个文件的文件夹,我想循环播放.当我打印出文件的名称时,我的代码工作正常:
import os
indir = '/home/des/test'
for root, dirs, filenames in os.walk(indir):
for f in filenames:
print(f)
Run Code Online (Sandbox Code Playgroud)
哪个印刷品:
1
2
3
4
5
6
7
8
9
10
Run Code Online (Sandbox Code Playgroud)
但是如果我尝试在循环中打开文件,我会收到IO错误:
import os
indir = '/home/des/test'
for root, dirs, filenames in os.walk(indir):
for f in filenames:
log = open(f, 'r')
Traceback (most recent call last):
File "/home/des/my_python_progs/loop_over_dir.py", line 6, in <module>
log = open(f, 'r')
IOError: [Errno 2] No such file or directory: '1'
>>>
Run Code Online (Sandbox Code Playgroud)
我是否需要在循环内部传递文件的完整路径open()?
最近我一直在问编写一个函数读取二进制文件到std::vector<BYTE>哪里BYTE是unsigned char.我很快就找到了这样的东西:
#include <fstream>
#include <vector>
typedef unsigned char BYTE;
std::vector<BYTE> readFile(const char* filename)
{
// open the file:
std::streampos fileSize;
std::ifstream file(filename, std::ios::binary);
// get its size:
file.seekg(0, std::ios::end);
fileSize = file.tellg();
file.seekg(0, std::ios::beg);
// read the data:
std::vector<BYTE> fileData(fileSize);
file.read((char*) &fileData[0], fileSize);
return fileData;
}
Run Code Online (Sandbox Code Playgroud)
这似乎是不必要的复杂,并且char*我在呼叫时被迫使用的明确演员file.read并没有让我感觉更好.
另一种选择是使用std::istreambuf_iterator:
std::vector<BYTE> readFile(const char* filename)
{
// open the file:
std::ifstream file(filename, std::ios::binary);
// read the data:
return std::vector<BYTE>((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>()); …Run Code Online (Sandbox Code Playgroud) 我是 Rust 新手,浏览了一下源代码,发现了这个:
#[stable(feature = "fs_read_write_bytes", since = "1.26.0")]
pub fn write<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> io::Result<()> {
fn inner(path: &Path, contents: &[u8]) -> io::Result<()> {
File::create(path)?.write_all(contents)
}
inner(path.as_ref(), contents.as_ref())
}
Run Code Online (Sandbox Code Playgroud)
这个函数有什么理由定义这样的内部函数吗?为什么不直接写:
File::create(path.as_ref())?.write_all(contents.as_ref())
Run Code Online (Sandbox Code Playgroud) 我需要将一组DLL和PDB文件从一组文件夹中递归复制到另一个文件夹中.我不想在目标文件夹中重新创建文件夹层次结构.我想使用内置的Windows工具,例如DOS命令.
以下代码不生成文件(我无法在任何地方看到该文件).缺什么?
try {
//create a temporary file
String timeLog = new SimpleDateFormat("yyyyMMdd_HHmmss").format(
Calendar.getInstance().getTime());
File logFile=new File(timeLog);
BufferedWriter writer = new BufferedWriter(new FileWriter(logFile));
writer.write (string);
//Close writer
writer.close();
} catch(Exception e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud) 我正在使用文件上传选项上传文件.我在POST方法中直接将此文件从View发送到Controller,如,
[HttpPost]
public ActionResult Page2(FormCollection objCollection)
{
HttpPostedFileBase file = Request.Files[0];
}
Run Code Online (Sandbox Code Playgroud)
假设,我正在上传记事本文件.我如何阅读此文件并将此文本附加到字符串生成器,而不保存该文件....
我知道在SaveAs这个文件之后,我们可以读取这个文件.但是如何在HttpPostedFileBase不保存的情况下读取此文件?
file-io ×10
c++ ×2
file ×2
java ×2
android-ndk ×1
binaryfiles ×1
c ×1
c#-4.0 ×1
command-line ×1
console ×1
file-exists ×1
lua ×1
python ×1
rust ×1
stream ×1
vector ×1
windows ×1