email program in c - help me solve one error,please...

T

tyler_durden

thanks a lot for all your help..I'm really appreciated...
with all the help I've been getting in forums I've been able to continue
my program and it's almost done, but I'm having a big problem that I
believe if it's solved, the remaining stuff is easy...
my full program until now is here:
http://www.geocities.com/tom4_h4wk/progmail.zip

the problem is the segmentation fault when main trys to run
leficheiro.c.... the *.c2 files are the functions I am working now...
please, someone download the file and help me solving that error..I need
to have the program done tomorrow...
 
N

Nick Keighley

tyler_durden said:
thanks a lot for all your help..I'm really appreciated...
with all the help I've been getting in forums I've been able to continue
my program and it's almost done, but I'm having a big problem that I
believe if it's solved, the remaining stuff is easy...
my full program until now is here:
http://www.geocities.com/tom4_h4wk/progmail.zip

the problem is the segmentation fault when main trys to run
leficheiro.c.... the *.c2 files are the functions I am working now...
please, someone download the file and help me solving that error..I need
to have the program done tomorrow...

I'm sure you are a completly trustworthy person, but I'm unwilling to
download unknown files. I don't even know how big it is.

Could you boil down your problem into something that is postable,
say around 20 lines? I'd guess the problem is in leficheiro.c....



--
Nick Keighley

"Beware of bugs in the above code;
I have only proved it correct, not tried it."
Donald Knuth
 
F

Flash Gordon

tyler_durden said:
thanks a lot for all your help..I'm really appreciated...
with all the help I've been getting in forums I've been able to continue
my program and it's almost done, but I'm having a big problem that I
believe if it's solved, the remaining stuff is easy...
my full program until now is here:
http://www.geocities.com/tom4_h4wk/progmail.zip

the problem is the segmentation fault when main trys to run
leficheiro.c.... the *.c2 files are the functions I am working now...
please, someone download the file and help me solving that error..I need
to have the program done tomorrow...

Consistent indenting would help. I've changed the indenting to something
more useful and commented the problems.

Please note, that the following code is NOT mine, it is tyler_durden's
with a few modifications. Please keep this statement in any replies
since I don't want anyone thinking I would write code like this!

/* progmail.h */
#ifndef PROGMAIL_H_INCLUDED
#define PROGMAIL_H_INCLUDED
#define MAX 120
#define NUM 20

typedef struct mail
{
char from[MAX+1];
char to[MAX+1];
char sub[MAX+1];
char msg[MAX+1];
} MAIL;

typedef struct
{
MAIL mail;
}MAILR[NUM];

void leficheiro(MAILR[]);
void comandom(MAIL *m);
void comandoC(MAIL *m);
void comandoh(MAILR[]);
void comandoL(MAIL *m);
#endif

/* leficherio.c */
#include <stdio.h>
#include <stdlib.h>
/* #include <string.h> you are not using anything from string.h so why
include it? */
#include "progmail.h"

void leficheiro(MAILR *ptr)
{
FILE *fp;
int i;

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

/* Added error checking */
if (fp == NULL) {
fprintf(stderr, "Failed to open file mail.txt\n");
exit(EXIT_FAILURE);
}

for(i=0;i<10;i++){
/*
fgets("MAILR.to",120,fp);
fgets("MAILR.from",120,fp);
fgets("MAILR.sub",120,fp);
fgets("MAILR.msg",120,fp);
fgets("MAILR.spc",120,fp);
*/
/* Why the **** have you passed a string literal to fgets? It expects
the buffer it is to write the data to. Also, should use the MAX you
#defined in progmail.h rather than the magic number 120. Finally, DO
SOME ERROR CHECKING! */
char *res;
res = fgets(ptr.to,MAX,fp);
if (res == NULL) {
/* handle error */
}
/* You need to check the last character in the string fgets has read.
Check the definition of fgets and you will see why. */
}
fclose(fp);
}

/* 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 */
#include "progmail.h"

/* int help(){ you don't use the result, so why have a return value? */
void help(void)
{
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; /* You have declared this as a single instance */

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

leficheiro(&mailr); /* This expects an array of 10 instances */

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;
}

I think you have problems with all your other functions as well.
 
?

=?ISO-8859-1?Q?Bj=F8rn_Augestad?=

tyler_durden said:
thanks a lot for all your help..I'm really appreciated...
with all the help I've been getting in forums I've been able to continue
my program and it's almost done, but I'm having a big problem that I
believe if it's solved, the remaining stuff is easy...
my full program until now is here:
http://www.geocities.com/tom4_h4wk/progmail.zip

the problem is the segmentation fault when main trys to run
leficheiro.c.... the *.c2 files are the functions I am working now...
please, someone download the file and help me solving that error..I need
to have the program done tomorrow...

Below is a copy of your source code. A couple of comments:
a) fgets() expects a pointer to writable memory, but you are giving a
string literal as argument.
b) Even if the literals were writable, the length argument is incorrect
c) You try to read 50 lines from mail.txt, but the file contains 13 lines.

What you need to do is:
a) Remove the quotes (") in your calls to fgets()
b) Use the ptr variable as argument to fgets(), as in
fgets(ptr->mail.to, 120, fp);
(Someting like that ;-)
c) Add error checking/handling

Good luck.
Bjørn
#include <stdio.h>
#include <string.h>
#include "progmail.h"

void leficheiro(MAILR *ptr)
{
FILE *fp;
int i;

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

for(i=0;i<10;i++){
fgets("MAILR.to",120,fp);
fgets("MAILR.from",120,fp);
fgets("MAILR.sub",120,fp);
fgets("MAILR.msg",120,fp);
fgets("MAILR.spc",120,fp);
}
fclose(fp);

}
 
I

infobahn

tyler_durden said:
thanks a lot for all your help..I'm really appreciated...
with all the help I've been getting in forums I've been able to continue
my program and it's almost done, but I'm having a big problem that I
believe if it's solved, the remaining stuff is easy...
my full program until now is here:
http://www.geocities.com/tom4_h4wk/progmail.zip

the problem is the segmentation fault when main trys to run
leficheiro.c.... the *.c2 files are the functions I am working now...
please, someone download the file and help me solving that error..I need
to have the program done tomorrow...

Here's the code for that file, with my comments:

#include <stdio.h>
#include <string.h>
#include "progmail.h"

void leficheiro(MAILR *ptr)
{
FILE *fp;
int i;

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

/* CHECK that this request succeeded, by
comparing fp against NULL. If fp is
NULL, the file could not be opened.
*/

for(i=0;i<10;i++){
fgets("MAILR.to",120,fp);

/* I haven't inspected the calling code,
but I hope this function is being
passed a pointer to the first element
in an array of 10 MAILR objects.
If so, then this code is almost there.
You need to remove the quotes! You
also need to specify the base address
of the target object for the data,
rather than the type. So use:

fgets(ptr.to, 120, fp);

instead. Note that, if ptr.to is
an array, you can instead use

fgets(ptr.to, sizeof ptr.to, fp);

which is more robust. Also note that
you should check the return value of
fgets(). It /can/ fail.
*/

fgets("MAILR.from",120,fp);
fgets("MAILR.sub",120,fp);
fgets("MAILR.msg",120,fp);
fgets("MAILR.spc",120,fp);

/* The same applies to all these. */

}
fclose(fp);

}
 
T

tyler_durden

thanks for yuour help...I dunno why but I tried substituting all the things
like you said, and removed the quotes, but I get an error for each line
from 13 to 17 like this:

->request for member "from" in something not a structure or union
 
K

Keith Thompson

tyler_durden said:
please, someone download the file and help me solving that error..I need
to have the program done tomorrow...

Why? What is the point of writing this program? If we know that, we
may be able to help you more effectively (or decide whether to help
you at all).

The only explanation I can think of is that this is a homework
assignment. If you're taking a class, presumably there are local
resources you can use (like asking your instructor).
 
N

Nick Keighley

tyler_durden said:
thanks for yuour help...I dunno why but I tried substituting all the things
like you said, and removed the quotes, but I get an error for each line
from 13 to 17 like this:

->request for member "from" in something not a structure or union

post the offending lines. At a guess you're using S.memeber when S is
not
a structure. If S is a pointer to a structure use S->member
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,157
Messages
2,570,879
Members
47,414
Latest member
djangoframe

Latest Threads

Top