在Python中以原子方式替换文件的推荐方法是什么?
即如果Python脚本被中断,则会出现断电等文件,并且很可能最终处于不一致状态(一半写入磁盘).
Linux/UNIX平台的解决方案是首选.
(我知道获得100%原子操作可能取决于您的文件系统,但至少使腐败的可能性降低)
我有一个C应用程序(VStudio 2010,win7 64位)在具有双xeon芯片的机器上运行,意味着12个物理核心和24个逻辑核心,以及192 gig的ram.编辑:操作系统是win7(即Windows 7,64位).
该应用程序有24个线程(每个线程都有自己的逻辑核心)进行计算并填充大规模C结构的不同部分.当所有线程都完成(并且线程完全平衡以便它们同时完成)时,结构大约为60千兆字节.
(我可以控制硬件设置,因此我将使用运行RAID 0的6个2tb驱动器,这意味着写入时的物理限制大约是平均顺序写入速度的6倍,或大约2千兆克/秒.)
将此更新到磁盘的最有效方法是什么?显然,i/o时间会使计算时间相形见绌.根据我对这个主题的研究,看起来像write()(而不是fwrite())是要走的路.但是,在设置缓冲区大小等方面,我可以在软件方面进行哪些其他优化?mmap会更有效吗?
这是我为一个我正在研究的大型项目编写的测试程序.它与使用fwrite()将struct数据写入磁盘然后用fread()读取该数据有关.结构的一个成员是动态分配的.
首先,这是我的代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STRING_LEN 128
struct Person {
int age;
char *name;
};
int main(int argc, const char *argv[])
{
struct Person *person = calloc(1, sizeof(struct Person));
person->age = 22;
person->name = calloc(STRING_LEN, sizeof(char));
char *name = "Name that is really, really, really, really, really, really, long.";
strncpy(person->name, name, STRING_LEN);
FILE *out_file = fopen("rw.out", "w");
fwrite(person, sizeof(struct Person), 1, out_file);
fclose(out_file);
FILE *in_file = fopen("rw.out", "r");
struct Person *person_read = calloc(1, sizeof(struct Person));
fread(person_read, …Run Code Online (Sandbox Code Playgroud) 我正在尝试将大量数据写入通过php中的fopen()打开的文件.我正在使用的协议包装器是ftp,因此该文件远程运行php代码的服务器.我正在写的文件是在Windows服务器上.
我确认该文件实际上是由我的php代码创建的,但问题是文件中的数据要么不存在(0KB),要么过早地写入文件.不知道为什么会这样.
这是我用来处理操作的代码:
$file_handle = fopen($node['ftp'].$path_to_lut, "wb", 0, $node['ftp_context']);
include_once($file);
if ($file_handle)
{
fwrite($file_handle, $string); //$string is inside included $file
fclose($file_handle);
} else {
die('There was a problem opening the file.');
}
Run Code Online (Sandbox Code Playgroud)
当我在本地计算机上托管它时,此代码可以正常工作,但是当我将其上传到我的webhost(Rackspace Cloud)时,它会失败.这让我相信这是一个与Rackspace我的服务器配置有关的问题,但是想知道我能用什么做些我的PHP代码来使它更强大.
确保fwrite实际完成将字符串写入远程机器的任何想法?
谢谢!
好的,我更改了写入文件的代码,如下所示:
if ($file_handle)
{
if ($bytesWritten = fwrite($file_handle, $string) ) {
echo "There were " . $bytesWritten . " bytes written to the text file.";
}
if (!fflush($file_handle)) {
die("There was a problem outputting all the data to the text file.");
}
if (!fclose($file_handle)) …Run Code Online (Sandbox Code Playgroud) 基本问题,但我希望这个结构占用13个字节的空间(1表示char,12表示3个无符号整数).相反,sizeof(ESPR_REL_HEADER)给我16个字节.
typedef struct {
unsigned char version;
unsigned int root_node_num;
unsigned int node_size;
unsigned int node_count;
} ESPR_REL_HEADER;
Run Code Online (Sandbox Code Playgroud)
我要做的是用一些值初始化这个结构并将它包含的数据(原始字节)写入文件的开头,这样当我打开这个文件后我才能重构这个结构并获得一些元数据有关文件其余部分包含内容的数据.
我正在初始化结构并将其写入文件,如下所示:
int esprime_write_btree_header(FILE * fp, unsigned int node_size) {
ESPR_REL_HEADER header = {
.version = 1,
.root_node_num = 0,
.node_size = node_size,
.node_count = 1
};
return fwrite(&header, sizeof(ESPR_REL_HEADER), 1, fp);
}
Run Code Online (Sandbox Code Playgroud)
node_size我实验时目前在哪里4.
在我向其编写结构后,该文件包含以下数据:
-bash$ hexdump test.dat
0000000 01 bf f9 8b 00 00 00 00 04 00 00 00 01 00 00 00
0000010
Run Code Online (Sandbox Code Playgroud)
我希望它实际上包含:
-bash$ hexdump …Run Code Online (Sandbox Code Playgroud) 如果您使用python写入文件,是否有任何方法可以使文本的某些部分变为粗体,斜体或下划线?
我试过了:
test = '/location/tester.rtf'
out_file = open(test,'w')
out_file.write('is this {\bold}?')
out_file.close() #thanks to the comment below
Run Code Online (Sandbox Code Playgroud)
是否可以通过python编写格式化的文本,如粗体,斜体或带下划线的文本?我觉得.rtf是最基本的格式化文本,但如果我错了,请纠正我
我有一个用C编写的小示例程序.我有一个main函数writeFile,它调用一个函数在二进制文件中写入一些数字.然后我调用overwrite0替换为0,最后打印结果.
这是代码:
#include <stdio.h>
/* Print the content of the file */
void printFile(){
printf("Read test.dat:\n");
int r;
FILE* fp = fopen("test.dat", "rb+");
if(fp) {
while(fread(&r,sizeof(int),1,fp)){
printf("%d\n", r);
}
}
fclose(fp);
}
/* Replace 0 with 1 */
void overwrite(){
int r;
FILE *fp = fopen("test.dat", "rb+");
if (fp) {
int i=0;
while (i < 4 && fread(&r, sizeof(int), 1, fp)) {
i++;
if (r == 0) {
r = 1;
fseek(fp,-sizeof(int),SEEK_CUR);
fwrite(&r,sizeof(int),1,fp);
}
}
} …Run Code Online (Sandbox Code Playgroud) 源代码:
main(void) {
unsigned char tmp[5] = {10, 10, 180, 255, 40};
FILE *ff = fopen("aa.bin", "w");
fwrite(&tmp, sizeof(char), 5, ff);
}
Run Code Online (Sandbox Code Playgroud)
执行并查看文件aa.bin的Hex内容时,它看起来像这样:
0D 0A 0D 0A B4 FF 28
为什么值10以两个字节(0D 0A)写入,而char类型仅保存1个字节.(0D)是什么意思?
我正在使用G ++编译器(Windows上的MinGW)和cpp11.
我有以下程序:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLEN 100
typedef struct {int key; char data[MAXLEN];} record;
main(int argc, char *argv[])
{
int n, i;
record x;
FILE *fp;
fp = fopen(argv[1], "w+");
printf("How many records will be entered? \n");
scanf("%d", &n);
for (i=0; i<n; i++)
{
printf("Enter record: \n");
scanf("%d", &x.key);
scanf("%s", &x.data);
fwrite(&x, sizeof(record), 1, fp);
}
}
Run Code Online (Sandbox Code Playgroud)
我正在做的是从用户输入创建记录,然后将这些"记录"存储到文件中.但是,当我使用fwrite()时,创建的文件中会写入许多奇怪的字符,而不是简单地让每条记录都包含其键和数据.有人能告诉我为什么写这些奇怪的人物吗?
假设我有以下代码:
fid = fopen(my_filename,'w','ieee-le','ISO-8859-1');
fwrite(fid,1,'short',10,'ieee-le')
Run Code Online (Sandbox Code Playgroud)
然后这将打开一个早期指定的文件,跳过前10个字节并将1写入以下两个.
但前十个字节会发生什么,假设打开的文件以前不存在?如果我要访问一个,我最终会得到什么?为什么?