如何在Linux中用C语言创建一个字符串(0x01 0x02 0x03……0x50)?

如何在Linux中用C语言创建一个字符串(0x01 0x02 0x03……0x50)?,第1张

首先,0x50比50大,其次你程序定义的两个指针无法存放字符串,只能运敏用来指向一个已经存在的册晌字符串。

#include <旁姿枝stdio.h>

#include <string.h>

#include <stdlib.h>

int main() { char data[0x50+1]={0}int i

for ( i=0i<0x50i++ ) data[i]=i+1

data[i]=0

printf("%s\n",data)

return 0

}

在程序当前目录下建一个txt文件bill.txt,文件内录入以下内容:

<pay_flowid>CTC4789</pay_flowid>

<>嗨,百度知道<dd>

<he和> hello linux

运行程念悄序后在当前目录下生成str.txt文件,可以提取。

程序代码如下:

#include <stdio.h>

#include <string.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#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=0i<leni++)

{

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

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11988842.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-20
下一篇 2023-05-20

发表评论

登录后才能评论

评论列表(0条)

保存