解析字符串C

发布于 2024-11-07 04:20:04 字数 857 浏览 0 评论 0原文

我正在尝试从电子邮件中获取正文,但我不知道如何获取。正文与标题之间用空格分隔。您能给我一些例子吗?

谢谢。

消息如下所示(带有标题和正文):

From username@localhost  Fri May 13 12:28:30 2010
Return-Path: <username@localhost>
X-Original-To: recipe@localhost
Delivered-To: recipe@localhost
Received: from cristi?localhost (localhost [127.0.0.1])
by Notebook (Postfix) with SMTP id 50F6F809E0
for <test@localhost>; Fri, 13 May 2010 12:28:30 +0300 (EEST)
Message-Id: <20110513092830.50F6F809E0@Cristi-Notebook>
Date: Fri, 13 May 2010 12:28:30 +0300 (EEST)
From: username@localhost
To: undisclosed-recipients:;

Text Body

.

到目前为止:

while ( buffer_recieved[begin]){
    if ( buffer_recieved[begin] == '\r' && buffer_recieved[begin+1] == '\n' ) {
         body[end++]=buffer_recieved[begin];
    }
    begin++;
}
body[end]=0;

I'm trying to get the body text from an email , but i don't know how. The body is separated with a space from header.Could you give me some examples ?

Thanks.

A message looks like this ( with header and body):

From username@localhost  Fri May 13 12:28:30 2010
Return-Path: <username@localhost>
X-Original-To: recipe@localhost
Delivered-To: recipe@localhost
Received: from cristi?localhost (localhost [127.0.0.1])
by Notebook (Postfix) with SMTP id 50F6F809E0
for <test@localhost>; Fri, 13 May 2010 12:28:30 +0300 (EEST)
Message-Id: <20110513092830.50F6F809E0@Cristi-Notebook>
Date: Fri, 13 May 2010 12:28:30 +0300 (EEST)
From: username@localhost
To: undisclosed-recipients:;

Text Body

.

So far :

while ( buffer_recieved[begin]){
    if ( buffer_recieved[begin] == '\r' && buffer_recieved[begin+1] == '\n' ) {
         body[end++]=buffer_recieved[begin];
    }
    begin++;
}
body[end]=0;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千寻… 2024-11-14 04:20:04

找到 POP RFC。阅读规格。

我没读过POP,但我读过SMTP。在 SMTP 中,我记得标头以“\r\n \r\n”结尾,正文也以“\r\n \r\n”结尾。也许对于 POP 来说也是如此。

Find the POP RFC. Read the spec.

I've not read POP, but I've read SMTP. In SMTP I think I recall the header ends in "\r\n \r\n" and the body likewise. Maybe it's the same for POP.

百思不得你姐 2024-11-14 04:20:04

你把这件事搞得太复杂了。你的身体开始 if(strnstr(buffer, "\r\n\r\n", sizeof(buffer)) != NULL)。现在,您只需要对缓冲区在“\r\n\r\n”序列中分割的情况下存在的 4 个异常进行编码。

另一种方法是进行行读取,直到读到“\r\n”,然后转到正文的缓冲区。这是更可取的,因为您可以更轻松地存储标头值,并且与较大缓冲区相比,执行行读取的开销可以忽略不计。

You're making this too complicated. Your body starts if(strnstr(buffer, "\r\n\r\n", sizeof(buffer)) != NULL). Now you only need to code the 4 exceptions that exist where the buffer splits within the "\r\n\r\n" sequence.

Another approach is to do line reads till you read "\r\n", then go to buffers for the body. This is preferable because you can store header values more easily and the overhead from doing line reads vs larger buffers is negligible.

源来凯始玺欢你 2024-11-14 04:20:04

如果我理解正确的话,正文由换行符分隔,因此我们可以保留一个变量来告诉我们是否已经遇到该换行符。在这种情况下,复制文本,否则检查我们是否已经到达它。

bool foundBody = false;
char *bodyBegin = "\r\n\r\n";
int i = 0,j = 0, k = 0;

while(bufferReceived[i]) {
    if(foundBody)
        body[j++] = bufferReceived[i];
    else {
        if(bufferReceived[i] == bodyBegin[k])
            foundBody = bodyBegin[k++] == '\0';
        else
            k = 0;
    }

    i += 1;
}

body[j] = '\0';

If I understood correctly, the body is separated by a newline, so we can keep a variable which tells us if we already met that newline. In that case copy the text, else check if we've reached it.

bool foundBody = false;
char *bodyBegin = "\r\n\r\n";
int i = 0,j = 0, k = 0;

while(bufferReceived[i]) {
    if(foundBody)
        body[j++] = bufferReceived[i];
    else {
        if(bufferReceived[i] == bodyBegin[k])
            foundBody = bodyBegin[k++] == '\0';
        else
            k = 0;
    }

    i += 1;
}

body[j] = '\0';
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文