小编liu*_*uan的帖子

C在不同文件中定义的相同全局变量

我正在这里阅读这段代码(中文).有一段关于在C中测试全局变量的代码.该变量a已在文件t.h中定义,该文件已包含两次.在文件中foo.c定义了一个struct b带有一些值和一个main函数.在main.c文件中,定义了两个没有初始化的变量.

/* t.h */
#ifndef _H_
#define _H_
int a;
#endif

/* foo.c */
#include <stdio.h>
#include "t.h"

struct {
   char a;
   int b;
} b = { 2, 4 };

int main();

void foo()
{
    printf("foo:\t(&a)=0x%08x\n\t(&b)=0x%08x\n
        \tsizeof(b)=%d\n\tb.a=%d\n\tb.b=%d\n\tmain:0x%08x\n",
        &a, &b, sizeof b, b.a, b.b, main);
}

/* main.c */
#include <stdio.h>
#include "t.h"

int b;
int c;

int main()
{
    foo();
    printf("main:\t(&a)=0x%08x\n\t(&b)=0x%08x\n
        \t(&c)=0x%08x\n\tsize(b)=%d\n\tb=%d\n\tc=%d\n", …
Run Code Online (Sandbox Code Playgroud)

c linker one-definition-rule

15
推荐指数
1
解决办法
1万
查看次数

如何使用linux命令sed处理Little-endian UTF-16文件

我正在开发一个关于windows rdp的应用程序.现在,当我尝试使用sed命令直接替换rdp文件中的IP地址字符串时,我遇到了问题.但执行此命令后,原始rdp文件出现乱码.

sed -i "s/address:s:.*/address:s:$(cat check-free-ip.to.rdpzhitong.rdp)/" rdpzhitong.rdp
Run Code Online (Sandbox Code Playgroud)

我发现该文件的格式是Little-endian UTF-16 Unicode.

在此输入图像描述

我还可以使用sed命令正确替换文件中的文本吗?还是其他方法来处理这个问题?

shell utf-16 endianness

4
推荐指数
1
解决办法
4920
查看次数

python多线程与fcntl flcok处理文件

我尝试使用python处理文本替换问题。有一个Little-endian UTF-16格式的文件,我想替换此文件中的IP地址。首先,我逐行读取此文件,然后替换目标字符串,最后,我将新字符串写入文件。但是使用多线程操作此文件时,该文件将出现乱码。这是我的代码。

import re
import codecs 
import time
import thread
import fcntl

ip = "10.200.0.1" 
searchText = r"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" 

def replaceFileText(fileName,searchText,replaceText,encoding):
    lines = []
    with codecs.open(fileName,"r",encoding) as file:
        fcntl.flock(file,fcntl.LOCK_EX)
        for line in file:
            lines.append(re.sub(searchText,replaceText,line))
        fcntl.flock(file,fcntl.LOCK_UN)

    with codecs.open(fileName,"w",encoding) as file:
        fcntl.flock(file,fcntl.LOCK_EX)
        for line in lines:
            file.write(line)
        fcntl.flock(file,fcntl.LOCK_UN)

def start():
    replaceFileText("rdpzhitong.rdp",searchText,ip,"utf-16-le")                                                                 
    thread.exit_thread()

def test(number):
    for n in range(number):
        thread.start_new_thread(start,())
        time.sleep(1)

test(20) 
Run Code Online (Sandbox Code Playgroud)

我不明白为什么文件出现乱码,我使用fcntl flock保持读取/写入顺序,问题出在哪里?

python multithreading

2
推荐指数
1
解决办法
684
查看次数