Pages: 1 [2]   Go Down
Author Topic: auto reset during the loop()  (Read 1718 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am giving the code because there are errors:

Code:
int rechercheUtilisateur(char numUtilisateur[TAILLE_NUM_UTI]){
    int i, ligne = 1;
    //declaration des tableaux locaux   
    char numString[TAILLE_NUM_UTI];

    //place le pointeur au debut du fichier
    myFile.seek(0);
    //recupére le numéro d'identifiant de la ligne
    for(i=0; i<TAILLE_NUM_UTI-1; i++) numString[i]=myFile.read(); 
    numString[i]='\0';

   
    while(strcmp(numUtilisateur, numString)){
        if(!sautDeLigne()){
          myFile.seek(0);
          return 0;
        }
     
        ligne++;
        //met dans 'numString' le numéro d'identifiant de la ligne
        for(i=0; i<TAILLE_NUM_UTI-1; i++){
          numString[i]=myFile.read();
        }
        numString[i]='\0';
    }
    //place le pointeur au debut de la ligne
    myFile.seek(myFile.position()-TAILLE_NUM_UTI);
    return ligne;
}

TAILLE_NUM_UTI is defined in the beginning of the code:
Code:
#define TAILLE_NUM_UTI 8+1

Do you want the entire program?
Logged

Switzerland
Offline Offline
Faraday Member
**
Karma: 108
Posts: 5144
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, please give us the whole program and also tell us what's in the file you're reading in.
Logged

Germany
Offline Offline
Faraday Member
**
Karma: 56
Posts: 2983
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

BTW:
Code:
#define TAILLE_NUM_UTI 8+1
is dangererous:

int x = TAILLE_NUM*4; // x = 12 !!!

better say
#define TAILLE_NUM_UTI (8+1) 
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    while(strcmp(numUtilisateur, numString)){
strcmp() returns 0 if the strings match, or -1 or +1 if they don't match, depending on whether string 1 comes before or after string 2. It does NOT return a boolean, so this code should not be written as though it does. Explicitly state what you are expecting.

Code:
    for(i=0; i<TAILLE_NUM_UTI-1; i++) numString[i]=myFile.read(); 
Suppose you read past the end of the file. Then what happens?

Some Serial.print()s in this code will tell you where you are going wrong. Why are there none?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 5
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Some Serial.print()s in this code will tell you where you are going wrong. Why are there none?
I've watched with Serial.println () but that the reset occurs at different times if I change the code.

Code:
    for(i=0; i<TAILLE_NUM_UTI-1; i++) numString[i]=myFile.read(); 
Suppose you read past the end of the file. Then what happens?
I normally can not to be at the end of the file, I read the first 8 characters of a great line.
Logged

Canada
Offline Offline
Full Member
***
Karma: 0
Posts: 246
Code Monkey
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Code:
   for(i=0; i<TAILLE_NUM_UTI-1; i++) numString[i]=myFile.read();  
Suppose you read past the end of the file. Then what happens?
I normally can not to be at the end of the file, I read the first 8 characters of a great line.
What if your file contains less than 8 bytes?

You really ought to be checking for unexpected results from read().
« Last Edit: May 14, 2012, 11:55:41 am by The Clever Monkey » Logged

I yield() for co-routines.

Pages: 1 [2]   Go Up
Jump to: