By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,068 Members | 1,734 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,068 IT Pros & Developers. It's quick & easy.

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

P: n/a
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...

Nov 14 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
tyler_durden wrote:
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

Nov 14 '05 #2

P: n/a
tyler_durden wrote:
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[i].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[i].to",120,fp);
fgets("MAILR[i].from",120,fp);
fgets("MAILR[i].sub",120,fp);
fgets("MAILR[i].msg",120,fp);
fgets("MAILR[i].spc",120,fp);
}
fclose(fp);

}


Nov 14 '05 #3

P: n/a
tyler_durden wrote:
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[i].to",120,fp);
fgets("MAILR[i].from",120,fp);
fgets("MAILR[i].sub",120,fp);
fgets("MAILR[i].msg",120,fp);
fgets("MAILR[i].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[i].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.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #4

P: n/a
tyler_durden wrote:

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[i].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[i].to, 120, fp);

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

fgets(ptr[i].to, sizeof ptr[i].to, fp);

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

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

/* The same applies to all these. */

}
fclose(fp);

}
Nov 14 '05 #5

P: n/a
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

Nov 14 '05 #6

P: n/a
"tyler_durden" <to*******@hotmail.com> writes:
[...]
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).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <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.
Nov 14 '05 #7

P: n/a
tyler_durden wrote:
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
--
Nick Keighley

Nov 14 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.