Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
It's a simple code, should run fine, i read something online about the error and it's about memory handling, can someone find the error and explain me why?
You see i just don't know enough of programming to know what's wrong here so any help would be very appreciated, thanks guys, i'll still keep looking at the program.
#include <time.h> /* para calcular data e duracao */
#include <stdio.h>
#include <string.h>

typedef struct relatorioDeChegada
{
    int codCorredor;
    char nomeCorredor[40];
    struct tm t;/* duracao da corrida e data também*/
}relatorio;

int main(void)
{
    int i , choice;
    FILE *fp1;
    relatorio relatorio;
    int recsize;
    char c;

    fp1 = fopen("relatorio.dat" , "r+");
    if(fp1 == NULL)
    {
        fp1 = fopen("relatorio.dat" , "w+");
        if(fp1 == NULL)
        {
            printf("error in opening file : \n");
            return -1;
        }
    }
    recsize = sizeof(relatorio);

    fseek(fp1 , 0 ,SEEK_END);

    printf("Digite o nome do corredor ");
    scanf("%[^\n]%*c" ,&relatorio.nomeCorredor);

    printf("Digite o codigo do corredor");
    scanf("%d" , &relatorio.codCorredor);

    printf("\n Digite o dia da corrida");
    scanf("%d" , &relatorio.t.tm_yday);

    printf("\n Digite o mes da corrida");
    scanf("%d" , &relatorio.t.tm_mon);

    printf("\n Digite o ano da corrida");
    scanf("%d" , &relatorio.t.tm_year);

    printf("\n Digite a duracao da corrida em horas");
    scanf("%d" , &relatorio.t.tm_hour);

    printf("\n Digite a duracao da corrida em minutos");
    scanf("%d" , &relatorio.t.tm_min);

    printf("\n Digite a duracao da corrida em segundos");
    scanf("%d" , &relatorio.t.tm_sec);

    fwrite(&relatorio,recsize,1,fp1);
    fclose(fp);
}
Posted
Comments
[no name] 16-Aug-14 18:54pm    
You say "the error" like we would have any idea at all which error you are talking about.
[no name] 16-Aug-14 20:34pm    
Your program will not run as it won't compile. fp is not defined anywhere. It should be fclose(fp1);
Mohibur Rashid 16-Aug-14 22:05pm    
One error i have noticed is in main function, the definition of a variable of the struct type you have defined. Your struct name and variable name are same. Line 4 of main function. You wrote fwrite function wrong. Check the manual please

1 solution

You can't run a program until it compiles without errors, (and preferably without warning either) - it's a bit like getting someone to do your shopping for you: if they can't read your shopping list, they can't get what you want.

And as has been mentioned, there are at least two problems which prevent this compiling.
Fix the compilation errors, (or ask us how to fix them - but give us the error information) and try again.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900