You're passing mailr, a single instance of a MAILR struct, to leficheiro. But then in leficheiro you're trying to use it as an array. You can't do that.
Printable View
You're passing mailr, a single instance of a MAILR struct, to leficheiro. But then in leficheiro you're trying to use it as an array. You can't do that.
so can you please tell me what to change in the code to solve the error? I would be really thankful :DQuote:
Originally Posted by itsme86
In quite a few places, you are using the MAIL or the MAILR structure as if it is a variable, when it is not.
For example, in leficheiro.c, the lines 13-17, you use this following code:
Where it SHOULD be using the ptr variable. Also, when using variables, you don't put them inside quotes as you have. The compilable, working code should look like this:Code: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);
I got rid of the .spc value in there as I didn't find an spc variable anywhere....Code:fgets(ptr[i].mail.to,120,fp);
fgets(ptr[i].mail.from,120,fp);
fgets(ptr[i].mail.sub,120,fp);
fgets(ptr[i].mail.msg,120,fp);
But that is basically your problem, that same thing occurs elsewhere in the program, you just need to remember to get rid of the quotes and make sure you are pointing to the correct values.
MAILR is an array of MAIL, therefor any MAILR variable should look like so:
That is rough pseudocode, but you should see what I mean.Code:MAILR variable;
variable[0].mail.<place variable here>
Also, you do not need to pass a pointer to a MAILR variable, as I noticed you do in one of the functions you use MAILR[] which isn't necessary, as the MAILR structure is already declared as an array, which in itself is a pointer. So it's best to remove the [] and also the MAILR *ptr in the declaration of the function.
Hopefully this clears a bit up, I have a working, compileable version of the code, but I want to see if you can fix it yourself.
Pass the address of the array and its number of element.Quote:
Originally Posted by Tyler_Durden
rather than the address of a single variable :Code:int a[3];
f(a, 3);
Code:int a;
f(&a);
Quote:
Originally Posted by jverkoey
i changed my leficheiro.c like you said but when compiling it gives me this error for lines 13-17
-> request for member "MAILR" in something not a structure or union
and like you say in the end of your post, how should I declare then the functions?
In your source, progmail.h:
should be:Code:void leficheiro(MAILR[]);
And you need to change the leficheiro function in the definition file respectively so that it is just MAILR ptr in the parameter list.Code:void leficheiro(MAILR);
Quote:
Originally Posted by jverkoey
ok, i changed progmail.h, but what you said in the end i didn't understand..definition file? parameter list? :confused: sorry but I'm really not getting it....God, I have to do this quickly, lol :cool:
Gah, sorry.
Ok, where you actually define the function, is what I meant by the "definition file." This is the file where you find the actual function. The parameter list is everything found inside of the parenthesis.
So:
myfunc(int a, int b)
has two parameters.
-edit-
W00T, POST 1000!!
*does a dance*
so in leficheiro.c I should declare it like this?
void leficheiro(MAILR,i) ??
I tried it but if I don't declare it leficheiro(MAILR *ptr) it then says in lines from 13-17 that ptr is undeclared...
It's being difficult solving this and I still have to solve the fgets "\n" problem..lol
This is what I have for leficheiro.c:
and it compiles fine.Code:#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(ptr[i].mail.to,120,fp);
fgets(ptr[i].mail.from,120,fp);
fgets(ptr[i].mail.sub,120,fp);
fgets(ptr[i].mail.msg,120,fp);
}
fclose(fp);
}
-edit-
I'm not sure exactly what you are trying to achieve with this program, as when I ran it it appeared all it did was took input and then I could display it. I'm assuming you want to somehow keep track of multiple emails, in which case you will want to make the function in the way you stated above with the extra int i variable to say which slot in the array to read in to.
Quote:
Originally Posted by jverkoey
yep, I need to keep track of numerous emails... I have to save them to "mail.txt" to and be able to view them, and erase them...
/me wants to dance too when the program is done :cool:
EDIT - changing my progmail.h like you posted gets me this error from lines 13-17
structure has no member named MAILR :(
Ok, then you'll need to define your function as:
void leficheiro(MAILR,int)
in the .h file
and
void leficheiro(MAILR ptr,int i)
in the .c file.
Then get rid of the for loop using the i variable and it should work fine. Just make sure to put checking in there so i is 0<=i<MAXSIZE where MAXSIZE is the size of your array.
Done...Quote:
Originally Posted by jverkoey
but the error "structure has no member named MAILR " is still there...
this is so frustrating... I had the program working but now I can't seem to get it... Bye bye positive grade ... :rolleyes:
Can you post what's currently in the leficheiro.c file? It seems something's different between our two compilations somehow. Also, what compiler are you using? I compiled this successfully with VC++6.0
Quote:
Originally Posted by jverkoey
I'm using gcc...Code:#include <stdio.h>
#include <string.h>
#include "progmail.h"
void leficheiro(MAILR ptr, int i)
{
FILE *fp;
int i;
fp = fopen("mail.txt","r"); /*Abre o ficheiro para escrita em modo append */
for(i=0;i<10;i++){ /*Ciclo para atribuir um numero a cada email*/
fgets(ptr[i].MAILR.to,120,fp); /*Obtenção dos parametros dos mails a partir do ficheiro e passagem para estrutura*/
fgets(ptr[i].MAILR.from,120,fp);
fgets(ptr[i].MAILR.sub,120,fp);
fgets(ptr[i].MAILR.msg,120,fp);
fgets(ptr[i].MAILR.spc,120,fp);
}
fclose(fp); /*Fecho do ficheiro*/
}
my progmail.h is like this:
and progmail.cCode:#ifndef PROGMAIL_H_INCLUDED
#define PROGMAIL_H_INCLUDED
#define MAX 120
#define NUM 20
typedef struct mail /*Definição da estrutura dos mails escritos*/
{
char from[MAX+1];
char to[MAX+1];
char sub[MAX+1];
char msg[MAX+1];
} MAIL;
typedef struct /*Definição da estrutura dos mails lidos do ficheiro*/
{
MAIL mail;
}MAILR[NUM];
void leficheiro(MAILR, int); /Declaração dos comandos usados no main*/
void comandom(MAIL *m);
void comandoC(MAIL *m);
void comandoh(MAILR[]);
void comandoL(MAIL *m);
#endif
Code:#include <stdio.h> /* funções como printf, scanf, etc */
#include <stdlib.h> /* função exit */
#include <string.h> /* funções relacionadas com strings */
#include "progmail.h" /* Para uso de funcoes inerentes a todo o programa*/
int help(){ /*Função de ajuda*/
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;
printf("E-mail program.\n");
leficheiro(&mailr);
do{
putchar ('&');
fgets(input,BUFSIZ,stdin); /*Detecção do input do user*/
c = input[0];
switch (c) /*Detecção do comando correspondente ao input do user */
{
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; /*Detecta se o comando existe no programa e mostra-o ao user*/
}
}while (c!='x');
return 0;
}