如何以'\t'为分界符读取字段?

freewind88 2005-10-12 09:02:46
一个char input[2048]类型的字符串,其中的内容如下格式:0\t1\tx\t...
就如上面的格式,也就是说input[1]就是\t这个标志符,要怎么才能除去\t,把具体内容一个个读出来?
小弟对C不是很熟,最近用到,所以才来问这个简单问题,见晾
...全文
118 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Cnwanglin 2005-10-13
  • 打赏
  • 举报
回复
正确的代码: 我大哥写的;
==================================================================
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <string.h>

void main()
{
char input[2048] = "00000\t11111111111\t22\t33333333333\txxxxxxxxxxxxxxxx\tmmddd\trrrrrrrrrrrrr\ty";
char temp[2048];
memset(temp, 0, 2048);

char* p1 = input;
char* p2 = temp;
while(1)
{
*p2 = *p1;
if('\t' == *p1 || '\0' == *p1)
{
*p2 = '\0';
printf("%s\n", temp);
if('\0' == *p1)
break;
else
{
p2 = temp;
p1 ++;
continue;
}
}
p2 ++;
p1 ++;
}
}
=====================================================================
努力学习
Cnwanglin 2005-10-13
  • 打赏
  • 举报
回复
我晕 。。。。。。。。。。

刚才那个有BUG 。。。。。。。。。。。。。。。。。。。。。。。。。。。。

今天糊涂了

============================================================================
#include <stdlib.h>
#include <stdio.h>
#include <memory.h>
#include <string.h>

void main()
{
char input[2048] = "00000\t11111111111\t22\t33333333333\txxxxxxxxxxxxxxxx\tmmddd\trrrrrrrrrrrrr\ty";
char temp[2048];
memset(temp, 0, 2048);

char* p1 = input;
char* p2 = temp;
while('\0' != *p1)
{
*p2 = *p1;
if('\t' == *p1)
{
*p2 = '\0';
printf("%s\n", temp);
p2 = temp;
p1 ++;
continue;
}
p2 ++;
p1 ++;
if('\0' == *p1)
{
*p2 = '\0';
printf("%s\n", temp);
}
}
}
==========================================================================

测试通过了
Cnwanglin 2005-10-13
  • 打赏
  • 举报
回复
..................................................
==================================================

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

void main()
{
char input[2048] = "00000\t11111111111\t22\t33333333333\txxxxxxxxxxxxxxxx\tmm";
char temp[2048];
memset(temp, 0, 2048);

char* p1 = input;
char* p2 = temp;
while('\0' != *p1)
{
*p2 = *p1;
if('\t' == *p1)
{
*p2 = '\0';
printf("%s\n", temp);
memset(temp, 0, 2048);
p2 = temp;
}
p1 ++;
}
}
===========================================================================
Win2000/VC 6.0 下测试通过
===========================================================================

唉。。。愧对大哥阿 T_T
jsjjms 2005-10-13
  • 打赏
  • 举报
回复
for(i = 0; i<n, input(i) != '\t'; i++){....}
jixingzhong 2005-10-13
  • 打赏
  • 举报
回复
楼主的意思是说 \t 是规则的在每个字符后面都有的么 ?

那就读取的时候这样就好了啊 :
for (i = 0; i < n; i+=2) {...} // 2 字符跳跃就好了啊
wasoxi 2005-10-12
  • 打赏
  • 举报
回复
要除去 就得一个个读的吧
xiaocai0001 2005-10-12
  • 打赏
  • 举报
回复
\t也只是一个字符啊
当作一个普通字符处理得了

69,382

社区成员

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

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