Thread: write structe to a file and then randomize func

  1. #16
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    Hi

    I just try to do like this, like arrays

    Code:
    void jg2(struct perg *p)
    {
        char lista[1000][256]; // mil perguntas com tamanho máximo de 255 caracteres
        int count;
        int n, i;
        FILE *f;
        if ((f = fopen("Atividades/gp1.dat", "wb")) == NULL)
        {
            printf("Erro ao abrir ficheiro\n");
        }
        else
        {
            fread(p, sizeof *p, 1, f);
            count = 0;
            while (fgets(lista[i], 256, f) == lista[i])
            count++;
            // fechar ficheiro
            fclose(f);
            //escolher pergunta
            n= rand() % count;
        }
        printf("Bemvindo ao jogo curto do BUZZ, boa sorte \n");
    }
    Using the char lista to hold questions from text

    Then i open my file (gp1.dat)

    Read it
    fgets to get the text to the array..

    Variable i unused.
    Last edited by Gil Carvalho; 05-18-2012 at 03:54 PM.

  2. #17
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    wow, gonna need some work.

    Does *p point to an array?
    You should probably also pass the max elements of the array as well.
    You could also use p[xxx].

    Next, you read in one chunk the size of perg to the pointer.
    Then you go into a loop that reads lines.
    Don't you want to continue to read chunks -o- perg into the array?

    There is more wrong.
    Start over and write a pseudocode outline which includes a workable calling function.

  3. #18
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by Gil Carvalho View Post
    Code:
        if ((f = fopen("Atividades/gp1.dat", "wb")) == NULL)
    You won't read anything from that file because opening it for write truncates the file. You'll want to use "rb" to open it for reading.

  4. #19
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    Hi

    Code:
    char lista[1000][256];
    .....
    while (fgets(lista[i], 256, f) == lista[i])
    The erro comes in the while

    Warning comes with the char

    Howard, thanks again


    My steps:

    create a variable to read how many records are in the file
    create an array
    Read file into the array
    count the records

    call (random) to choose x records

    regards

  5. #20
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    If the structs in the file are stored in binary your fgets(lista[i], 256, f) == lista[i] idea may not work out so well.
    But forget that for now. Work on your pseudo code.
    Code:
    create a variable to read how many records are in the file
    create an array
    Read file into the array
    count the records
    call (random) to choose x records
    Ok so put those in function blocks where they will be declared and occur. ie: main(), F1(), F2() ...
    INDENT your code

  6. #21
    Registered User
    Join Date
    Apr 2012
    Posts
    159
    Quote Originally Posted by HowardL View Post
    If the structs in the file are stored in binary your fgets(lista[i], 256, f) == lista[i] idea may not work out so well.
    But forget that for now. Work on your pseudo code.
    Code:
    create a variable to read how many records are in the file
    create an array
    Read file into the array
    count the records
    call (random) to choose x records
    Ok so put those in function blocks where they will be declared and occur. ie: main(), F1(), F2() ...
    INDENT your code
    pseudocode:

    Code:
    Variables
    cont, rand, i
    char lista[1000][256];
    open file
    cont = 0
    while (fgets lista == struct *p)
    cont++
    I know how many (cont)
    close file
    rand = random()%count
    is that??

    Howard, and how about dinamic memeory allocation?? Do you think it's easier or not?

    Many thanks

  7. #22
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    No offense but that is ridiculous. You did not even address the binary file data issue.
    Make that look just like a complete program... main() and required functions.
    Your wasting time and space.
    Last edited by HowardL; 05-19-2012 at 08:00 AM.

  8. #23
    Registered User
    Join Date
    Apr 2012
    Posts
    159

    Talking

    Thanks Howard, i'm learnig that language and sometimes i think something can be the way i think and is not, i have modified my code to:

    Code:
    void jg2(struct perg *p)
    {
    
        char * lista[1000][256]; // mil perguntas com tamanho máximo de 255 caracteres
        int count;
        FILE *f;
        if ((f = fopen("Atividades/gp1.dat", "rb")) == NULL)
        {
            printf("Erro ao abrir ficheiro\n");
        }
        else
        {
            count = 0;
            while (fgets(lista[count], 256, f) == lista[count])
            count++;
            // fechar ficheiro
            fclose(f);
            //escolher pergunta
            rand() % count;
        }
    }
    I have no errors now but two warnings

    (while)
    comparison of distinct pointer types lacks a cast [enabled by default]
    passing argument 1 of ‘fgets’ from incompatible pointer type

    I'm creating variables,
    array
    open my file
    put the variable count to 0
    get the contents of my file to lista
    closing the file
    random the count

    Still ridiculous? Hope no....i'm struggle to do better thanks to you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-08-2010, 02:43 PM
  2. Replies: 2
    Last Post: 03-04-2010, 04:19 AM
  3. File Handling -Read write and edit a file
    By aprop in forum C Programming
    Replies: 3
    Last Post: 02-27-2010, 02: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, 11:19 PM