-3

I'm trying to write inside a file the whole content of an array. Here is my code:

   fichier = fopen(patch, "w+");

if (fichier != NULL)
{
        if(methode==1)
        {
            trieur(tableau, ttableau);
            int d;
            d=0;
            for (d = 0 ; d < ttableau ; d++)
            {
                fputs(tableau[d], fichier);
            }

        }
        else if(methode==2)
        {
            trieur2(tableau, ttableau);
            int d;
            d=0;
            for (d = 0 ; d < ttableau ; d++)
            {
                fputs(tableau[d], fichier);
            }

        }
        else
        {
        printf("Methode non disponible!\nBye Bye!");
        exit(0);
        }

    do
    {
        caractere = fgetc(fichier);
        printf("%c", caractere);
    } while (caractere != EOF);

    fclose(fichier);

}
else
{
    printf("Hum something wrong witch file %s", name);
}

But it isn't working. Can anyone help me?

Thanks!

ps: More script, all is working normally, but my script shutting down when he should write on the file :/

main => pastebin.com/m2AM0080 func.h => pastebin.com/KQkAbwin

insidelulz
  • 167
  • 1
  • 2
  • 11
  • 2
    Please, show the declaration of `tableau` and `fichier`. **EDIT:** and `ttableau`. – Math Jul 15 '13 at 11:27
  • `fputs` puts a STRING. `int fputs (const char * str, FILE * stream);`. Did you mean to convert the numbers to strings and then store them into a file? – dda Jul 15 '13 at 11:27
  • 1
    A little bit more information, please: what is tableau, ttableau etc.? What exaclty do you mean by ''it isn't working''? – Ingo Leonhardt Jul 15 '13 at 11:28
  • Here is the whole script: main => pastebin.com/m2AM0080 func.h => pastebin.com/KQkAbwin ttableau mean array size (number of case in the array) and tableau is the name of the array. – insidelulz Jul 15 '13 at 11:42

1 Answers1

0

Following your link to the complete code, you have int tableau[]. So ypu pass an int to fputs() but fputs() expects a char *. That cannot work. To write the int binarily to the output file you could use

fwrite( tableau+d, sizeof(int), 1, fichier );

To write a number of ASCII digits you could use

fprintf( fichier, "%d", tableau[d] );

Btw. it's really easier if you include declarations of the variables you use in your post directly.

Ingo Leonhardt
  • 9,435
  • 2
  • 24
  • 33