Like Tree2Likes

write structe to a file and then randomize func

This is a discussion on write structe to a file and then randomize func within the C Programming forums, part of the General Programming Boards category; Hi i have a struct called perg writed in a file I want to create a function to randomize 2 ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    154

    write structe to a file and then randomize func

    Hi

    i have a struct called perg writed in a file

    I want to create a function to randomize 2 questions from that file.

    the stuct has

    id
    ques
    op1
    op2
    op3
    op4
    res.

    i started like this
    Code:
    void randomize()
    {
    struct perg p;
    int random, n;
    fp=fopen(fopen("Questions.txt","rb");
    n=(fread(&p,sizeof(p),1,fp)==1){
    srand(time(NULL)); 
    random = ????
    }
    Do i need a loop??
    I'm lost, i just want the ques and op1,2,3,4 to be printed

  2. #2
    Registered User
    Join Date
    Apr 2012
    Posts
    154
    None knows??

  3. #3
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    It looks like you are way ahead of yourself.
    Why don't you first learn to write the struct to a file and then read it back to the program.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,301
    Take that srand out of there and move it to the top of your main function.
    To generate a sequence of random numbers, it should only be called once in the program.

    Most things that deal with more than one of something tend to involve a loop. However I don't think you're clear on what you want to do, because you haven't explained it clearly to us.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    154
    Quote Originally Posted by HowardL View Post
    It looks like you are way ahead of yourself.
    Why don't you first learn to write the struct to a file and then read it back to the program.
    Already did this mate;
    Now i just want to random some questions...not to hapeard all of them...

    regards

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,467
    Indeed, you need to explain what you mean by "randomize 2 questions from that file".

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    154
    Hi

    I have my file with questions, options and correct answer (gp1.dat)

    When an user choose that option in my menu, the printf will print id, question an options, the user write the correct answer, if the answer is equal to my correct answer ...printf "correct".
    I have 50 questions, but i just want two of them...i know i must implement a random..but don't know how.


    any help?

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,467
    Programming is not really about writing code. It's about problem solving. The first step to solving a problem is understanding that problem. What data do you have? What data are you asked to find/calculate? What tasks are you asked to perform? Once you know what to do, you should solve your problem on paper. If you don't understand the process, or can't solve it yourself, you can never program a computer to do it.

    How would you do this if, instead of a file, you had the questions written down in a book. Let's say you had a book full of questions, and you wanted to pick two random questions. What numbers would you pick? Well, it depends on how many questions you have, right? So lets say there are 500 questions, and you decide you want to pick questions #37 and #242. How would you locate those questions?
    rags_to_riches and Salem like this.

  9. #9
    Registered User
    Join Date
    Apr 2012
    Posts
    154
    Quote Originally Posted by anduril462 View Post

    How would you do this if, instead of a file, you had the questions written down in a book. Let's say you had a book full of questions, and you wanted to pick two random questions. What numbers would you pick? Well, it depends on how many questions you have, right? So lets say there are 500 questions, and you decide you want to pick questions #37 and #242. How would you locate those questions?
    I know how to do it with an array, but now i'm talking about all structure writed in a file.

    Code:
    struct perg
    {
    int id;
    char ques [100];
    char op1 [15];
    char op2 [15];
    char op3 [15];
    char op4 [15];
    int res;
    }
    So my file is writed in binary mode, but each id is unique, when i choose the function see questions, i can see that.

    Code:
    void ver_perguntas(struct perg *p)
    {
        int retorno, cont = 0;
        FILE *f;
        if ((f= fopen("Atividades/gp1.dat", "rb")) == NULL)
        {
            printf("Erro ao abrir ficheiro\n");
        }
        retorno = fread(p, sizeof *p, 1, f);
        while ( retorno == 1)
        {
            cont++;
            printf("\nId : %d \n", p->id);
            printf("\nPergunta : %s", p->ques);
            printf("\nOpcao 1 : %s",p->op1);
            printf("\nOpcao 2 : %s",p->op2);
            printf("\nOpcao 3 : %s",p->op3);
            printf("\nOpcao 4 : %s",p->op4);
            printf("\nResposta Correta : %d",p->res);
            printf("\n");
            retorno= fread(p, sizeof *p, 1, f);
        }
        printf(" \n\n %d perguntas registadas \n", cont);
        fclose(f);
    }
    So what i pretend now is to randomize all my id in that file and give me 2 questions.

    step by step

    function x()
    open the file
    read the file
    rand id
    print some variables.

    is not that?

    regards

  10. #10
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,650
    Quote Originally Posted by Gil Carvalho View Post
    None knows??
    You might consider that people are getting wary of answering you, because every thread you open turns into a marathon spoon-feeding session.

    FAQ > Generate random numbers? - Cprogramming.com

  11. #11
    Registered User
    Join Date
    Apr 2012
    Posts
    154
    Wake up my friend, that post has 16 hours now.
    I just ask, who wants, help, who doesn't want, like you, just simply have to ignore.
    If you read my firts post i was talking about random 2 questions from a file writed froma structure....not random numbers.
    Anyway thanks for your "help"

    regards

  12. #12
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    Is it the op's you want to select from?
    How many total do you have , 4?
    Well rand() a value in that range.
    Associate the value to a specific op like:
    Code:
      if (x == 1) {
        printf("%s \n",  p->op1);
      }
    Or you could have the op's in an array and have the rand value indicate the index.

  13. #13
    Registered User
    Join Date
    Apr 2012
    Posts
    154
    Quote Originally Posted by HowardL View Post
    Is it the op's you want to select from?
    How many total do you have , 4?
    Well rand() a value in that range.
    Associate the value to a specific op like:
    Code:
      if (x == 1) {
        printf("%s \n",  p->op1);
      }
    Or you could have the op's in an array and have the rand value indicate the index.
    Hi Howard
    No i want to select from the id.

    The op(s) is the options the user have to respond.

    then the "rest" variable will compare with the input from user.
    like this
    Code:
    if(p->id==user)
    printf("Correc")
    else
    printf("Not correct...you will leave the game");
    return;
    Do you understand what i pretend?

    Thanks

  14. #14
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,467
    @Gil
    You say you can do this with an array, so why should it be much different with a file? Your file is stored sequentially like your array. The only real difference is how you look up the 3rd, 12th, 100th, etc question from the list. You can't just use the [ ] operator, you have to use file operations (functions).

  15. #15
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    No i want to select from the id.
    But the id is a single int.
    What happened to the "questions"?

    So there are many struct perg's in the file?
    anduril462's idea sounds great.

    If you need just id's then just read id's into an array.
    Then you can rand() for a value in the range of that total.
    If you need to refer back to the id data just multiply index by sizeof(perg).

    Heck you could just determine how many structs are in the file, do rand() on that and then seek those id's as needed.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-08-2010, 01:43 PM
  2. Replies: 2
    Last Post: 03-04-2010, 03:19 AM
  3. File Handling -Read write and edit a file
    By aprop in forum C Programming
    Replies: 3
    Last Post: 02-27-2010, 01:01 PM
  4. Load BMP from file func
    By Death_Wraith in forum C++ Programming
    Replies: 2
    Last Post: 09-20-2004, 06:46 PM
  5. how to add save file func into this code!!
    By sam3291 in forum C++ Programming
    Replies: 5
    Last Post: 04-03-2002, 10:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21