在程序当前目录下建一个txt文件bill.txt,文件内录入以下内容:
<>嗨,百度知道
运行程序后在当前目录下生成str.txt文件,可以提取。
程序代码如下:
#include
#include
#include
#include
#include
#define LEN 100 /* 字符数,可以根据需要自己定义 */
int main(void)
{
int fd;
long len,i,flag,j;
char num[LEN],string[LEN];
fd = open("bill.txt",O_RDWR);
len = read(fd,num,LEN); /* read string for bill.txt */
num[len] = '\0';
close(fd);
for(i=0,j=0;i { switch(num[i]) { case '<': { flag=1; break; } case '>': { flag=2; break; } default: { if(flag==2) { string[j]=num[i]; j++; } } } } string[j] = '\0'; fd = open("str.txt",O_RDWR|O_CREAT,S_IRUSR|S_IWUSR); if(fd) { write(fd,string,j); close(fd); } return 0; }
和楼上的思路差不多,先判断'<'获取当前,然后取index + sizeof("PAY_FLOWID>"),如果为真,取后面的字符串。再放入数组就容易了。
s[i]是字符,应该用%c,而s+i是指针型数据,与字符串是一个类型,可以用%s。
222