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.