Thread: Problem with reading from file... problem with strings

  1. #31
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You can re-use the same string for all your inputs. Let's call this string buffer and give it a size of MAX_BUFFER which we define a 100.

    Code:
    #define MAX_BUFFER 100
    ...
    
    char buffer[MAX_BUFFER];
    
    ...
    
    printf("Please enter the telephone number: ");
    fgets(buffer, MAX_BUFFER, stdin);
    sscanf(buffer, " %d", &contacto[i].telefone);
    // now get the next value:
    fgets(buffer, MAX_BUFFER, stdin);
    // Now retrieve that value from the string, like above but use the proper variable.
    Jim

  2. #32
    Registered User
    Join Date
    Jun 2014
    Posts
    41
    i got it. i solved the problem with the first fgets skipping... i put a fflush(stdin) after my first printf and it worked... another problem solved

  3. #33
    Registered User
    Join Date
    Jun 2014
    Posts
    41
    Quote Originally Posted by jimblumberg View Post
    You can re-use the same string for all your inputs. Let's call this string buffer and give it a size of MAX_BUFFER which we define a 100.

    Code:
    #define MAX_BUFFER 100
    ...
    
    char buffer[MAX_BUFFER];
    
    ...
    
    printf("Please enter the telephone number: ");
    fgets(buffer, MAX_BUFFER, stdin);
    sscanf(buffer, " %d", &contacto[i].telefone);
    // now get the next value:
    fgets(buffer, MAX_BUFFER, stdin);
    // Now retrieve that value from the string, like above but use the proper variable.
    Jim
    so i use the buffer for the numbers(grades and telephone(i will put this one as a char))?
    i gonna try it. thanks


    EDIT: working fine. many thanks
    Last edited by Ruben Marques; 06-29-2014 at 06:18 PM.

  4. #34
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    i put a fflush(stdin) after my first printf and it worked... another problem solved
    Not really, fflush() on an input stream is undefined, it may work today on your paticular operating system with your paticular compiler but it will break if you change either of these items. You should read the FAQ, there is an entry in the FAQ about this issue.

    Jim

  5. #35
    Registered User
    Join Date
    Jun 2014
    Posts
    41
    when i create a new file it always start with a line in blank, but after that works fine... is because if the "a" mode that i use?

    for reading the file and show it on screen any recomendation? i was thinking to do a fgets(for the strings, line by line) and a fscanf(for the number, read to the last number after a blank)... and then copy everything to the computer memory and work it from there(search something is one of the things that i have to do);

    to delete in the file if i have this strcpy thing i just use my delete code that i have for the info that is on the memory and then save in the file again(in this case i have to use "w");

    i.e

    read file ->copy to pc memory -> do whatever i have to do(delete information in this case)-> save in the file(overwriting what i had before);

  6. #36
    Registered User
    Join Date
    Jun 2014
    Posts
    41
    Quote Originally Posted by jimblumberg View Post
    Not really, fflush() on an input stream is undefined, it may work today on your paticular operating system with your paticular compiler but it will break if you change either of these items. You should read the FAQ, there is an entry in the FAQ about this issue.

    Jim
    danm... i thought i was getting good i will read it then. thank you... again


    Edit: i don't knnow if is this what i want
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392

    i can't find anything else

    i also saw some people recomend put getchar()
    Last edited by Ruben Marques; 06-29-2014 at 06:43 PM.

  7. #37
    Registered User
    Join Date
    Jun 2014
    Posts
    41
    i finished almost everything but now i don't know what i should do... i have an option in the program that removes information from the file.

    i did it this way
    open file -> copy everything to pc memory -> delete what i want -> now i need to save(i made a funtion only for this one)... should i save in the same file by using "w" or remove the file, creating a new one(and save it here) and rename it with the original file name?...

    wich is the best option??

  8. #38
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Edit: i don't knnow if is this what i want
    This is usually okay:
    Code:
    int ch;
    while ((ch = getchar()) != '\n' && ch != EOF);
    Fact - Beethoven wrote his first symphony in C

  9. #39
    Registered User
    Join Date
    Jun 2014
    Posts
    41
    how do i clear my memory??? i'm using two menus...one for input from keyboard to pc and the other to input from keyboard to file

    but to do "stuff" with the file information i copy it from the file to the pc memory.. but when i need to use only the input->pc the information that i got from the file is still there... how can i clear that?

  10. #40
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    but to do "stuff" with the file information i copy it from the file to the pc memory..
    You mean you place the information into a variable.
    but when i need to use only the input->pc the information that i got from the file is still there... how can i clear that?
    You will need reassign the value of your variable to it's default value.

    Jim

  11. #41
    Registered User
    Join Date
    Jun 2014
    Posts
    41
    there is a place where i can see i to do it?.
    my program is now working like a charm except for this,
    a problem with my search menus,
    and when i use the show the contacts option from the file(i only got one contact there) i get two, one with everything correct and the next one missing the name only, but this should be printing...if i have 2 i got 3,1st ok, 2nd bad, 3rd ok and so on. the second one is always bad(should be there) and the other ones are correct


    but for now i need to fix the cleaning thing and this

    search menu
    by number
    by name
    if i do this by keyboard only works great, but when i use the file to search it always give me "invalide name/number"

    my function to gets the info from the file
    Code:
    void abrir(dados *contacto)
    {
        FILE *f;
        char nome[MAX_NOME], email[MAX_EMAIL], morada[MAX_MORADA], postal[MAX_POSTAL], numero[MAX_NUMERO];
        int telefone, FP, SD, AM1, ALGA, CM;
    
    
        //printf("insira o caminho do ficheiro\n");
        //scanf("%s", &nfich);
    
    
        f = fopen("teste.txt", "r");
        if (f == NULL)
        {
            printf("erro ao abrir ficheiros");
        }
        {
            while (!feof(f))
            {
                fgets(nome, MAX_NOME, f);
                fgets(numero, MAX_NUMERO, f);
                fgets(morada, MAX_MORADA, f);
                fgets(postal, MAX_POSTAL, f);
                fgets(email, MAX_EMAIL, f);
                fscanf(f, "%d %d %d %d %d %d", &telefone, &FP, &AM1, &SD, &ALGA, &CM);
                strcpy(contacto[i].nome, nome);
                strcpy(contacto[i].numero, numero);
                strcpy(contacto[i].morada, morada);
                strcpy(contacto[i].postal, postal);
                strcpy(contacto[i].email, email);
                contacto[i].telefone = telefone;
                contacto[i].FP = FP;
                contacto[i].AM1 = AM1;
                contacto[i].SD = SD;
                contacto[i].ALGA = ALGA;
                contacto[i].CM = CM;
                i++;
            }
            fclose(f);
        }
    
    
    }
    my function to do the search
    Code:
    void pesquisar1(dados* contacto)
    {
        char proc[50];
        int j;
    
    
        printf("Name to search: ");
        scanf("%s", proc);
    
    
        for (j = 0; j<i; j++)
        {
            if (0 == strcmp(proc, contacto[j].nome))
            {
                system("cls");
                printf("\n\nDados: \n");
                printf("\tName: %s", contacto[j].nome);
                printf("\n\tEmail: %s", contacto[j].email);
                printf("\n\tTelefone: %d", contacto[j].telefone);
                printf("\n----------------------------------\n");
                system("pause");
                return;
            }
        }
    
    
        printf("\nNome invalido\n");
        system("pause");
    }

    the problem may be on the scanf?? should i use fgets()?
    Last edited by Ruben Marques; 06-30-2014 at 10:31 AM.

  12. #42
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    the problem may be on the scanf?? should i use fgets()?
    Yes!! USE fgets() EVERYWHERE YOU CAN! AVOID THE FSCANF() SCANF() FUNCTIONS WHENEVER YOU CAN!

    Code:
                fgets(nome, MAX_NOME, f);
                fgets(numero, MAX_NUMERO, f);
                fgets(morada, MAX_MORADA, f);
                fgets(postal, MAX_POSTAL, f);
                fgets(email, MAX_EMAIL, f);
                fscanf(f, "%d %d %d %d %d %d", &telefone, &FP, &AM1, &SD, &ALGA, &CM); // USE fgets() sscanf() instead of fscanf()!!!
                strcpy(contacto[i].nome, nome); // WHY?? WHY NOT USE THE CORRECT VARIABLE ABOVE??

    Jim

  13. #43
    Registered User
    Join Date
    Jun 2014
    Posts
    41
    i changed it and it's not working

    i will put my code here
    menu: 1- enter 0 exit
    menu2: 1-from keyboard 2-from file 3-exit
    menu3(equal on both)
    1-show every contact
    2-add
    3-delete(not done it for file, i working on it)
    4 and 5- search by name
    6 and 7- search by number
    8- qsort(not done, i will not do it, tryed and not able to do it)
    9- back to menu2


    the stuff that enter this particular problem is
    void abrir(open file and get the info)
    void adicionar(input from keyboard)
    void pesquisar1(2, 3 or 4) (search by name/number);

    total code
    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <stdlib.h>
    #include <String.h>
    #include <windows.h> 
    
    
    const int total = 25;
    int i = 0;
    
    
    #define MAX_NOME 100
    #define MAX_NUMERO 10
    #define MAX_MORADA 100
    #define MAX_POSTAL 50
    #define MAX_EMAIL 100
    #define MAX_GERAL 100
    
    
    typedef struct {
        char nome[MAX_NOME], email[MAX_EMAIL], morada[MAX_MORADA], postal[MAX_POSTAL], numero[MAX_NUMERO];
        int telefone, FP, SD, AM1, ALGA, CM;
    }dados;
    void libertar(dados*contacto)
    {
        dados *libertar;
        libertar = (dados*)malloc(sizeof(libertar));
        //...
        free(libertar);
    }
    int sair()
    {
        char a;
    
    
    
    
        fflush(stdin);
        printf("deseja relamente sair do programa [s- Sim/n- Nao]: ");
        scanf("%c", &a);
        // a=getch();
    
    
        if (a == 's' || a == 'S')
            exit(1);
    
    
        while (a != 'n' && a != 'N');
    }
    void titulo()
    {
        HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
        WORD wOldColorAttrs;
        CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
    
    
        /*
        * First save the current color information
        */
        GetConsoleScreenBufferInfo(h, &csbiInfo);
        wOldColorAttrs = csbiInfo.wAttributes;
    
    
        /*
        * Set the new color information
        */
        SetConsoleTextAttribute(h, FOREGROUND_GREEN | FOREGROUND_INTENSITY);
    
    
        printf("\n\t.:: AGENDA ELECTRONICA EM LINGUAGEM C [3 DE JULHO DE 2014] ::.\n\n\t ");
    
    
    
    
        /*
        * Restore the original colors
        */
        SetConsoleTextAttribute(h, wOldColorAttrs);
    
    
    }
    void substituir(dados *contacto)
    {
        FILE *f;
        //char nfich[20];
        int j = 0;
    
    
        //printf("insira o caminho do ficheiro\n");
        //scanf("%s", &nfich);
    
    
        f = fopen("teste.txt", "w+");
    
    
    
    
        for (j = 0; j<i; j++)
        {
            fprintf(f, "%s", contacto[j].nome);
            
            fprintf(f, " %s ", contacto[j].numero);
            
            fprintf(f, " %s ", contacto[j].morada);
            
            fprintf(f, " %s ", contacto[j].postal);
            
            fprintf(f, " %s ", contacto[j].email);
            
            fprintf(f, " %d ", contacto[j].telefone);
            
            fprintf(f, " %d ", contacto[j].FP);
            
            fprintf(f, " %d ", contacto[j].AM1);
            
            fprintf(f, " %d ", contacto[j].SD);
            
            fprintf(f, " %d ", contacto[j].ALGA);
            
            fprintf(f, " %d ", contacto[j].CM);
            
        }
    
    
    
    
        printf("Dados guardados com suscesso!!! \n");
        fclose(f);
    }
    
    
    void abrir(dados *contacto)
    {
        FILE *f;
        char nome[MAX_NOME], email[MAX_EMAIL], morada[MAX_MORADA], postal[MAX_POSTAL], numero[MAX_NUMERO], geral[MAX_GERAL];
        int telefone, FP, SD, AM1, ALGA, CM;
    
    
        //printf("insira o caminho do ficheiro\n");
        //scanf("%s", &nfich);
    
    
        f = fopen("teste.txt", "r");
        if (f == NULL)
        {
            printf("erro ao abrir ficheiros");
        }
        {
            while (!feof(f))
            {
                fgets(nome, MAX_NOME, f);
                fgets(numero, MAX_NUMERO, f);
                fgets(morada, MAX_MORADA, f);
                fgets(postal, MAX_POSTAL, f);
                fgets(email, MAX_EMAIL, f);
                fgets(geral, MAX_GERAL, f);
                sscanf(geral, "%d", &telefone);
                fgets(geral, MAX_GERAL, f);
                sscanf(geral, " %d", &FP);
                fgets(geral, MAX_GERAL, f);
                sscanf(geral, " %d", &AM1);
                fgets(geral, MAX_GERAL, f);
                sscanf(geral, " %d", &SD);
                fgets(geral, MAX_GERAL, f);
                sscanf(geral, " %d", &ALGA);
                fgets(geral, MAX_GERAL, f);
                sscanf(geral, " %d", &CM);
                strcpy(contacto[i].nome, nome);
                strcpy(contacto[i].numero, numero);
                strcpy(contacto[i].morada, morada);
                strcpy(contacto[i].postal, postal);
                strcpy(contacto[i].email, email);
                contacto[i].telefone = telefone;
                contacto[i].FP = FP;
                contacto[i].AM1 = AM1;
                contacto[i].SD = SD;
                contacto[i].ALGA = ALGA;
                contacto[i].CM = CM;
                i++;
            }
            fclose(f);
        }
    
    
    }
    
    
    void mostrar(dados* contacto)
    {
        int j = 0;
    
    
        system("cls"); 
        titulo();
        fflush(stdin);
        for (j = 0; j<i; j++)
        {
            printf("\t%d->   \n\tNome:  %s |", j + 1, contacto[j].nome);
            printf(" \n\tNumero de Aluno ISEL: %s |", contacto[j].numero);
            printf(" \n\tMorada: %s |", contacto[j].morada);
            printf(" \n\tCodigo Postal: %s |", contacto[j].postal);
            printf(" \n\tE-Mail: %s |", contacto[j].email);
            printf(" \n\tNumero de telefone: %d |", contacto[j].telefone);
            printf(" \n\tNota final de FP: %d |", contacto[j].FP);
            printf(" \n\tNota final de AM1: %d |", contacto[j].AM1);
            printf(" \n\tNota final de SD: %d |", contacto[j].SD);
            printf(" \n\tNota final de ALGA: %d |", contacto[j].ALGA);
            printf(" \n\tNota final de CM: %d\t\n\n\n |", contacto[j].CM);
            
        }
    }
    void f_adicionar(dados* contacto)
    {
        FILE*f;
        f = fopen("teste.txt", "a");
    
    
        char nome[MAX_NOME], email[MAX_EMAIL], morada[MAX_MORADA], postal[MAX_POSTAL], numero[MAX_NUMERO],geral[MAX_GERAL];
        int telefone, FP, SD, AM1, ALGA, CM;
    
    
        if (i<total)
        {
            
            printf("Introduza o Nome:  ");
            fflush(stdin);
            fgets(contacto[i].nome, MAX_NOME, stdin);
            fprintf(f, "%s", contacto[i].nome);
            
            printf("Introduza o Numero de Aluno ISEL:  ");
            fgets(contacto[i].numero, MAX_NUMERO, stdin);
            fprintf(f, "%s", contacto[i].numero);
    
    
            printf("Introduza a Morada:  ");
            fgets(contacto[i].morada, MAX_MORADA, stdin);
            fprintf(f, "%s", contacto[i].morada);
    
    
            printf("Introduza o Codigo Postal:  ");
            fgets(contacto[i].postal, MAX_POSTAL, stdin);
            fprintf(f, "%s", contacto[i].postal);
            
            printf("Introduza o email de Aluno ISEL:  ");
            fgets(contacto[i].email, MAX_EMAIL, stdin);
            fprintf(f, "%s", contacto[i].email);
            
            printf("Introduza o telefone:  ");
            fgets(geral, MAX_GERAL, stdin);
            sscanf(geral," %d", &contacto[i].telefone);
            fprintf(f, " %d", &contacto[i].telefone);
            printf("Nota Final de FP:  ");
            fgets(geral, MAX_GERAL, stdin);
            sscanf(geral, " %d", &contacto[i].FP);
            fprintf(f, " %d", &contacto[i].FP);
            printf("Nota Final de AM1:  ");
            fgets(geral, MAX_GERAL, stdin);
            sscanf(geral, " %d", &contacto[i].AM1);
            fprintf(f, " %d", &contacto[i].AM1);
            printf("Nota Final de SD:  ");
            fgets(geral, MAX_GERAL, stdin);
            sscanf(geral, " %d", &contacto[i].SD);
            fprintf(f, " %d", &contacto[i].SD);
            printf("Nota Final de ALGA:  ");
            fgets(geral, MAX_GERAL, stdin);
            sscanf(geral, " %d", &contacto[i].ALGA);
            fprintf(f, " %d", &contacto[i].ALGA);
            printf("Nota Final de CM:  ");
            fgets(geral, MAX_GERAL, stdin);
            sscanf(geral, " %d", &contacto[i].CM);
            fprintf(f, " %d\n", &contacto[i].CM);
            i++;
        }
        else
        {
            printf("a lista esta cheia");
        }
        substituir(contacto);
    }
    
    
    void f_eliminar(dados *contacto){
        char c;
        mostrar(contacto);
        int j, opcao;
    
    
        do{
            printf("Introduza numero a eliminar:\n");
            scanf("%d", &opcao);
            if (opcao >i || opcao <= 0)
                printf("Opcao Invalida\n");
        } while (opcao >i || opcao <= 0);
    
    
        getchar();
        do{
            printf("Deseja mesmo apagar o contacto? (s - Sim; n -Nao)");
            scanf("%c", &c);
        } while (c != 's' && c != 'n' && c != 'N' && c != 'S');
    
    
        if (c == 's' || c == 'S'){
            for (j = opcao; j<i; j++)
            {
                strcpy(contacto[j].nome, contacto[i - 1].nome);
                strcpy(contacto[j].numero, contacto[i - 1].numero);
                strcpy(contacto[j].morada, contacto[i - 1].morada);
                strcpy(contacto[j].postal, contacto[i - 1].postal);
                strcpy(contacto[j].email, contacto[i - 1].email);
                contacto[j].telefone = contacto[i - 1].telefone;
                contacto[j].FP = contacto[i - 1].FP;
                contacto[j].AM1 = contacto[i - 1].AM1;
                contacto[j].SD = contacto[i - 1].SD;
                contacto[j].ALGA = contacto[i - 1].ALGA;
                contacto[j].CM = contacto[i - 1].CM;
            }
            i--;
        }
        substituir(contacto);
    
    
    }
    void adicionar(dados* contacto)
    {
    
    
        if (i<total)
        {
            printf("\nIntroduza o Nome:  ", i + 1);
            fflush(stdin);
            scanf(" %[^\n]s", contacto[i].nome);
            printf("Introduza o Numero de Aluno ISEL:  ", i + 1);
            fflush(stdin);
            scanf(" %s", &contacto[i].numero);
            printf("Introduza a Morada:  ", i + 1);
            fflush(stdin);
            scanf(" %[^\n]s", contacto[i].morada);
            printf("Introduza o Codigo Postal:  ", i + 1);
            fflush(stdin);
            scanf(" %[^\n]s", contacto[i].postal);
            printf("Introduza o email:  ", i + 1);
            fflush(stdin);
            scanf(" %[^\n]s", contacto[i].email);
            printf("Introduza o telefone:  ", i + 1);
            fflush(stdin);
            scanf(" %d", &contacto[i].telefone);
            printf("Nota Final de FP:  ", i + 1);
            fflush(stdin);
            scanf(" %d", &contacto[i].FP);
            printf("Nota Final de AM1:  ", i + 1);
            fflush(stdin);
            scanf(" %d", &contacto[i].AM1);
            printf("Nota Final de SD:  ", i + 1);
            fflush(stdin);
            scanf(" %d", &contacto[i].SD);
            printf("Nota Final de ALGA:  ", i + 1);
            fflush(stdin);
            scanf(" %d", &contacto[i].ALGA);
            printf("Nota Final de CM:  ", i + 1);
            fflush(stdin);
            scanf(" %d\n\n\n", &contacto[i].CM);
            i++;
        }
        else
        {
            printf("a lista esta cheia");
        }
    
    
    }
    void eliminar(dados *contacto){
        char c;
        mostrar(contacto);
        int j, opcao;
    
    
        do{
            printf("Introduza numero a eliminar:\n");
            scanf("%d", &opcao);
            if (opcao >i || opcao <= 0)
                printf("Opcao Invalida\n");
        } while (opcao >i || opcao <= 0);
    
    
        getchar();
        do{
            printf("Deseja mesmo apagar o contacto? (s - Sim; n -Nao)");
            scanf("%c", &c);
        } while (c != 's' && c != 'n' && c != 'N' && c != 'S');
    
    
        if (c == 's' || c == 'S'){
            for (j = opcao; j<i; j++)
            {
                strcpy(contacto[j].nome, contacto[i - 1].nome);
                strcpy(contacto[j].numero, contacto[i - 1].numero);
                strcpy(contacto[j].morada, contacto[i - 1].morada);
                strcpy(contacto[j].postal, contacto[i - 1].postal);
                strcpy(contacto[j].email, contacto[i - 1].email);
                contacto[j].telefone = contacto[i - 1].telefone;
                contacto[j].FP = contacto[i - 1].FP;
                contacto[j].AM1 = contacto[i - 1].AM1;
                contacto[j].SD = contacto[i - 1].SD;
                contacto[j].ALGA = contacto[i - 1].ALGA;
                contacto[j].CM = contacto[i - 1].CM;
            }
            i--;
        }
    
    
    }
    
    
    void pesquisar1(dados* contacto)
    {
        char proc[50], geral[MAX_GERAL];
        int j;
    
    
        printf("Introduza o nome que pretende pesquisar: ");
        fgets(geral, MAX_GERAL, stdin);
        sscanf(geral, " %s", proc);
        getchar();
        for (j = 0; j<i; j++)
        {
            if (0 == strcmp(proc, contacto[j].nome))
            {
                system("cls");
                printf("\n\nDados: \n");
                printf("\tNome Pesquisado: %s", contacto[j].nome);
                printf("\n\tEmail: %s", contacto[j].email);
                printf("\n\tTelefone: %d", contacto[j].telefone);
                printf("\n----------------------------------\n");
                system("pause");
                return;
            }
        }
    
    
        printf("\nNome invalido\n");
        system("pause");
    }
    void pesquisar2(dados* contacto)
    {
        char proc[50];
        int j;
    
    
        printf("Introduza o nome que pretende pesquisar: ");
        scanf("%s", proc);
    
    
        for (j = 0; j<i; j++)
        {// isto diz o numero total de contactos         
            if (0 == strcmp(proc, contacto[j].nome))
            {
                system("cls");
                printf("\n\nDados: \n");
                printf("\n\tNome Pesquisado: %s", contacto[j].nome);
                printf("\n\tMorada: %s", contacto[j].morada);
                printf("\n\tCodigo Postal: %s", contacto[j].postal);
                printf("\n\tTelefone: %d", contacto[j].telefone);
                printf("\n----------------------------------\n");
                system("pause");
                return;
            }
        }
    
    
        printf("\nNome invalido\n");
        system("pause");
    }
    void pesquisar3(dados* contacto)
    {
        char proc[50];
        int j;
    
    
        printf("Introduza o numero de aluno ISEL que pretende pesquisar: ");
        scanf("%s", proc);
    
    
        for (j = 0; j<i; j++)
        {// isto diz o numero total de contactos         
            if (0 == strcmp(proc, contacto[j].numero))
            {
                system("cls");
                printf("\n\nDados: \n");
                printf("\n\tNumero Pesquisado: %s", contacto[j].numero);
                printf("\n\tNome: %s", contacto[j].nome);
                printf("\n\tEmail: %s", contacto[j].email);
                printf("\n\tTelefone: %d", contacto[j].telefone);
                printf("\n----------------------------------\n");
                printf("\n----------------------------------\n");
                system("pause");
                return;
            }
        }
    
    
        printf("\nNumero invalido\n");
        system("pause");
    }
    void pesquisar4(dados* contacto)
    {
        char proc[50];
        int j;
    
    
        printf("Introduza o numero de aluno ISEL que pretende pesquisar: ");
        scanf("%s", proc);
    
    
        for (j = 0; j<i; j++)
        {// isto diz o numero total de contactos         
            if (0 == strcmp(proc, contacto[j].numero))
            {
                system("cls");
                printf("\n\nDados: \n");
                printf("\n\tNumero Pesquisado: %s", contacto[j].numero);
                printf("\n\tNome: %s", contacto[j].nome);
                printf("\n\tMorada: %s", contacto[j].morada);
                printf("\n\tCodigo Postal: %s", contacto[j].postal);
                printf("\n\tTelefone: %d", contacto[j].telefone);
                printf("\n----------------------------------\n");
                system("pause");
                return;
            }
        }
    
    
        printf("\nNumero invalido\n");
        system("pause");
    }
    
    
    void submenu(dados *contacto)
    {
        char op;
    
    
        do
        {
            system("cls");
            titulo();
            printf("\n\tEscolha uma opcao\n\n\t");
            printf("1- Mostrar todos os registos da agenda\n\n\t");
            printf("2- Acrescentar um registo na agenda\n\n\t");
            printf("3- Apagar um registo da agenda\n\n\t");
            printf("4- Qual eh o telefone e o endereco\n\t de e-mail de um NOME de aluno que conste na agenda?\n\n\t");
            printf("5- Qual o telefone, a morada, e o\n\t codigo postal de um NOME de um aluno que conste na agenda?\n\n\t");
            printf("6- Qual o telefone e o endereco de\n\t e-mail de um NUMERO de um aluno que conste na agenda?\n\n\t");
            printf("7- Qual o telefone, a morada, e o\n\t codigo postal de um NUMERO de um aluno que conste na agenda?\n\n\t");
            printf("8- Ordenar a agenda por ordem crescente\n\t do numero de aluno, para todos os registos da agenda\n\n\t");
            printf("9- Voltar ao menu principal\n\n\t");
            printf("\n\tSelecione a opcao --> ");
            op = getchar();
            switch (op)
            {
            case '1':
                system("cls");
                mostrar(contacto);
                system("pause");
                break;
            case '2':
                system("cls");
                adicionar(contacto);
                system("pause");
                break;
            case '3':
                system("cls");
                eliminar(contacto);
                system("pause");
                break;
            case '4':
                system("cls");
                pesquisar1(contacto);
                system("pause");
                break;
            case '5':
                system("cls");
                pesquisar2(contacto);
                system("pause");
                break;
            case '6':
                system("cls");
                pesquisar3(contacto);
                system("pause");
                break;
            case '7':
                system("cls");
                pesquisar4(contacto);
                system("pause");
                break;
            case'8':
                system("cls");
                printf("\tEm desenvolvimento\n\n");//qSort;
                system("pause");
                break;
            case '9':
                system("cls");
                op = '0';
                break;
            }
        } while (op != '0');
    }
    void submenu2(dados *contacto)
    {
        char op;
    
    
        do
        {
            system("cls");
            titulo();
            printf("\n\tEscolha uma opcao\n\n\t");
            printf("1- Mostrar todos os registos da agenda\n\n\t");
            printf("2- Acrescentar um registo na agenda\n\n\t");
            printf("3- Apagar um registo da agenda\n\n\t");
            printf("4- Qual eh o telefone e o endereco\n\t de e-mail de um NOME de aluno que conste na agenda?\n\n\t");
            printf("5- Qual o telefone, a morada, e o\n\t codigo postal de um NOME de um aluno que conste na agenda?\n\n\t");
            printf("6- Qual o telefone e o endereco de\n\t e-mail de um NUMERO de um aluno que conste na agenda?\n\n\t");
            printf("7- Qual o telefone, a morada, e o\n\t codigo postal de um NUMERO de um aluno que conste na agenda?\n\n\t");
            printf("8- Ordenar a agenda por ordem crescente\n\t do numero de aluno, para todos os registos da agenda\n\n\t");
            printf("9- Voltar ao menu principal\n\n\t");
            printf("\n\tSelecione a opcao --> ");
            op = getchar();
            switch (op)
            {
            case '1':
                system("cls");
                mostrar(contacto);
                system("pause");
                break;
            case '2':
                system("cls");
                f_adicionar(contacto);
                system("pause");
                break;
            case '3':
                system("cls");
                eliminar(contacto);
                system("pause");
                break;
            case '4':
                system("cls");
                pesquisar1(contacto);
                system("pause");
                break;
            case '5':
                system("cls");
                pesquisar2(contacto);
                system("pause");
                break;
            case '6':
                system("cls");
                pesquisar3(contacto);
                system("pause");
                break;
            case '7':
                system("cls");
                pesquisar4(contacto);
                system("pause");
                break;
            case'8':
                system("cls");
                printf("\tEm desenvolvimento\n\n");//qSort;
                system("pause");
                break;
            case '9':
                system("cls");
                op = '0';
                libertar(contacto);
                break;
            }
        } while (op != '0');
    }
    void menu(dados *contacto)
    {
    
    
        char op;
    
    
    
    
        do
        {
            system("cls");
            titulo();
            printf("\n\n\tComo pretende introduzir os dados:\n\n\t\t");
            printf("1- Atraves do teclado\n\t\t");
            printf("2- Atraves de ficheiro\n\n\t\t");
            printf("3- Sair do Programa\n\n\t");
            printf("-->>> ");
            scanf("%c", &op);
            switch (op)
            {
            case'1':
                submenu(contacto);
                break;
            case'2':
                abrir(contacto);
                submenu2(contacto);
                break;
            case '3':
                sair();
                break;
    
    
            }
        } while (op != '0');
    
    
    
    
        system("PAUSE");
    
    
    }
    int main(int argc, char *argv[])
    {
        dados contacto[total];
        char op;
    
    
    
    
        do
        {
            system("cls"); 
            titulo();
            printf("-----------------------------------------------------------------");
            printf("\n\t-----------------------------------------------------------------");
            printf("\n\t|Este programa eh uma agenda eletronica.                          |\n\t|Permite introduzir varios tipos de informacao.                   |");
            printf("\n\t|Neste programa voce pode inserir o nome, numero de aluno do ISEL,|\n\t|morada, codigo postal, email, telefone                           |\n\t|e as notas finais das cinco disciplinas do primmeiro semestre    |\n\t|do curso de Eng. Electrotecnica no ISEL                          |\n\t");
            printf("------------------------------------------------------------------");
            printf("\n\t------------------------------------------------------------------");
            printf("\n\n\tPressione:\n\t\t1- Para entrar na agenda\n\n\t\t");
            printf("0- Sair do Programa\n\n\t");
            printf("-->>> ");
            scanf("%c", &op);
            switch (op)
            {
            case'1':
                menu(contacto);
                break;
    
    
            }
        } while (op != '0');
    
    
    
    
        system("PAUSE");
    
    
    
    
    }

  14. #44
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    First it looks like you need to increase your compiler warning levels. And if you are getting warnings fix them. These are the warnings I get from your code:
    main.c|24|warning: no previous declaration for ‘libertar’ [-Wmissing-declarations]|
    main.c||In function ‘libertar’:|
    main.c|24|warning: unused parameter ‘contacto’ [-Wunused-parameter]|
    main.c|31|warning: no previous declaration for ‘sair’ [-Wmissing-declarations]|
    main.c|50|warning: no previous declaration for ‘titulo’ [-Wmissing-declarations]|
    main.c|82|warning: no previous declaration for ‘substituir’ [-Wmissing-declarations]|
    main.c|132|warning: no previous declaration for ‘abrir’ [-Wmissing-declarations]|
    main.c|188|warning: no previous declaration for ‘mostrar’ [-Wmissing-declarations]|
    main.c|212|warning: no previous declaration for ‘f_adicionar’ [-Wmissing-declarations]|
    main.c||In function ‘f_adicionar’:|
    main.c|251|warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘int *’ [-Wformat=]|
    main.c|255|warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘int *’ [-Wformat=]|
    main.c|259|warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘int *’ [-Wformat=]|
    main.c|263|warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘int *’ [-Wformat=]|
    main.c|267|warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘int *’ [-Wformat=]|
    main.c|271|warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘int *’ [-Wformat=]|
    main.c|219|warning: unused variable ‘CM’ [-Wunused-variable]|
    main.c|219|warning: unused variable ‘ALGA’ [-Wunused-variable]|
    main.c|219|warning: unused variable ‘AM1’ [-Wunused-variable]|
    main.c|219|warning: unused variable ‘SD’ [-Wunused-variable]|
    main.c|219|warning: unused variable ‘FP’ [-Wunused-variable]|
    main.c|219|warning: unused variable ‘telefone’ [-Wunused-variable]|
    main.c|218|warning: unused variable ‘numero’ [-Wunused-variable]|
    main.c|218|warning: unused variable ‘postal’ [-Wunused-variable]|
    main.c|218|warning: unused variable ‘morada’ [-Wunused-variable]|
    main.c|218|warning: unused variable ‘email’ [-Wunused-variable]|
    main.c|218|warning: unused variable ‘nome’ [-Wunused-variable]|
    main.c|282|warning: no previous declaration for ‘f_eliminar’ [-Wmissing-declarations]|
    main.c|324|warning: no previous declaration for ‘adicionar’ [-Wmissing-declarations]|
    main.c||In function ‘adicionar’:|
    main.c|330|warning: too many arguments for format [-Wformat-extra-args]|
    main.c|333|warning: too many arguments for format [-Wformat-extra-args]|
    main.c|335|warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[10]’ [-Wformat=]|
    main.c|336|warning: too many arguments for format [-Wformat-extra-args]|
    main.c|339|warning: too many arguments for format [-Wformat-extra-args]|
    main.c|342|warning: too many arguments for format [-Wformat-extra-args]|
    main.c|345|warning: too many arguments for format [-Wformat-extra-args]|
    main.c|348|warning: too many arguments for format [-Wformat-extra-args]|
    main.c|351|warning: too many arguments for format [-Wformat-extra-args]|
    main.c|354|warning: too many arguments for format [-Wformat-extra-args]|
    main.c|357|warning: too many arguments for format [-Wformat-extra-args]|
    main.c|360|warning: too many arguments for format [-Wformat-extra-args]|
    main.c|372|warning: no previous declaration for ‘eliminar’ [-Wmissing-declarations]|
    main.c|415|warning: no previous declaration for ‘pesquisar1’ [-Wmissing-declarations]|
    main.c|444|warning: no previous declaration for ‘pesquisar2’ [-Wmissing-declarations]|
    main.c|474|warning: no previous declaration for ‘pesquisar3’ [-Wmissing-declarations]|
    main.c|505|warning: no previous declaration for ‘pesquisar4’ [-Wmissing-declarations]|
    main.c|538|warning: no previous declaration for ‘submenu’ [-Wmissing-declarations]|
    main.c||In function ‘submenu’:|
    main.c|559|warning: switch missing default case [-Wswitch-default]|
    main.c|608|warning: no previous declaration for ‘submenu2’ [-Wmissing-declarations]|
    main.c||In function ‘submenu2’:|
    main.c|629|warning: switch missing default case [-Wswitch-default]|
    main.c|679|warning: no previous declaration for ‘menu’ [-Wmissing-declarations]|
    main.c||In function ‘menu’:|
    main.c|698|warning: switch missing default case [-Wswitch-default]|
    main.c||In function ‘main’:|
    main.c|724|warning: variable length array ‘contacto’ is used [-Wvla]|
    main.c|744|warning: switch missing default case [-Wswitch-default]|
    main.c|722|warning: unused parameter ‘argc’ [-Wunused-parameter]|
    main.c|722|warning: unused parameter ‘argv’ [-Wunused-parameter]|
    main.c||In function ‘sair’:|
    main.c|49|warning: control reaches end of non-void function [-Wreturn-type]|
    ||=== Build finished: 0 errors, 55 warnings (0 minutes, 3 seconds) ===|
    As you can see 55 warnings.

    Next you have been informed about the use of fflush() with input streams, yet I see you're still using it all over the place. You need to remove all the fflush() calls.

    The same with the using "temporary" variables to retrieve your information, then using strcpy() to copy this information to the proper structure variables. You should be using the structure variables instead of the "temporary" variables. This will reduce the size of your functions and make your code much easier to understand.

    Next you should only need to be getting the information in two places at most. Use one function to get the data from the user and another function to get the data from the file. All these two funcitons really need to do is get the information for one "contact". If you want multiple contacts put a loop in the calling function. But write your functions to only get one contact at time.



    Jim

  15. #45
    Registered User
    Join Date
    Jun 2014
    Posts
    41
    i only have a few warnings(7)... i can you see all of those? any option on the compiler(visual studio 2013) to get that as well?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem reading file
    By coder_009 in forum C Programming
    Replies: 10
    Last Post: 01-15-2008, 01:22 PM
  2. problem with reading from file.
    By esi in forum C Programming
    Replies: 5
    Last Post: 06-23-2007, 02:50 PM
  3. Problem with reading strings
    By goron350 in forum C++ Programming
    Replies: 8
    Last Post: 06-05-2005, 12:05 AM
  4. File Reading Problem
    By Tarkad in forum C++ Programming
    Replies: 3
    Last Post: 03-30-2005, 10:49 AM
  5. Problem reading file into array of strings
    By myrddinb in forum C Programming
    Replies: 7
    Last Post: 03-26-2005, 12:12 AM