IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

 C Discussion :

Un problème de boucle


Sujet :

C

  1. #1
    Membre confirmé
    Homme Profil pro
    Étudiant à l'école 42
    Inscrit en
    Avril 2013
    Messages
    59
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 30
    Localisation : France, Seine et Marne (Île de France)

    Informations professionnelles :
    Activité : Étudiant à l'école 42

    Informations forums :
    Inscription : Avril 2013
    Messages : 59
    Par défaut Un problème de boucle
    Bonjour,

    Tout d'abord voici mon code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
    int main()
    {
        FILE* fichier = NULL;
        fichier = fopen("fichier.txt", "w+");
        int i = 0;
        int j = 0;
        int k = 0;
        int l = 0;
        int m = 0;
        long size;
        char alphabet[] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','0','1','2','3','4','5','6','7','8','9','\0'};
        char retour = '\n';
        int fplus = 0;
        char tmp[500];
     
        while (i<36) // Alphabet 1
        {
            fputc(alphabet[i], fichier);
            fputc(retour, fichier);
            i++;
            fseek(fichier, 0, SEEK_END);
            size = ftell(fichier);
            if(size>=104857600)
            {
                    fplus++;
                    sprintf(tmp, "fichier%d.txt", fplus);
                    fichier = fopen(tmp, "w+");
            }
        }
        fclose(fichier);
        fichier = fopen("fichier.txt", "w");
        i = 0;
     
        while (j<36) // Alphabet 2
        {
            fputc(alphabet[i], fichier);
            fputc(alphabet[j], fichier);
            fputc(retour, fichier);
            j++;
            if(j==36)
            {
                i++;
                j = 0;
            }
            if(i==36)
            {
                break;
            }
            fseek(fichier, 0, SEEK_END);
            size = ftell(fichier);
            if(size>=104857600)
            {
                    fplus++;
                    sprintf(tmp, "fichier%d.txt", fplus);
                    fichier = fopen(tmp, "w+");
            }
        }
        fclose(fichier);
        fichier = fopen("fichier.txt", "w");
        i=0;
        j=0;
     
        while (k<36) // Alphabet 3
        {
            fputc(alphabet[i], fichier);
            fputc(alphabet[j], fichier);
            fputc(alphabet[k], fichier);
            fputc(retour, fichier);
            k++;
            if(k==36)
            {
                j++;
                k = 0;
            }
            if(j==36)
            {
                i++;
                j = 0;
            }
            if(i==36)
            {
                break;
            }
            fseek(fichier, 0, SEEK_END);
            size = ftell(fichier);
            if(size>=104857600)
            {
                    fplus++;
                    sprintf(tmp, "fichier%d.txt", fplus);
                    fichier = fopen(tmp, "w+");
            }
        }
        fclose(fichier);
        fichier = fopen("fichier.txt", "w");
        i=0;
        j=0;
        k=0;
     
        while(l<36) // Alphabet 4
        {
            fputc(alphabet[i], fichier);
            fputc(alphabet[j], fichier);
            fputc(alphabet[k], fichier);
            fputc(alphabet[l], fichier);
            fputc(retour, fichier);
            j++;
            if(l==36)
            {
                k++;
                l = 0;
            }
            if(k==36)
            {
                j++;
                k = 0;
            }
            if(j==36)
            {
                i++;
                j = 0;
            }
            if(i==36)
            {
                break;
            }
            fseek(fichier, 0, SEEK_END);
            size = ftell(fichier);
            if(size>=104857600)
            {
                    fplus++;
                    sprintf(tmp, "fichier%d.txt", fplus);
                    fichier = fopen(tmp, "w+");
            }
        }
        fclose(fichier);
        fichier = fopen("fichier.txt", "w");
        i=0;
        j=0;
        k=0;
        l=0;
        while(m<36) // Alphabet 5
        {
            fputc(alphabet[i], fichier);
            fputc(alphabet[j], fichier);
            fputc(alphabet[k], fichier);
            fputc(alphabet[l], fichier);
            fputc(alphabet[m], fichier);
            fputc(retour, fichier);
            m++;
            if(m==36)
            {
                l++;
                m = 0;
            }
            if(l==36)
            {
                k++;
                l = 0;
            }
            if(k==36)
            {
                j++;
                k = 0;
            }
            if(j==36)
            {
                i++;
                j = 0;
            }
            if(i==36)
            {
                break;
            }
            fseek(fichier, 0, SEEK_END);
            size = ftell(fichier);
            if(size>=104857600)
            {
                    fplus++;
                    sprintf(tmp, "fichier%d.txt", fplus);
                    fichier = fopen(tmp, "w+");
            }
        }
        fclose(fichier);
        return 0;
    }
    Celui ci est censé écrire toutes les combinaisons de alphabet avec 1 caractères, puis 2 caractères, etc jusqu'à 5 caractères.

    Soit :

    A
    B
    C
    D
    ... Jusqu'à : 99999

    Cependant dans mon fichier texte il m'écrit uniquement les chaînes de 5 caractères (AAAAA, AAAAB, etc jusquà 99999), il ignore donc mes 4 premières boucles et passe directement à la 5e...

    Quelqu'un saurait pourquoi il ignore mes 4 premières boucles svp ?

  2. #2
    Membre averti
    Homme Profil pro
    Étudiant
    Inscrit en
    Février 2013
    Messages
    18
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Février 2013
    Messages : 18
    Par défaut
    Citation Envoyé par Iron-Avenger Voir le message
    Bonjour,

    Tout d'abord voici mon code :
    ...

    Quelqu'un saurait pourquoi il ignore mes 4 premières boucles svp ?
    Bonjour.

    Pourquoi close ton fichier si tu l'open après ? Par défaut, quand tu ouvres un fichier, le curseur est placé au début, il écrase donc tout ce que tu as marqué précédemment.
    Deux solutions : soit tu ne closes juste à la fin, soit tu utilises la fonction lseek (fseek) pour te déplacer dans le fichier.

  3. #3
    Membre confirmé
    Homme Profil pro
    Étudiant à l'école 42
    Inscrit en
    Avril 2013
    Messages
    59
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 30
    Localisation : France, Seine et Marne (Île de France)

    Informations professionnelles :
    Activité : Étudiant à l'école 42

    Informations forums :
    Inscription : Avril 2013
    Messages : 59
    Par défaut
    En effet, je ne sais pas pourquoi j'ai ajouté sa...

    Problème résolu merci beaucoup

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Problème de boucles imbriquées
    Par Gnux dans le forum Algorithmes et structures de données
    Réponses: 9
    Dernier message: 09/12/2005, 20h26
  2. [Tableaux] Problème avec boucle
    Par MYster dans le forum Langage
    Réponses: 6
    Dernier message: 11/11/2005, 18h39
  3. Problème de boucle
    Par TheUltimaSephiroth dans le forum C
    Réponses: 8
    Dernier message: 10/10/2005, 13h58
  4. Problème de boucle
    Par Louis-Guillaume Morand dans le forum Langage SQL
    Réponses: 3
    Dernier message: 25/09/2005, 09h10
  5. Problème de boucle
    Par basclln dans le forum C++
    Réponses: 19
    Dernier message: 02/04/2005, 09h13

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo