Thread: Passe back Struct from Void function

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    15

    Passe back Struct from Void function

    Hi!

    I have this code and inside the fuction print the correct texto and numbers. Outside the function or in the main no print texto and just print a 2 big numbers.

    Inside print "500 dados novos 1000"

    Otside print: "2686672 2686728" and no print the texto

    Someone help me please?

    Thanks

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <windows.h>
    
    struct dados
    {
        int largura;
        int velocidade;
        char texto[50];
    };
    
    void escreve(struct dados  val);
    void ler(struct dados val);
    
    int main()
    {
    
       struct dados entdados;
       
      	ler(entdados);
        printf("%s\n",entdados.texto);
        printf("%d\n",entdados.largura);
        printf("%d\n",entdados.velocidade);
    
        getchar();
        getchar();
    
        return 0;
    }
    
    
    
    
    void ler(struct dados  val)
    {
    
       FILE* fin = fopen("c:\\castanha1.txt", "r");
       fread(&val, sizeof(val), 1, fin);
       fclose(fin);
         printf("%d %s %d\n", val.largura, val.texto, val.velocidade);
       return (void) val;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,799
    You need to pass a pointer to your struct.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    15
    Quote Originally Posted by Salem View Post
    You need to pass a pointer to your struct.
    Can you help me how do this?

    Please

    Thanks

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,191
    Code:
    #include <stdio.h>
    #include <stdlib.h>
     
    struct dados {
        int largura;
        int velocidade;
        char texto[50];
    };
     
    void ler(struct dados *val);
     
    int main() {
        struct dados entdados;
        ler(&entdados);  // pass the address of the object
        printf("%s\n",entdados.texto);
        printf("%d\n",entdados.largura);
        printf("%d\n",entdados.velocidade);
        return 0;
    }
    
    void ler(struct dados *val) { // receive as a pointer
        FILE* fin = fopen("c:\\castanha1.txt", "rb"); // open in binary mode
        if (fin == NULL) {
            perror("fopen");
            exit(EXIT_FAILURE);
        }
        fread(val, sizeof(*val), 1, fin);
        fclose(fin);
        printf("%d %s %d\n", val->largura, val->texto, val->velocidade);
    }
    If the file really is a text file, i.e., if it's contents look something like this (in a text editor) :
    Code:
    1234
    56
    hello there
    then don't open it in binary mode and you'll need to read it with fscanf instead of fread:
    Code:
    fscanf(fin, "%d %d %[^\n]", &val->largura, &val->velocidade, val->texto);
    Last edited by john.c; 12-23-2017 at 06:08 PM.
    We live as it were by chance, and by chance we are governed. - Seneca

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,799
    Quote Originally Posted by totoco View Post
    Can you help me how do this?

    Please

    Thanks
    You declare this
    void ler(struct dados *val)

    You use an & when you call it.

    You use -> instead of . to access members inside your function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    15
    That whork fine like i need.

    with your help, I understand the pointers and I will study them better.

    Thankyou for all.

  7. #7
    Registered User
    Join Date
    Dec 2017
    Posts
    15
    Hi again!

    this code if i rebot the PC work, if i press F9 again does not work.

    Someone can help me plese?

    Code:
    #include <stdio.h>
    #include <string.h>
    struct dados
    {
        int largura;
        int velocidade;
        char texto[50];
    };
    void escreve(struct dados  val);
    void ler(struct dados *val);
    int main()
    {
        struct dados entdados;
        scanf("%[^\n]%*c", entdados.texto);
        entdados.largura=10;
        entdados.velocidade=20;
    printf("%d %s %d\n", entdados.largura, entdados.texto, entdados.velocidade);
    
      //Write text in the file
       escreve(entdados);
    
    
        //read text in the file
        ler(&entdados);
    
    
        printf("%s\n",entdados.texto);
        printf("%d\n",entdados.largura);
        printf("%d\n",entdados.velocidade);
        return 0;
    };
    
    void ler(struct dados *val)   // receive as a pointer
    {
        FILE* fin = fopen("c:\\castanha1.txt", "r"); // open in binary mode
        if (!fin)
        {
            printf("Unable to open file!");
            return 1;
        }
        fread(&val, sizeof(*val), 1, fin);
        fclose(fin);
        printf("%d %s %d\n", val->largura, val->texto, val->velocidade);
    }
    
    
    
    void escreve(struct dados  val)
    {
        FILE *ficheiro=fopen("c:\\castanha1.txt","w");
        if (!ficheiro)
        {
            printf("Unable to open file!");
            return 1;
        }
        fwrite(&val, sizeof(val), 1, ficheiro);
        fclose(ficheiro);
    }

    Content of the file after recording:
    Passe back Struct from Void function-erro-jpg

    Error after read: (Scanf plus first printf. The print after read does not appear )
    Passe back Struct from Void function-erro1-png

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    Look carefully at the lines where you call fread() in the function ler(), and where you call fwrite() in the function escreve().

    john.c gives an example of calling fread() in his post:
    Code:
        fread(val, sizeof(*val), 1, fin);
    Do you see where his call is different? val is already a pointer, so you don't need the &. Since fread() and fwrite() have similar arguments, you don't need to use & in the other function either.

  9. #9
    Registered User
    Join Date
    Dec 2017
    Posts
    15
    Quote Originally Posted by whiteflags View Post
    Look carefully at the lines where you call fread() in the function ler(), and where you call fwrite() in the function escreve().

    john.c gives an example of calling fread() in his post:
    Code:
        fread(val, sizeof(*val), 1, fin);
    Do you see where his call is different? val is already a pointer, so you don't need the &. Since fread() and fwrite() have similar arguments, you don't need to use & in the other function either.
    Ho Ho!...

    It was confusion on my parts.

    I was tested and it work fine.

    Merry Christmas for all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP: Returning struct data back into a file?
    By ntomaski in forum C Programming
    Replies: 4
    Last Post: 04-17-2017, 04:49 PM
  2. changing void * value in struct
    By amahmoo in forum C Programming
    Replies: 33
    Last Post: 11-24-2015, 06:49 PM
  3. difference between void and static void function
    By mahaju in forum C++ Programming
    Replies: 7
    Last Post: 12-27-2011, 04:02 AM
  4. Passing a variable in void to another void function
    By stevedawg85 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 06:17 PM
  5. Returning a Tree node back to void main()
    By gazza in forum C++ Programming
    Replies: 2
    Last Post: 07-07-2002, 02:48 AM

Tags for this Thread