Thread: Help doing an e-mail program in c...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    45

    Help doing an e-mail program in c...

    I have to do an email program for a homework assignment...here I give you what I have made so far..
    ok..so what I did until now is this...
    the main file is this one:
    ------------------------------(progmail.c)----
    Code:
    #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-----------
    Code:
    #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)------------
    Code:
    #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:
    Code:
    From: [email protected]
    to: [email protected]
    Date: ....
    Subject: hfdhrt
    Message: fdhdh
    
    From: [email protected]
    to: [email protected]
    Date: ....
    Subject: hfdhrt
    Message: fdhdh
    
    From: [email protected]
    to: [email protected]
    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 because I have to deliver it the 2nd of january...


    EDIT - the main problem it's the segmentation fault in the comandom.c , and I need some help in comandoq.c, which when called has to save all the written e-mails in "mail.txt"...
    thanks.
    Last edited by Tyler_Durden; 12-29-2004 at 04:25 PM.

  2. #2
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Edit your post with code to add code tags please. Given the scope of your project it may pay to focus on a single command, try to make the code for it yourself, then post your code and explain what works and what doesn't. Help will then flood in. Good questions get good answers around here.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    In main() you have this:
    Code:
        char c;
        scanf("%s",&c);
    To scan in a character you should use %c instead of %s.

    Also, never ever use gets(). Replace it with fgets().

    You also need to actually pass something to commandom() (a pointer to type MAIL).
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    45
    when substituting gets with fgets I get an error when compiling:
    too few arguments for function fgets...
    doesn't fgets get somethi8ng from a file? I need to get what the user types...

    how do I resolve it?

    thanks a lot for the tips..keep'em coming
    Last edited by Tyler_Durden; 12-29-2004 at 04:58 PM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's covered in the FAQ. Use stdin as the file.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    45
    with your help I was able to do this:
    Code:
    #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"); 
    fgets("MAIL.from",120,stdin);
      printf("To:     \n"); 
    fgets("MAIL.to",120,stdin);
      printf("Subject:\n");
    fgets("MAIL.sub",120,stdin);
      printf("Message:\n"); 
    fgets("MAIL.msg",120,stdin);
    
    
    
    }
    it compiles with no errors, but when executed it gives a segmentation fault....now what can I do?

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I think you should replace all your MAIL. references with ptr->
    If you understand what you're doing, you're not learning anything.

  9. #9
    Registered User
    Join Date
    Dec 2004
    Posts
    45
    thanks but tried it now and the Segmentation fault error is still there...

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    It's probably the way you're calling the function then. You need to have memory allocated for a MAIL struct and pass a pointer to that memory to your commandom() function.
    If you understand what you're doing, you're not learning anything.

  11. #11
    Registered User
    Join Date
    Dec 2004
    Posts
    45
    hmm... I see..
    could you please show me how to do that?..
    has it got something to do with the calling of comandom from the progmail.c?I have it like this
    comandom(void)

    do I have to change it now that it is different?

  12. #12
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    From my earlier post in this thread:
    Quote Originally Posted by itsme86
    You also need to actually pass something to commandom() (a pointer to type MAIL).
    It should go something like this:
    Code:
    int main(void)
    {
      MAIL m;
    
      // Blah blah code here
    
      commandom(&m);
      return 0;
    }
    
    void commandom(MAIL *ptr)
    {
      // Code goes here
    }
    Last edited by itsme86; 12-29-2004 at 07:01 PM.
    If you understand what you're doing, you're not learning anything.

  13. #13
    Registered User
    Join Date
    Dec 2004
    Posts
    45
    So instead of comandom() I'm supposed to have comandom(MAIL *ptr), right?
    sorry if I don't understand everything you try to explain, but my language is not english, and I'm a noob at C, the two together don't help much
    I'm gonna try it now..thanks

  14. #14
    Registered User
    Join Date
    Dec 2004
    Posts
    45
    Quote Originally Posted by itsme86
    From my earlier post in this thread:


    It should go something like this:
    Code:
    int main(void)
    {
      MAIL m;
    
      // Blah blah code here
    
      commandom(&m);
      return 0;
    }
    
    void commandom(MAIL *ptr)
    {
      // Code goes here
    }
    so I have to make a main(void) inside my comandom.c, right? Or that main is the one from progmail.c, and the void comandom is from comandom.c ?

  15. #15
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Code:
    #ifndef PROGMAIL_H_INCLUDED
    #define PROGMAIL_H_INCLUDED
    // this is progmail.h
    typedef struct mail
    {
      char from[120];
      char to[120];
      char sub[120];
      char msg[120];
    } MAIL;
    
    void comandom(MAIL *m);
    void comandoC(MAIL *m);
    void comandoL(MAIL *m);
    #endif

    Code:
    // this is progmail.c (part of it)
    #include <stdio.h>    /* para funcoes como printf, scanf, etc */
    #include <stdlib.h>   /* para a funcao exit */ 
    #include <string.h>   /* para funcoes relacionadas com strings */
    #include "progmail.h"
    
    int help(){
      
      printf ("Help \n \n");
      printf ("Mail Commands \n \n");
      // add rest of help text here
      return 0;
    }
    
    int main (){
      char c;
      char input[BUFSIZ];
      MAIL mailmessage;
      
      fopen("mail.txt","r");
      
      printf("E-mail program.\n");
      
      
      do{
        putchar ('&');
        fgets( input, BUFSIZ, stdin );
        c = input[0];
        
        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(&mailmessage);break;
          case 'q':printf("ok\n");break;
          case 'x':printf("Program shutting down!\n"); break; 
          case 'h':printf("ok\n");break;
          case 'C':comandoC(&mailmessage);break;
          case 'L':comandoL(&mailmessage);break;
          default: printf("Unknown command!\n");break;
          } 
    
          }while (c!='x');
      
        return 0;
      }

    Code:
    // this is comandom.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 */
    #include "progmail.h"
    
    void comandom(MAIL *ptr)
    {
      printf("From:   \n"); fgets(ptr->from,sizeof ptr->from, stdin);
      printf("To:     \n"); fgets(ptr->to,sizeof ptr->to, stdin);
      printf("Subject:\n"); fgets(ptr->sub,sizeof ptr->sub, stdin);
      printf("Message:\n"); fgets(ptr->msg,sizeof ptr->msg, stdin);
    }
    Code:
    // this is comandoL.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 */
    #include "progmail.h"
    
    void comandoL(MAIL *ptr)
    {
      printf("From:   %s\n",ptr->from);
      printf("To:     %s\n",ptr->to);
      printf("Subject:%s\n",ptr->sub);
      printf("Message:%s\n",ptr->msg);
    }
    Notice progmail.h
    It contains all the bits of information which need to be shared between all the source files in your project.

    One of the first tasks in a programming exercise is simply making sure that you can input information to the program, and print it back again without doing anything else.
    To this end, making sure comandom() and comandoL() work as expected is a useful first step.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM