Problem doing an e-mail program in C

Discussion in 'C Programming' started by tyler_durden, Dec 28, 2004.

  1. tyler_durden

    tyler_durden Guest

    hi there peeps...
    like I say in the topic, I need to do an e-mail program in C language, and
    has to be made until the 3th of january..the problem is I'm having some
    problems with it.
    I was wondering if someone had a program like this..it has to send
    e-mails, read e-mails from a .txt file and show them in the screen with
    the subject, date, etc...
    can someone please help me out?
    thanks
     
    tyler_durden, Dec 28, 2004
    #1
    1. Advertising

  2. tyler_durden <> scribbled the following:
    > hi there peeps...
    > like I say in the topic, I need to do an e-mail program in C language, and
    > has to be made until the 3th of january..the problem is I'm having some
    > problems with it.
    > I was wondering if someone had a program like this..it has to send
    > e-mails, read e-mails from a .txt file and show them in the screen with
    > the subject, date, etc...
    > can someone please help me out?
    > thanks


    Sending or reading e-mails is not possible in ISO standard C. Please ask
    in a newsgroup dedicated to your own implementation.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-------------------------------------------------------- rules! --------/
    "All that flower power is no match for my glower power!"
    - Montgomery Burns
     
    Joona I Palaste, Dec 28, 2004
    #2
    1. Advertising

  3. tyler_durden

    Flash Gordon Guest

    On Tue, 28 Dec 2004 17:06:22 -0500
    "tyler_durden" <> wrote:

    > hi there peeps...
    > like I say in the topic, I need to do an e-mail program in C language,
    > and has to be made until the 3th of january..the problem is I'm having
    > some problems with it.
    > I was wondering if someone had a program like this..it has to send
    > e-mails, read e-mails from a .txt file and show them in the screen
    > with the subject, date, etc...
    > can someone please help me out?
    > thanks


    Yes, lots of people could help you. For 50UKP I'll send you the source
    for a program that does all this and more. I'll even give you the
    following advice for free.

    1) All the network stuff is beyond the scope of the C language and hence
    off topic here.

    2) Any sophisticated screen handling you might want is beyond the scope
    of the C standard and hence off topic here.

    3) Around here we tend to help people fix on-topic code that they post,
    not write it for people.

    4) You will certainly need advice from people who know the OS you are
    programing for, so you will have questions that should be asked on an OS
    specific group.

    So I suggest you try to start on the program and find groups where
    specific questions are topical. To do the latter you will have to read
    the FAQ and a weeks worth of posts for any group you think might be
    appropriate before posting.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Dec 28, 2004
    #3
  4. "tyler_durden" <> writes:
    > hi there peeps...
    > like I say in the topic, I need to do an e-mail program in C language, and
    > has to be made until the 3th of january..the problem is I'm having some
    > problems with it.
    > I was wondering if someone had a program like this..it has to send
    > e-mails, read e-mails from a .txt file and show them in the screen with
    > the subject, date, etc...


    The January 3 deadline makes me suspect this is a homework assignment.
    There are enough existing e-mail programs floating around that I don't
    believe someone would ask you do to this as part of a paying job; if
    it's a hobby there shouldn't be a deadline. (I'm assuming that's what
    "has to be made until the 3th of january" means; I hope you're taking
    classes in English as well as in C.)

    If this is homework, just send us your instructor's e-mail address,
    and we'll consider writing the program and sending it directly. (You
    wouldn't expect us to do your work for you and not get any credit for
    it, would you?)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Dec 29, 2004
    #4
  5. tyler_durden

    infobahn Guest

    tyler_durden wrote:
    > hi there peeps...
    > like I say in the topic, I need to do an e-mail program in C language, and
    > has to be made until the 3th of january..the problem is I'm having some
    > problems with it.


    Ah, so the problem is that you're having problems. Okay. What problems
    might they be?

    > I was wondering if someone had a program like this..it has to send
    > e-mails, read e-mails from a .txt file and show them in the screen with
    > the subject, date, etc...


    Many of us have email clients, yes. Some of us even have the C source
    for email clients. But I hope none of us think it would be a good idea
    to send you such source code simply to help you fake your way through
    an assignment.

    > can someone please help me out?


    Sure. The whole of your program, with the exception of the sockets bit
    (and it's not clear from your article whether you're supposed to do a
    "real" email client or something that merely imitates one using files),
    can be written in ISO C if you're clever.

    So write it in ISO C, and then let us know if you have problems.

    If you decide to go for flashy
    stuff like graphics, then you'd have to ask about problems with the
    flashy stuff in a newsgroup where flashy stuff is topical, such as
    comp.os.ms-windows.programmer.win32.
     
    infobahn, Dec 29, 2004
    #5
  6. tyler_durden

    tyler_durden Guest

    thanks a lot for all your replyes.
    the program is simplier than that, because it doesn't need to actyually
    send the e-mails..
    I have the sheet with the specifications it has to have, but it's in
    portuguese....
    I've already made some of it, the help menu, and the command that asks you
    for the adress, the subject and the message..
    then when you want to exit the program if you press "x" it exits without
    changing anything, but with "q" it has to keep all of the sent messages in
    a file with a certain structure...
    and it has to be able to present the mails in the file by pressing
    "h"...it shows each mail, and its subsject,date and adress..then you can
    delete the mails, os view them...
    Yes, its a homework assignment.
    all the help you can give me is appreciated, and if you think it will help
    I could put my work done till now online, for you to view it...
    thanks a lot...
     
    tyler_durden, Dec 29, 2004
    #6
  7. tyler_durden

    tyler_durden Guest

    If you could at least send me a source code for a simple e-mail program it
    would be great....just to get some ideas of it..
     
    tyler_durden, Dec 29, 2004
    #7
  8. On Wed, 29 Dec 2004 07:42:40 -0500, in comp.lang.c , "tyler_durden"
    <> wrote:

    >If you could at least send me a source code for a simple e-mail program it
    >would be great....just to get some ideas of it..


    This is offtopic. I suggest you go look in the gnu archives, or at
    sourceforge. The implementation of blat might be interesting.


    --
    Mark McIntyre
    CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
    CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
     
    Mark McIntyre, Dec 29, 2004
    #8
  9. On Tue, 28 Dec 2004 17:06:22 -0500, "tyler_durden"
    <> wrote:

    >hi there peeps...
    >like I say in the topic, I need to do an e-mail program in C language, and
    >has to be made until the 3th of january..the problem is I'm having some
    >problems with it.


    Dude, you really need to get started on your assignments earlier. I'm
    sure your instructor didn't assign this problem just today?

    What are the problems you're having with your code? Procrastination
    aside, I mean. Post relevant sections of your code and indicate where
    you're having difficulty.


    --
    Robert B. Clark (email ROT13'ed)
    Visit ClarkWehyr Enterprises On-Line at http://www.3clarks.com/ClarkWehyr/
     
    Robert B. Clark, Dec 29, 2004
    #9
  10. tyler_durden

    Novitas Guest

    It sounds like you are having problems with specific things (handling
    file storage and retrieval maybe? Something else?) but you have not
    identified the specific things you are having issues with.

    It would be more helpful to start from there and identify those
    specific issues to the group.
     
    Novitas, Dec 29, 2004
    #10
  11. tyler_durden

    tyler_durden Guest

    Specific problem...now you can help =)

    I made this command but it gives me a "segmentation fault" error...can you
    help me?

    -------------------------

    #include <stdio.h>

    typedef struct mail
    {
    char from[120];
    char to[120];
    char sub[120];
    char msg[120];
    } MAIL;

    void comandom(MAIL *ptr)
    {
    printf("From: \n"); gets(ptr->from);
    printf("To: \n"); gets(ptr->to);
    printf("Subject:\n"); gets(ptr->sub);
    printf("Message:\n"); gets(ptr->msg);


    }

    ---------------------------

    and I need to implement the time and date when I write each e-mail..I have
    the function which is:
    -------------------
    #include<stdio.h>
    #include<time.h>
    #define MAX 120

    void tempo(char *tempo){

    time_t horact;

    time(horact);

    ctime_r(horact,tempo);
    }


    -----------------------------

    but I dunno how to use it..
    thanks a lot!
     
    tyler_durden, Dec 29, 2004
    #11
  12. tyler_durden

    tyler_durden Guest

    What I have until now

    ok..so what I did until now is this...
    the main file is this one:
    ------------------------------(progmail.c)----
    #include <stdio.h> /* para funcoes como printf, scanf, etc */
    #include <stdlib.h> /* para a funcao exit */
    #include <string.h> /* para funcoes relacionadas com strings */
    #define DIMMAIN 20

    void comandom(void);
    void comandoC(void);
    void comandoL(void);

    int help(){

    printf ("Help \n \n");
    printf ("Mail Commands \n \n");


    printf("------------------------------------------------------------------------\n");
    printf("| t <message list> type messages
    |\n");
    printf("| d <message list> delete messages
    |\n");
    printf("| R <message list> reply to message senders
    |\n");
    printf("| r <message list> reply to message senders and all
    recipients |\n");
    printf("| m <user list> mail to specific users
    |\n");
    printf("| q quit, saving unresolved messages in mbox
    |\n");
    printf("| x quit, do not remove system mailbox
    |\n");
    printf("| h print out active message headers
    |\n");

    printf("------------------------------------------------------------------------\n");

    return 0;

    }



    int main (){
    char c;

    fopen("mail.txt","r");

    printf("E-mail program.\n");


    do{
    putchar ('&');
    scanf("%s",&c);


    switch (c)
    {
    case '?':help();break;
    case 't':printf("ok\n");break;
    case 'd':printf("ok\n");break;
    case 'R':printf("ok\n");break;
    case 'r':printf("ok\n");break;
    case 'm':comandom();break;
    case 'q':printf("ok\n");break;
    case 'x':printf("Program shutting down!\n"); break;
    case 'h':printf("ok\n");break;
    case 'C':comandoC();break;
    case 'L':comandoL();break;
    default: printf("Unknown command!\n");break;
    }

    }while (c!='x');

    return 0;
    }
    ---------------------------------

    my "m" command, which is supposed to ask you for the email informations is
    this one:
    ----------------/comandom.c-----------
    #include <stdio.h>

    typedef struct mail
    {
    char from[120];
    char to[120];
    char sub[120];
    char msg[120];
    } MAIL;

    void comandom(MAIL *ptr)
    {
    printf("From: \n"); gets(ptr->from);
    printf("To: \n"); gets(ptr->to);
    printf("Subject:\n"); gets(ptr->sub);
    printf("Message:\n"); gets(ptr->msg);


    }
    ------------------------------

    then I have the command "q" which when pressed has to keep all the written
    emails in a mail.txt fuile..I made this:
    --------------(comandoq.c)------------
    #include <stdio.h>

    FILE *fp;

    fp = fopen("mail.txt","a");

    fprintf(fp,"From:%s\n",from);
    fprintf(fp,"To:%s\n",to);
    fprintf(fp,"Subject:%s\n",sub);
    fprintf(fp,"Message:%s\n",msg);
    fprintf(fp,"\n");

    fclose(fp);

    }
    ----------------
    it's not complete so I woudl like you to help me, please.
    the output of the mail.txt file has to be something like this:
    From:
    to:
    Date: ....
    Subject: hfdhrt
    Message: fdhdh

    From:
    to:
    Date: ....
    Subject: hfdhrt
    Message: fdhdh

    From:
    to:
    Date: ....
    Subject: hfdhrt
    Message: fdhdh

    --------------

    kan you help me please?
    next I have to do a command that shows in the program the to,date and
    subject of each message in "mail.txt" like this:

    & h
    N 1 <endereço da mensagem1> <data1> <titulo da mensagem1>
    N 2 <endereço da mensagem2> <data2> <titulo da mensagem2>
    N 3 <endereço da mensagem3> <data3> <titulo da mensagem3>

    ...
    thanks a lot, help me please, it's a case of life or death =)
     
    tyler_durden, Dec 29, 2004
    #12
  13. tyler_durden

    Flash Gordon Guest

    Re: Specific problem...now you can help =)

    On Wed, 29 Dec 2004 15:25:59 -0500
    "tyler_durden" <> wrote:

    > I made this command but it gives me a "segmentation fault" error...can
    > you help me?
    >
    > -------------------------
    >
    > #include <stdio.h>
    >
    > typedef struct mail
    > {
    > char from[120];
    > char to[120];
    > char sub[120];
    > char msg[120];
    > } MAIL;
    >
    > void comandom(MAIL *ptr)
    > {
    > printf("From: \n"); gets(ptr->from);


    Using gets is a *very* bad idea. You cannot prevent a user causing a
    buffer overflow by entering a string too long to enter in to the buffer.
    Use fgets, but look at what it does first especially in terms of new
    line and lines longer than the buffer.

    > printf("To: \n"); gets(ptr->to);
    > printf("Subject:\n"); gets(ptr->sub);
    > printf("Message:\n"); gets(ptr->msg);
    >
    >
    > }
    >
    > ---------------------------
    >
    > and I need to implement the time and date when I write each e-mail..I
    > have the function which is:
    > -------------------
    > #include<stdio.h>
    > #include<time.h>
    > #define MAX 120
    >
    > void tempo(char *tempo){
    >
    > time_t horact;
    >
    > time(horact);


    The compiler should have complained about this.
    time(&horact);

    > ctime_r(horact,tempo);


    This is not a standard C function so we don't know about it here.
    <OT>
    However, the compiler should have complained about this as well
    </OT>

    > }
    >
    >
    > -----------------------------
    >
    > but I dunno how to use it..
    > thanks a lot!


    You have the source for a function but don't know how to use it? Well,
    in this case you pass a pointer to a buffer large enough for the longest
    possible string representation of the time.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Dec 29, 2004
    #13
  14. tyler_durden

    tyler_durden Guest

    Re: Specific problem...now you can help =)

    So I should not use gets?
    but I need the user to insert the from,to,subject and message...the
    function of fgets is to get something from a file,right?
    I thought of using scanf, but then if a space was inserted, it would not
    keep the information correctly...
    in the "What I have until now" is all my program until this phase...
    thanks a lot for your help..
     
    tyler_durden, Dec 29, 2004
    #14
  15. tyler_durden

    Flash Gordon Guest

    Re: What I have until now

    On Wed, 29 Dec 2004 16:36:41 -0500
    "tyler_durden" <> wrote:

    > ok..so what I did until now is this...
    > the main file is this one:
    > ------------------------------(progmail.c)----
    > #include <stdio.h> /* para funcoes como printf, scanf, etc */
    > #include <stdlib.h> /* para a funcao exit */
    > #include <string.h> /* para funcoes relacionadas com strings */
    > #define DIMMAIN 20
    >
    > void comandom(void);
    > void comandoC(void);
    > void comandoL(void);
    >
    > int help(){
    >
    > printf ("Help \n \n");
    > printf ("Mail Commands \n \n");
    >
    >
    > printf("-------------------------------------------------------------
    > -----------\n");
    > printf("| t <message list> type messages
    >
    > |\n");
    > printf("| d <message list> delete messages
    >
    > |\n");
    > printf("| R <message list> reply to message senders
    >
    > |\n");
    > printf("| r <message list> reply to message senders and all
    > recipients |\n");
    > printf("| m <user list> mail to specific users
    >
    > |\n");
    > printf("| q quit, saving unresolved messages in mbox
    >
    > |\n");
    > printf("| x quit, do not remove system mailbox
    >
    > |\n");
    > printf("| h print out active message headers
    >
    > |\n");
    >
    > printf("-------------------------------------------------------------
    > -----------\n");
    >
    > return 0;
    >
    > }
    >
    >
    >
    > int main (){
    > char c;
    >
    > fopen("mail.txt","r");
    >
    > printf("E-mail program.\n");
    >
    >
    > do{
    > putchar ('&');
    > scanf("%s",&c);


    BANG!

    %s means read a string, which is 0 or more characters followed by a
    '\0'. So if there are any characters input then a minimum of 2 bytes
    will be written to c which is only large enough for 1 byte.

    > switch (c)
    > {
    > case '?':help();break;
    > case 't':printf("ok\n");break;
    > case 'd':printf("ok\n");break;
    > case 'R':printf("ok\n");break;
    > case 'r':printf("ok\n");break;
    > case 'm':comandom();break;
    > case 'q':printf("ok\n");break;
    > case 'x':printf("Program shutting down!\n"); break;
    > case 'h':printf("ok\n");break;
    > case 'C':comandoC();break;
    > case 'L':comandoL();break;
    > default: printf("Unknown command!\n");break;
    > }
    >
    > }while (c!='x');
    >
    > return 0;
    > }
    > ---------------------------------
    >
    > my "m" command, which is supposed to ask you for the email
    > informations is this one:
    > ----------------/comandom.c-----------
    > #include <stdio.h>
    >
    > typedef struct mail
    > {
    > char from[120];
    > char to[120];
    > char sub[120];
    > char msg[120];
    > } MAIL;
    >
    > void comandom(MAIL *ptr)


    You called this without any parameters. Always make sure functions are
    prototyped before they are called.

    > {
    > printf("From: \n"); gets(ptr->from);


    Still using gets, so once again BANG!

    > printf("To: \n"); gets(ptr->to);
    > printf("Subject:\n"); gets(ptr->sub);
    > printf("Message:\n"); gets(ptr->msg);
    >
    >
    > }
    > ------------------------------
    >
    > then I have the command "q" which when pressed has to keep all the
    > written emails in a mail.txt fuile..I made this:
    > --------------(comandoq.c)------------
    > #include <stdio.h>
    >
    > FILE *fp;
    >
    > fp = fopen("mail.txt","a");
    >
    > fprintf(fp,"From:%s\n",from);
    > fprintf(fp,"To:%s\n",to);
    > fprintf(fp,"Subject:%s\n",sub);
    > fprintf(fp,"Message:%s\n",msg);
    > fprintf(fp,"\n");
    >
    > fclose(fp);
    >
    > }
    > ----------------
    > it's not complete so I woudl like you to help me, please.
    > the output of the mail.txt file has to be something like this:
    > From:
    > to:
    > Date: ....
    > Subject: hfdhrt
    > Message: fdhdh
    >
    > From:
    > to:
    > Date: ....
    > Subject: hfdhrt
    > Message: fdhdh
    >
    > From:
    > to:
    > Date: ....
    > Subject: hfdhrt
    > Message: fdhdh
    >
    > --------------
    >
    > kan you help me please?
    > next I have to do a command that shows in the program the to,date and
    > subject of each message in "mail.txt" like this:
    >
    > & h
    > N 1 <endereço da mensagem1> <data1> <titulo da mensagem1>
    > N 2 <endereço da mensagem2> <data2> <titulo da mensagem2>
    > N 3 <endereço da mensagem3> <data3> <titulo da mensagem3>
    >
    > ..
    > thanks a lot, help me please, it's a case of life or death =)
    >
    >



    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Dec 29, 2004
    #15
  16. tyler_durden

    Mike Wahler Guest

    Re: Specific problem...now you can help =)

    "tyler_durden" <> wrote in message
    news:...
    > So I should not use gets?
    > but I need the user to insert the from,to,subject and message...the
    > function of fgets is to get something from a file,right?


    From a stream. One of the standard streams is 'stdin'.
    Look it up.

    -Mike
     
    Mike Wahler, Dec 29, 2004
    #16
  17. tyler_durden

    Flash Gordon Guest

    Re: Specific problem...now you can help =)

    On Wed, 29 Dec 2004 17:37:20 -0500
    "tyler_durden" <> wrote:

    > So I should not use gets?


    Definitely not. Ever.

    > but I need the user to insert the from,to,subject and message...the
    > function of fgets is to get something from a file,right?


    What do you think stdin is?

    > I thought of using scanf, but then if a space was inserted, it would
    > not keep the information correctly...


    Well, there are scan sets, but using fgets is the simplest solution if
    you want to read a line. Although you still have to be careful. The
    alternatives are reading a character at a time or using a library
    implemented by one of the regulars. However, using a third party library
    might upset your tutor.

    > in the "What I have until now" is all my program until this phase...
    > thanks a lot for your help..


    Well, we do help with C questions.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Dec 29, 2004
    #17
  18. Re: What I have until now

    On Wed, 29 Dec 2004 16:36:41 -0500, "tyler_durden"
    <> wrote:

    >ok..so what I did until now is this...
    >the main file is this one:
    >------------------------------(progmail.c)----
    >#include <stdio.h> /* para funcoes como printf, scanf, etc */
    >#include <stdlib.h> /* para a funcao exit */
    >#include <string.h> /* para funcoes relacionadas com strings */
    >#define DIMMAIN 20
    >
    >void comandom(void);
    >void comandoC(void);
    >void comandoL(void);


    The above declarations do not match the definitions you used later
    on--e.g., comandom() takes a pointer to MAIL as its sole argument, yet
    here you declare the function as taking no arguments.

    >
    >int help(){


    If you mean void, say so:

    int help(void)

    Actually, I fail to see why help returns an int. The return value seems
    to not be used elsewhere in your code. You could simply defined help as
    a void function:

    void help(void)

    and omitted the return 0 statement at the end of the function.

    <snip>

    >int main (){


    Again,

    int main(void)

    > char c;
    >
    > fopen("mail.txt","r");


    You discarded the returned FILE pointer--why?

    Unless you planned to pass the FILE pointer to one of your comandox
    functions, I fail to see why you opened the file at all.

    > printf("E-mail program.\n");
    >
    >
    > do{
    > putchar ('&');
    > scanf("%s",&c);


    fgets would be a better choice for user input. You could write a
    function around fgets and make it general enough to use for a variety of
    user input.

    > switch (c)
    > {
    > case '?':help();break;

    <snip>
    > case 'm':comandom();break;


    In your comandom.c file, you define comandom() as taking a pointer to
    MAIL. Here you omit the argument, and even if you hadn't, there is no
    definition in scope for the MAIL data type--unless you omitted this in
    your posted code.

    <snip>
    > case 'C':comandoC();break;
    > case 'L':comandoL();break;
    > default: printf("Unknown command!\n");break;
    > }


    See above comments about the comandox functions.

    Also, the default case would also be a good place to call your help()
    function.

    >
    > }while (c!='x');
    >
    > return 0;
    > }


    Use EXIT_SUCCESS or EXIT_FAILURE instead of 0.

    >my "m" command, which is supposed to ask you for the email informations is
    >this one:
    >----------------/comandom.c-----------
    >#include <stdio.h>
    >
    >typedef struct mail
    >{
    > char from[120];
    > char to[120];
    > char sub[120];
    > char msg[120];
    >} MAIL;


    DEFINEd macros would be a good choice instead of hard-coding these
    constants:

    #define MSG_MAX_FROM 120
    #define MSG_MAX_TO MSG_MAX_FROM
    #define MSG_MAX_SUB MSG_MAX_FROM
    #define MSG_MAX_MSG 1024
    // Did you really want only 120 chars for the message?

    typedef struct mail
    {
    char from[MSG_MAX_FROM];
    char to[MSG_MAX_TO];
    char sub[MSG_MAX_SUB];
    char msg[MSG_MAX_MSG];
    }
    MAIL;

    This data type should be accessible to all of your modules that require
    it, not just this one. I'd suggest placing it in a header file that
    your other modules can read.

    >void comandom(MAIL *ptr)
    >{
    > printf("From: \n"); gets(ptr->from);
    > printf("To: \n"); gets(ptr->to);
    > printf("Subject:\n"); gets(ptr->sub);
    > printf("Message:\n"); gets(ptr->msg);
    >}


    See comments about fgets.

    I'd assume that the MAIL pointer in this function would point to a MAIL
    object that already exists? Nowhere in your posted code is a MAIL
    object created.

    >------------------------------
    >
    >then I have the command "q" which when pressed has to keep all the written
    >emails in a mail.txt fuile..I made this:
    >--------------(comandoq.c)------------
    >#include <stdio.h>
    >

    There is no function here!

    I'm assuming you meant

    void comandoq(MAIL *ptr)
    {

    For that matter, void is a poor choice of return type for this function.
    More on this below.

    >FILE *fp;
    >
    >fp = fopen("mail.txt","a");


    This needs error-checking. What if the system could not open or create
    mail.txt?

    >fprintf(fp,"From:%s\n",from);
    >fprintf(fp,"To:%s\n",to);
    >fprintf(fp,"Subject:%s\n",sub);
    >fprintf(fp,"Message:%s\n",msg);
    >fprintf(fp,"\n");


    Where are from, to, sub and msg? Are these supposed to be members of
    the MAIL data type that you neglected to pass as an argument to this
    function?

    >
    >fclose(fp);
    >
    >}


    If there had been an error opening the file, your code as written does
    not communicate that back to the caller. I'd suggest something more
    like

    #include <stdio.h>
    #define MAIL_FILE "mail.txt"

    int comandoq(MAIL *ptr)
    {
    FILE *fp;

    if ((fp = fopen(MAIL_FILE, "a")) != NULL)
    {
    fprintf(fp, "From: %s\n", ptr->from);
    fprintf(fp, "To: %s\n", ptr->to);
    //...
    fclose(fp);
    }
    else
    fprintf(stderr, "\aError opening %s!\n", MAIL_FILE);

    return fp != NULL; // Returns 0 if file error; true otherwise
    }

    >----------------
    >it's not complete so I woudl like you to help me, please.
    >the output of the mail.txt file has to be something like this:


    You're not kidding. The code you posted doesn't even compile. :)

    Try this: Focus on writing routines to read the mail data from the user
    and write it to a file. Make sure that you can append several emails to
    the file.

    When this is working cleanly, THEN proceed to the next step.

    Then, write a routine to read those emails from the file and display
    them to the user.

    The rest should come easily after this initial hurdle is passed.

    Keep in mind that some declarations need to be accessible to all of your
    modules--the MAIL data type, for one.

    >thanks a lot, help me please, it's a case of life or death =)


    Hope this helps.


    --
    Robert B. Clark (email ROT13'ed)
    Visit ClarkWehyr Enterprises On-Line at http://www.3clarks.com/ClarkWehyr/
     
    Robert B. Clark, Dec 30, 2004
    #18
  19. Re: Specific problem...now you can help =)

    On Wed, 29 Dec 2004 17:37:20 -0500, "tyler_durden"
    <> wrote:

    >So I should not use gets?
    >but I need the user to insert the from,to,subject and message...the
    >function of fgets is to get something from a file,right?


    A stream, which is not necessarily a file. stdin can be a stream.

    Unlike gets, fgets can be told when to stop, preventing nasty buffer
    overflows that are possible with gets.

    -------
    fgets gets a string from a stream

    Declaration:
    char *fgets(char *s, int n, FILE *stream);

    fgets reads characters from a stream into the string s. It stops when
    it reads either n-1 characters or a newline character, whichever comes
    first.

    fgets retains the newline character at the end of s and appends a null
    byte to s to mark the end of the string.
    --------

    --
    Robert B. Clark (email ROT13'ed)
    Visit ClarkWehyr Enterprises On-Line at http://www.3clarks.com/ClarkWehyr/
     
    Robert B. Clark, Dec 30, 2004
    #19
  20. Re: Specific problem...now you can help =)

    On Wed, 29 Dec 2004 15:25:59 -0500, "tyler_durden"
    <> wrote:

    >I made this command but it gives me a "segmentation fault" error...can you
    >help me?


    C doesn't have commands. You wrote a function.

    >
    >-------------------------
    >
    >#include <stdio.h>
    >
    >typedef struct mail
    >{
    > char from[120];
    > char to[120];
    > char sub[120];
    > char msg[120];
    >} MAIL;
    >
    >void comandom(MAIL *ptr)
    >{
    > printf("From: \n"); gets(ptr->from);
    > printf("To: \n"); gets(ptr->to);
    > printf("Subject:\n"); gets(ptr->sub);
    > printf("Message:\n"); gets(ptr->msg);


    While using gets is undesirable, as long as you typed in short (<120)
    strings it did not cause the problem you are experiencing.
    >
    >
    >}

    You need to show us how you are calling this function.


    <<Remove the del for email>>
     
    Barry Schwarz, Dec 31, 2004
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?TXJGZXo=?=

    Mail message size problem (System.Web.Mail)

    =?Utf-8?B?TXJGZXo=?=, Mar 14, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    669
    =?Utf-8?B?TXJGZXo=?=
    Mar 14, 2005
  2. tfsmag
    Replies:
    4
    Views:
    618
    =?Utf-8?B?U3JlZWppdGggUmFt?=
    Oct 21, 2005
  3. Replies:
    11
    Views:
    728
    Mark Rae
    Dec 27, 2006
  4. Tony
    Replies:
    2
    Views:
    328
  5. Patrick
    Replies:
    0
    Views:
    345
    Patrick
    Feb 17, 2004
Loading...

Share This Page