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

  1. #16
    Quote Originally Posted by Tyler_Durden
    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 ?
    No. I was just an example. The general idea is that when you use a structure, it's good design to pass the address of it to the functions.

    The address can be the address of some object
    Code:
    void fa (void)
    {
       T o;
    
       fb (&o);
    }
    or the address got from a parameter
    Code:
    void fa (T *po)
    {
       fb (po);
    }
    or from malloc():
    Code:
    void fa (void)
    {
       T *po = malloc  (sizeof *po);
    
       if (po != NULL)
       {
          fb (po);
          free (po),  po = NULL;
       }
    }
    Emmanuel Delahaye

    "C is a sharp tool"

  2. #17
    Registered User
    Join Date
    Dec 2004
    Posts
    45
    thanks a lot people... you are being really helpful...
    Now I'm going to try to do comandoq.c, which has to save all emails written when "q" is pressed...
    I think it must be something like this...
    Code:
    #include <stdio.h>
    
    FILE *fp;
    
    fp = fopen("mail.txt","a");
    
    fprintf(fp,"From:%s\n",MAIL.from);
    fprintf(fp,"To:%s\n",MAIL.to);
    fprintf(fp,"Subject:%s\n",MAIL.sub);
    fprintf(fp,"Message:%s\n",MAIL.msg);
    fprintf(fp,"\n");
    
    fclose(fp);
    
    }
    but now I have a doubt, if I write more than one e-mail will it save all of them in the file?
    thanks =)

  3. #18
    ---
    Join Date
    May 2004
    Posts
    1,379
    Not sure what you mean. Put the reading and writing in a loop. Read the record(or whatever I came late to the thread), do what you gotta do then write the record to the file. Then loop back to the top and read the next record.

  4. #19
    Registered User
    Join Date
    Dec 2004
    Posts
    45
    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);
    }

    I dunno why but the program isn't asking me to insert the "From"...
    when I press "m" the output is:
    From:
    To:
    (insert to)
    .---------

    it skips the from, and then when I test it with "L" the from is empty...

  5. #20
    Quote Originally Posted by Tyler_Durden
    Code:
    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);
    }
    I dunno why but the program isn't asking me to insert the "From"...
    when I press "m" the output is:
    From:
    To:
    (insert to)
    .---------

    it skips the from, and then when I test it with "L" the from is empty...
    Probably because you have a pending '\n' in stdin. I guess you are using getchar() or scanf() in you menu...

    All inputs should be done with fgets().
    Emmanuel Delahaye

    "C is a sharp tool"

  6. #21
    Registered User
    Join Date
    Dec 2004
    Posts
    45
    ah ok...that problem is solved now..
    so the next step is to make some cycles in the writing and reading of the emails...but how can I make the loop in the writing so I get each mail and its characteristics into different variables..
    for example in the first mail I write when I open the program :
    subject: sub1
    from : from1
    message :msg1

    then when I write the second email, its subject is sub2, and its from is from2,etc....
    how can I do that? I can't manage to do that....
    thanks a lot for all your help till now, i'm really appreciated...

    EDIT- because right now, when I write a new email, the previous one is overwritten...

  7. #22
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You can use an array.

    Something like this:
    Code:
    {
      MAIL m[10];
      int i;
    
      for(i = 0;i < 10;++i)
      {
        // Get values for this mail message here
    
        commandom(&m[i]);
      }
    }
    If you understand what you're doing, you're not learning anything.

  8. #23
    Registered User
    Join Date
    Dec 2004
    Posts
    45
    Quote Originally Posted by itsme86
    You can use an array.

    Something like this:
    Code:
     void comandom(MAIL *ptr)
    {
      MAIL m[10];
      int i;
    
      for(i = 0;i < 10;++i)
      {
    
      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);
    }
    
        comandom(&m[i]);
      }
    }
    so, should my comandom.c be something like this?

  9. #24
    Quote Originally Posted by Tyler_Durden
    so the next step is to make some cycles in the writing and reading of the emails...but how can I make the loop in the writing so I get each mail and its characteristics into different variables..
    Why would you do that ?
    - Get the info
    - Save the info

    You should open your file in "a" mode. It will append to the end of it (or create it if non-existent)
    Emmanuel Delahaye

    "C is a sharp tool"

  10. #25
    Registered User
    Join Date
    Dec 2004
    Posts
    45
    but the problem is that the mails should only be saved when "q" is pressed and not when I insert the values...

  11. #26
    Quote Originally Posted by Tyler_Durden
    but the problem is that the mails should only be saved when "q" is pressed and not when I insert the values...
    So you want to input several mails before saving them ? Unsafe, but if you insist, you can use an array of structs as mentionned elswere.

    Of course a static array has a limited size. If you want a sky-is-the-limit solution, you want a flexible array (malloc() / realloc()) or a linked list...
    Emmanuel Delahaye

    "C is a sharp tool"

  12. #27
    Registered User
    Join Date
    Dec 2004
    Posts
    45
    Quote Originally Posted by Emmanuel Delaha
    So you want to input several mails before saving them ? Unsafe, but if you insist, you can use an array of structs as mentionned elswere.

    Of course a static array has a limited size. If you want a sky-is-the-limit solution, you want a flexible array (malloc() / realloc()) or a linked list...
    EDIT - ignore that, lol...It doesn't need to save only when "q" is pressed,lol....less a problem..now I'll try and fprintf all of the mails into the "mail.txt" file...wish me luck
    sorry if I'm being too boring and I'm asking for too much help, but I would really like to get the necessary grade to proceed...
    Last edited by Tyler_Durden; 12-30-2004 at 10:12 AM.

  13. #28
    Registered User
    Join Date
    Dec 2004
    Posts
    45
    Ok...the comandom.c is edited and know saves the mail info into the "mail.txt" file but there is a problem..
    I need the format of the mail.txt file to be like this:
    To:{[email protected]}
    From:{[email protected]}
    Subject:{blabla}
    {bla bla bla bla}

    so for that my fprintfs are this way:
    fprintf(fp,"{%s}",ptr->to);
    ...
    ..

    but the mail.txt program turns out like this:
    to:{[email protected]
    }
    From{[email protected]
    }
    Subject:{ggd
    }
    etc...


  14. #29
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> but the mail.txt program turns out like this:

    you need to remove the newline retrieved from fgets().

    >> but I would really like to get the necessary grade to proceed...

    if you *really* wanted that grade you would have started a lot sooner than this.
    Last edited by Sebastiani; 12-30-2004 at 10:47 AM.
    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. #30
    Hello,

    Keep in mind fgets reads the "newline" character '\n' and puts it at the end of the string as an extra character. There are ways of removing the last character from your string. A simple way is the following:
    Code:
    #include <stdio.h>
    
    int main() {
    	char s[25], *pch;
    
    	fgets(s, 24, stdin);
    
    	// Search for newline
    	if (pch = strrchr(s, '\n'))
    		*pch = '\0';
    
    	printf("%s", s);
    
    	return 0;
    }
    There are other ways to accomplish this also. There was once a discussion about this in great detail. You can find the thread here: Using fgets, removing last character.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

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