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

This is a discussion on Help doing an e-mail program in c... within the C Programming forums, part of the General Programming Boards category; Originally Posted by Tyler_Durden I need the format of the mail.txt file to be like this: To:{blabla@bla.com} fprintf(fp,"{%s}",ptr->to); but the ...

  1. #31
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by Tyler_Durden
    I need the format of the mail.txt file to be like this:
    To:{blabla@bla.com}

    fprintf(fp,"{%s}",ptr->to);

    but the mail.txt program turns out like this:
    to:{gfds@dfd.com
    }
    This is the expected behaviour when you use fgets() 'rawly'. It also 'catches' the '\n'. All you have to do is to 'clean-up' the string once you have got it (actually, fgets() is a 'line reader')

    The usual and minimum trick is the "search'n kill" procedure:
    Code:
    char *p = strchr(line, '\n'); /* needs <string.h> */
    
    if (p != NULL)
    {
       *p = 0;
    }
    Of course, it's more clever to stick this code in some function...
    Emmanuel Delahaye

    "C is a sharp tool"

  2. #32
    Registered User
    Join Date
    Dec 2004
    Posts
    45
    ok..I think I will not worry with this problem..its minimal, they shouldn't mind not having the {}'s in the .txt...
    Now I need to know how to make the program, when inicialized, read all the info in the mail.txt, and transform it into vectors so it can be manipulated....
    for example, I need to be able to press "h" and be presented with the mails in mail.txt this way:
    1 > (subject) ( date) ( from)
    2 > .........
    3 > ..........

    then I need a comand that erases the mails when I press "d1" or "d2" it eraes mail 1 or mail 2 , respectively(sp?).... but right now my concern is the program to read the mail.txt and pass all the mails to a struct...
    could you explain me how to do it please?

    I started the program just now because I didn't think it would be so difficult,lol... I only need a positive grade and I will be satisfied,

  3. #33
    Registered User
    Join Date
    Dec 2004
    Posts
    45
    Quote Originally Posted by Emmanuel Delaha
    This is the expected behaviour when you use fgets() 'rawly'. It also 'catches' the '\n'. All you have to do is to 'clean-up' the string once you have got it (actually, fgets() is a 'line reader')

    The usual and minimum trick is the "search'n kill" procedure:
    Code:
    char *p = strchr(line, '\n'); /* needs <string.h> */
    
    if (p != NULL)
    {
       *p = 0;
    }
    Of course, it's more clever to stick this code in some function...
    Could I put this code you gave me in the progmail.h file, and then as I call it in all my comandos, it would kill all that problems with the fgets...?

    EDIT - tried it and says that the variable "line" is not defined.
    Last edited by Tyler_Durden; 12-30-2004 at 10:08 AM.

  4. #34
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by Tyler_Durden
    Could I put this code you gave me in the progmail.h file, and then as I call it in all my comandos, it would kill all that problems with the fgets...?
    A header is not a good place for a function (unless you have C99 and 'inline'). You really want to write this cleanup function or to write some get_line() function of your own including fgets() and the clean-up code I supplied.
    EDIT - tried it and says that the variable "line" is not defined.
    Of course, I'm not going to give you a 100% working solution. You have to adapt the code. How is the 'line' named in this case ?
    Please shake your brain... We are not going to think for you for the rest of your life...
    Emmanuel Delahaye

    "C is a sharp tool"

  5. #35
    Registered User
    Join Date
    Dec 2004
    Posts
    45
    Quote Originally Posted by Emmanuel Delaha
    A header is not a good place for a function (unless you have C99 and 'inline'). You really want to write this cleanup function or to write some get_line() function of your own including fgets() and the clean-up code I supplied.

    Of course, I'm not going to give you a 100% working solution. You have to adapt the code. How is the 'line' named in this case ?
    Please shake your brain... We are not going to think for you for the rest of your life...
    oh..I think I understand now...Thanks a lot, and sorry for the bother...I'm going to try to do more things alone, I promise

  6. #36
    Registered User
    Join Date
    Dec 2004
    Posts
    45
    I have been searching my "c" book for how to pass a file to a struct that can then be manipulated...but I can't find anything..
    I have to read the mails from "mail.txt" and pass them to structs, to be able to manipulate them in the program, like viewing the mails and being able to erase the ones I want..
    can you help me with this?I believe it's really the last difficult step in the rpogram..then it's just some printfs to show the info, and the delete command..
    Be Well

  7. #37
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,167
    Open the file mail.txt with fopen() and then use fgets() on the stream just like you did with stdin. It will read in each line in the file one at a time, just like you wrote it to the file.
    If you understand what you're doing, you're not learning anything.

  8. #38
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    wow this sounds a lot like...uhmmmm let me see.....oh yeah - HOMEWORK!!!!

    Sounds like you need to get your code together. Work it out on paper first. write down an ouline of what you need to do in the program and then translate into C.

    As for reading a file into a structure - clicky clicky.

  9. #39
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,167
    Quote Originally Posted by caroundw5h
    wow this sounds a lot like...uhmmmm let me see.....oh yeah - HOMEWORK!!!!
    Sherlock, he said right in his first post that it was homework:
    Quote Originally Posted by Tyler_Durden
    I have to do an email program for a homework assignment
    That's why I've been careful not to lay out a full working program for him. It's pretty obvious that he should have paid a little closer attention in class, but it doesn't hurt to point him in the right direction on specific questions. At least he's working out the logic on his own.
    If you understand what you're doing, you're not learning anything.

  10. #40
    Registered User
    Join Date
    Dec 2004
    Posts
    45
    Quote Originally Posted by caroundw5h
    wow this sounds a lot like...uhmmmm let me see.....oh yeah - HOMEWORK!!!!

    Sounds like you need to get your code together. Work it out on paper first. write down an ouline of what you need to do in the program and then translate into C.

    As for reading a file into a structure - clicky clicky.
    thanks a lot for your help but
    could you please explain me better what are the "size" and "nmemb" of freads?

  11. #41
    Registered User
    Join Date
    Dec 2004
    Posts
    45
    so peple, right now I've already made the file to struct thing but it has a flaw.... it reads the file but when it printf's it in the screen it starts taking some letters of it..for example, the program starts, it gets the file to a struct, I ask it to printf the result in the screen and the output is like this:
    To: blabla@bla.com
    rom:blabla@bla.com
    bject:blabla
    blabla

    .....

    my leficheiro.c (file that reads the file to a struct) is like this:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include "progmail.h"
    
    void leficheiro(MAILR *ptr)
    {
      FILE *fp;
    
    fp = fopen("mail.txt","r");
    
     fgets(ptr->to,50,fp);
     fgets(ptr->from,50,fp);
     fgets(ptr->sub,50,fp);
     fgets(ptr->msg,50,fp);
     fclose(fp);
    
    }
    my command to present the result is comandoC.c:
    Code:
    #include<stdio.h>
    #define MAX 120
    
    typedef struct mailr{
      char from[MAX+1];
      char to[MAX+1];
      char sub[MAX+1];
      char msg[MAX+1];
    }MAILR;
    
    void comandoC(MAILR *ptr){
      
          
        printf("%s\n",ptr->from);
        printf("%s\n",ptr->to);
        printf("%s\n",ptr->sub);
        printf("%s\n",ptr->msg);
        puts("");
        
      
    }
    and my main file, 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 */
    #include "progmail.h"
    
    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,input[BUFSIZ];
      MAIL mailmessage;
      MAILR mailr;
    
      printf("E-mail program.\n");
    
      leficheiro(&mailr);  
      
      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(&mailr);break;
          case 'L':comandoL(&mailmessage);break;
          default: printf("Unknown command -> %s \n",input);break;
          } 
    
          }while (c!='x');
      
        return 0;
      }
    ...So can you tell me where's the problem, please?

  12. #42
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    post the entire program.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  13. #43
    Registered User
    Join Date
    Dec 2004
    Posts
    45
    Quote Originally Posted by Sebastiani
    post the entire program.
    ok..so the entire program until now is this:

    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 */
    #include "progmail.h"
    
    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,input[BUFSIZ];
      MAIL mailmessage;
      MAILR mailr;
    
      printf("E-mail program.\n");
    
      leficheiro(&mailr);  
      
      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(&mailr);break;
          case 'L':comandoL(&mailmessage);break;
          default: printf("Unknown command -> %s \n",input);break;
          } 
    
          }while (c!='x');
      
        return 0;
      }
    comandoC.c
    Code:
    #include<stdio.h>
    #define MAX 120
    
    typedef struct mailr{
      char from[MAX+1];
      char to[MAX+1];
      char sub[MAX+1];
      char msg[MAX+1];
    }MAILR;
    
    void comandoC(MAILR *ptr){
      
          
        printf("%s\n",ptr->from);
        printf("%s\n",ptr->to);
        printf("%s\n",ptr->sub);
        printf("%s\n",ptr->msg);
        puts("");
        
      
    }
    comandoL.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 */
    #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);
    
    }
    comandom.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 */
    #include "progmail.h"
    
    void comandom(MAIL *ptr)
    {
      FILE *fp;
    
      fp = fopen("mail.txt","a");
    
      printf("To:     \n"); fgets(ptr->to,sizeof ptr->to, stdin);
      fprintf(fp,"To:%s",ptr->to);
    
      printf("From:   \n"); fgets(ptr->from,sizeof ptr->from, stdin);
      fprintf(fp,"From: %s",ptr->from);
    
      printf("Subject:\n"); fgets(ptr->sub,sizeof ptr->sub, stdin);
      fprintf(fp,"Subject: %s",ptr->sub);  
    
      printf("Message:\n"); fgets(ptr->msg,sizeof ptr->msg, stdin);
      fprintf(fp,"%s",ptr->msg);
    
      fprintf(fp,"\n");
      fclose(fp);
    
        
    }
    leficheiro.c
    Code:
    #include <stdio.h>
    #include <string.h>
    #include "progmail.h"
    
    void leficheiro(MAILR *ptr)
    {
      FILE *fp;
    
    fp = fopen("mail.txt","r");
    
     fgets(ptr->to,50,fp);
     fgets(ptr->from,50,fp);
     fgets(ptr->sub,50,fp);
     fgets(ptr->msg,50,fp);
     fclose(fp);
    
    }
    mail.txt (example)
    Code:
    To:fdgd
    From: geb
    Subject: erg
    regdfgdfgdfffsdg
    
    To:dsgsdg
    From: asdd
    Subject: gfgd
    fddfhgdfheshresh

    ok, so what I am missing here is:
    ->to be able to insert the date and hour when each e-mail was written in the mail.txt...in comandom.c I need to be able to have the emails date too...

    ->Now I see that the mails can only be saved whern "q" is pressed.this is because the program has to work this way:
    -you write the mails you want
    -if you press "x" the mail.txt isn't modified and the program exits
    -if you press "q" the mails are saved in "mail.txt" and the program exits

    ->I then need to be able to go to the struct that was made out of the file "mail.txt" and when "h" is pressed, to have this output on the screen:
    Code:
    & h        /* h being inserted*/
    > N 1 <to> <date> <subject>
    N 2 <to2> <date2> <subject2>
    N 3 <to3> <date3> <subject3>
    ->Then I have to have a command that deletes mails from the "mail.txt", and works like this:
    comandod.c
    Code:
    & d2
    & h
    > N 1 <enderešo da mensagem1> <data1> <titulo da mensagem1>
    N 3 <enderešo da mensagem3> <data3> <titulo da mensagem3>
    &
    typing "d2", erases the second mail in "mail.txt"....

    ->the last comand that needs to be done is "t" that when typed like this for example "t2" gets an out put like this on the screen:
    Code:
    To: gfsddsf
    From : sgd
    Date:dfsgd
    Subject:dfgfewq
    Message:sdfgdsfg
    that means it shows the mail in the screen..

    and that is all...lol...see if you can help me in any of these tasks, please..time is running out


    PS - comandoL and comandoC are only to test the comandom capacity to save the info into structs, and the leficheiro capacity to read the file to a struct, respectively(sp?)

  14. #44
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> ok..so the entire program until now is this:

    I don't see progmail.h there.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  15. #45
    Registered User
    Join Date
    Dec 2004
    Posts
    45
    Quote Originally Posted by Sebastiani
    >> ok..so the entire program until now is this:

    I don't see progmail.h there.
    sorry
    progmail.h
    Code:
    #ifndef PROGMAIL_H_INCLUDED
    #define PROGMAIL_H_INCLUDED
    typedef struct mail
    {
      char from[120];
      char to[120];
      char sub[120];
      char msg[120];
    } MAIL;
    
    typedef struct mailr
    {
      char from[120];
      char to[120];
      char sub[120];
      char msg[120];
    } MAILR;
    
    void leficheiro(MAILR *m);
    void comandom(MAIL *m);
    void comandoC(MAIL *m);
    void comandoL(MAIL *m);
    #endif

Page 3 of 6 FirstFirst 123456 LastLast
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, 12: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, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21