大家看这个函数怎么实现呢?

czdj2000 2006-11-14 07:34:17
小弟新进公司 要实现一个函数 没什么工作经验 遇到个困难
是这样的

有个字符型指针变量 他可能是这样的char* c=“1|9600|abcd”(以|分割成三部分)
函数的功能是解析出这个字符串把这三部分 分别放到一个结构体的三个成员当中
struct A{
char* i;
char* j;
char* k;
}a;

就是说要得到的结果是 a.i=1 a.j=9600 a.k=abcd

试了好多办法不行 希望得到指点
...全文
285 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
一分之千 2006-11-15
  • 打赏
  • 举报
回复
这个是我以前做的一个算法,就是对文章进行取读,然后进行一些判断什么的,你可以考虑把这个改一下用用,主要用了就是IO流,我只拷了一个片断,你自己改成你实用的应该可以的!
#include "stdio.h"
#include "fstream.h"
#include "string.h"




char letter;
char Current[30]={""};

fstream myFile("text.txt",ios::out|ios::in);
if(myFile.fail())
{
cerr<<"error opening file!\n";
return;
}
cout<<"The content of the file is:"<<endl<<endl;
letter=myFile.get();
while (!myFile.eof())
{
cout.put(letter);//输出内容
if(!(letter=='|'))
{
Current[n]=letter;
n++;
}
else
{
Current[n]='\0';
/*
可以在对current进行存取,保存到你的结构体里边
*/
Current[0]='\0';
n=0;


}
letter=myFile.get();

}
myskyx 2006-11-15
  • 打赏
  • 举报
回复
你那间是什么公司的啊
这么容易就进去了吗???????????????????????????????????????????????
我也想去
lovingyou 2006-11-14
  • 打赏
  • 举报
回复
#include "stdlib.h"
int parse(char* str,char t)
{
char cStr[10] = "\0";
int first = 0;
int i = 0;
while(str[i] != '\0')
{
if(str[i] == t)
{
memset(cStr,0,10);
if(first == 0)
strncpy(cStr,&str[first],i - first);
else
strncpy(cStr,&str[first+1],i-first-1);
first = i;
printf("%s\n",cStr);

}
i++;
}
printf("%s\n",&str[first+1]);
return 0;

}

int _tmain(int argc, _TCHAR* argv[])
{
char *content = "1|9600|abcd";
parse(content,'|');
return 0;
}
czdj2000 2006-11-14
  • 打赏
  • 举报
回复
谢谢楼上的
可是 我查了下 如果arm平台没有这个库函数呢 怎么办呢
一分之千 2006-11-14
  • 打赏
  • 举报
回复
#include <string.h>
#include <stdio.h>

char string[] = "1|9600|abcd";
char seps[] = "|";
char *token;
struct A{
char* i;
char* j;
char* k;
}a;

void main( void )
{
printf( "%s\n\nTokens:\n", string );
/* Establish string and get the first token: */
token = strtok( string, seps );
a.i=token;
token = strtok( NULL, seps );
a.j=token;
token = strtok( NULL, seps );
a.k=token;

printf( "%s %s %s \n", a.i,a.j,a.k );
}
可以实现啊,如果是数量比较多的话可以取字符长度,然后使用指针数组按数字索引进行赋值!
czdj2000 2006-11-14
  • 打赏
  • 举报
回复
这个函数我看不懂啊
另外我是嵌入式软件的
基于arm平台的 是不是不带这个库函数?
nule 2006-11-14
  • 打赏
  • 举报
回复
to 楼主:
就当我没写,如果给分的话,把这部分给wanfustudio
nule 2006-11-14
  • 打赏
  • 举报
回复
呵呵,wanfustudio(雁南飞:雁无留踪之意,水无取影之心) ,我们俩的方法一样
nule 2006-11-14
  • 打赏
  • 举报
回复
参考如下代码,使用strok字符串分割函数:
#include <cstring>
#include <cstdio>
char string[] = "A string\tof ,,tokens\nand some more tokens";
char seps[] = " ,\t\n";
char *token;

int main( )
{
printf( "%s\n\nTokens:\n", string );
token = strtok( string, seps );
while( token != NULL )
{
printf( " %s\n", token );
token = strtok( NULL, seps );
}
}
飞哥 2006-11-14
  • 打赏
  • 举报
回复
1.strtok
Example

/* STRTOK.C: In this program, a loop uses strtok
* to print all the tokens (separated by commas
* or blanks) in the string named "string".
*/

#include <string.h>
#include <stdio.h>

char string[] = "A string\tof ,,tokens\nand some more tokens";
char seps[] = " ,\t\n";
char *token;

void main( void )
{
printf( "%s\n\nTokens:\n", string );
/* Establish string and get the first token: */
token = strtok( string, seps );
while( token != NULL )
{
/* While there are tokens in "string" */
printf( " %s\n", token );
/* Get next token: */
token = strtok( NULL, seps );
}
}



69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧