473,386 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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
7 3232
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
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
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
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: gudia | last post by:
I am using CDO to email a file as an attachment. The receiver receives the file, but when he opens the file (tried using both Acrobat Reader 6 and 5), he gets "There was an error opening this...
4
by: Sean Shanny | last post by:
To all, Running into an out of memory error on our data warehouse server. This occurs only with our data from the 'September' section of a large fact table. The exact same query running over...
14
by: Professor Yonce | last post by:
I have made form for E-Mail. I have entered code but the Import system does not work. It has squiggly line underneath it showing it is not communicating. It Will not build. Public Class...
1
by: sang | last post by:
Hi I prepared a program for Email Integration. That is email send to others through servlet's. I gave the code but it has some error in my code. Please check the code and give the feedback. ...
1
markmcgookin
by: markmcgookin | last post by:
Hi Folks, this is a problem I had a few days ago, and I was able to solve it, but I thought I'd post the solution I found here incase anyone has a similar problem! I found this stuff on an MSNDN...
2
by: ...:::JA:::... | last post by:
Hi, I using "Dev-C++", yesterday I was install Microsoft Visual Studio .NET 2003 I need it for building python extensions.Since I was install it when I write some program in Dev-C++ for...
2
by: Manikandan | last post by:
Hi, I have a program written in .Net Framework 1.1 using Visual studio enterprise edition 2003. I tried compiling the same program in visual c# express edition 2005. I'm getting following...
2
Banfa
by: Banfa | last post by:
Posted by Banfa The previous tutorial discussed what programming is, what we are trying to achieve, the answer being a list of instructions constituting a valid program. Now we will discuss how...
3
by: newbprogrammer | last post by:
I just started programming in c++ and i tried to explore and do programs on my own... So i wanted to do a program that would help me in my daily school work ... a program to solve quadratic...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.