这是我为一个我正在研究的大型项目编写的测试程序.它与使用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) 我对正则表达式完全陌生,所以我在这里寻求一些帮助。
我在JDK 1.5下编译
以我从标准输入读取的这一行为例:
ab:Some string po:bubblegum
Run Code Online (Sandbox Code Playgroud)
我想做的是由两个字符和冒号分割。也就是说,一旦该行被分割并放入字符串数组中,这些术语应该是:
ab:Some string
po:bubblegum
Run Code Online (Sandbox Code Playgroud)
我现在有这个正则表达式:
String[] split = input.split("[..:]");
Run Code Online (Sandbox Code Playgroud)
这在分号处分开;我想要的是它匹配两个字符和一个分号,但在开始之前的空格处分开。这可能吗?
这是字符串数组的输出:
ab
Some String po
bubblegum
Run Code Online (Sandbox Code Playgroud)
我也读过有关 Pattern.compile() 的内容。这是我应该考虑的事情吗?