Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C/C++ - ERRORE NEL PROGRAMMA
Forum - C/C++ - ERRORE NEL PROGRAMMA

Avatar
gighen991 (Normal User)
Newbie


Messaggi: 20
Iscritto: 27/04/2009

Segnala al moderatore
Postato alle 11:22
Mercoledì, 23/12/2009
Salve a tutti.. è due giorni che cerco di capire come mai mi si visualizzino questi errori nel codice del programma. Il mio sistema operativo è ubuntu 9.10 e quando compilo il programma mi da questi errori

Codice sorgente - presumibilmente Delphi

  1. hackerj@hackerj-laptop:~$ gcc -o notesearch notesearch.c
  2. notesearch.c:78: error: expected ‘)’ before ‘*’ token
  3. notesearch.c:79: error: expected declaration specifiers or...’ before string constant
  4. notesearch.c:79: error: expected declaration specifiers or...’ before ‘length
  5. notesearch.c:79: error: expected declaration specifiers or...’ before ‘note_uid’
  6. notesearch.c:79: warning: data definition has no type or storage class
  7. notesearch.c:79: error: conflicting types for ‘printf’
  8. notesearch.c:79: note: a parameter list with an ellipsis can’t match an empty parameter name list declaration
  9. notesearch.c:80: error: expected identifier or(’ before ‘return’
  10. notesearch.c:81: error: expected identifier or(’ before ‘}’ token



il codice del programma è il seguente

Codice sorgente - presumibilmente C++

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<fcntl.h>
  4. #include<sys/stat.h>
  5. #include "hacking.h"
  6.  
  7.  
  8. #define FILENAME "/var/notes"
  9.  
  10.  
  11. int print_notes(int, int, char *);
  12. int find_user_note(int, int);
  13. int search_note(char *, char *);
  14. void fatal(char *);
  15.  
  16.  
  17. int main(int argc, char *argv[]) {
  18.     int userid, printing=1, fd;
  19.     char searchstring[100];
  20. if(argc > 1)                               // se esiste un arg,
  21.     strcpy(searchstring, argv[1]);         // quella è la stringa di ricerca;
  22. else
  23.     searchstring[0] = 0;                    // la stringa di ricerca è vuota.
  24.  
  25. userid = getuid();
  26. fd = open(FILENAME, O_RDONLY);              // apre il file in sola lettura
  27. if( fd == -1)
  28.    fatal("in main() while opening file for reading");
  29.  
  30. while(printing)
  31.    printing = print_notes(fd, userid, searchstring);
  32.  printf("-------[end of note data ]--------\n");
  33.  close(fd);
  34. }
  35.  
  36. // Stampa le note per un determinato id utentecorrispondenti a una
  37. // Stringa opzionale
  38. // Restituisce 0 alla fine del file o 1 se ci sono altre note;
  39. int print_notes(int fd, int uid, char *searchstring){
  40.    int note_length;
  41.    char byte=0;
  42.    char note_buffer[100];
  43.  
  44.    note_length = find_user_note(fd, uid);
  45.    if(note_length == -1)
  46.       return 0;
  47.  
  48.      read(fd, note_buffer, note_length);
  49.      note_buffer[note_length] = 0;
  50.  
  51.    if(search_note(note_buffer, searchstring))
  52.       printf("%s", note_buffer);
  53.       return 1;
  54. }
  55.  
  56. // Trova la nota successiva per un determinato id utente;
  57. // Restituisce -1 alla fine del file;
  58. // Altrimenti restituisce la lunghezza del file
  59. int find_user_note(int fd, int user_uid) {
  60.     int note_uid = -1;
  61.     unsigned char byte;
  62.     int length;
  63.  
  64.     while(note_uid != user_uid) {
  65.        if(read(fd, &note_uid, 4) != 4)
  66.            return -1;
  67.        if(read(fd, &byte, 1) != 1)
  68.            return -1;
  69.  
  70.        byte = length = 0;
  71.        while(byte != '\n')
  72.          if(read(fd, &byte, 1) != 1)
  73.            return -1;
  74.          length++;
  75.     }
  76. }
  77.  
  78.    lseek(fd, length * -1, SEEK_CUR); // Riporta la lettura del file indietro di length byte.
  79.    printf("[DEBUG] found a %d byte note for user id %d\n", length, note_uid);
  80.    return length;
  81. }
  82.  
  83. // Ricerca di nota in base a una parola chiave, 1 in caso di successo e 0 in caso contrario
  84.  
  85. int search_note(char *note, char *keyword) {
  86.     int i, keyword_length, match=0;
  87.  
  88.     keyword_length=strlen(keyword);
  89.   if(keyword_length == 0)
  90.     return 1;
  91.  
  92. for(i=0;  i< strlen(note); i++) {
  93.    if(note[i] == keyword[match])
  94.       match++;
  95. else {
  96.    if(note[i] == keyword[0])
  97.       match = 1;
  98. else
  99.    match = 0;
  100. }
  101. if(match == keyword_length)
  102.     return 1;
  103. }
  104.  return 0;
  105. }



Grazie anticipatamente a tutti per l'aiuto :k:

PM Quote
Avatar
GuglielmoS (Ex-Member)
Pro


Messaggi: 114
Iscritto: 27/11/2009

Segnala al moderatore
Postato alle 11:43
Mercoledì, 23/12/2009
Gli errori credo siano dovuti alla parentesi che c'è prima del lseek alla fine della funzione find_user_note. Comunque hai fatto un po di casino tra indentazione e parentesi graffe.
Codice sorgente - presumibilmente C++

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<fcntl.h>
  4. #include<sys/stat.h>
  5. #include "hacking.h"
  6.  
  7. #define FILENAME "/var/notes"
  8.  
  9. int print_notes(int, int, char *);
  10. int find_user_note(int, int);
  11. int search_note(char *, char *);
  12. void fatal(char *);
  13.  
  14. int main(int argc, char *argv[]) {
  15.     int userid, printing=1, fd;
  16.     char searchstring[100];
  17.  
  18.     if(argc > 1)                               // se esiste un arg,
  19.         strcpy(searchstring, argv[1]);         // quella è la stringa di ricerca;
  20.     else
  21.         searchstring[0] = 0;                    // la stringa di ricerca è vuota.
  22.  
  23.     userid = getuid();
  24.     fd = open(FILENAME, O_RDONLY);              // apre il file in sola lettura
  25.     if( fd == -1)
  26.        fatal("in main() while opening file for reading");
  27.  
  28.     while(printing)
  29.        printing = print_notes(fd, userid, searchstring);
  30.    
  31.     printf("-------[end of note data ]--------\n");
  32.     close(fd);
  33. }
  34.  
  35. // Stampa le note per un determinato id utentecorrispondenti a una
  36. // Stringa opzionale
  37. // Restituisce 0 alla fine del file o 1 se ci sono altre note;
  38. int print_notes(int fd, int uid, char *searchstring){
  39.    int note_length;
  40.    char byte=0;
  41.    char note_buffer[100];
  42.  
  43.    note_length = find_user_note(fd, uid);
  44.    if(note_length == -1)
  45.       return 0;
  46.  
  47.    read(fd, note_buffer, note_length);
  48.    note_buffer[note_length] = 0;
  49.  
  50.    if(search_note(note_buffer, searchstring))
  51.       printf("%s", note_buffer);
  52.    return 1;
  53. }
  54.  
  55. // Trova la nota successiva per un determinato id utente;
  56. // Restituisce -1 alla fine del file;
  57. // Altrimenti restituisce la lunghezza del file
  58. int find_user_note(int fd, int user_uid) {
  59.     int note_uid = -1;
  60.     unsigned char byte;
  61.     int length;
  62.  
  63.     while(note_uid != user_uid) {
  64.        if(read(fd, &note_uid, 4) != 4)
  65.            return -1;
  66.        if(read(fd, &byte, 1) != 1)
  67.            return -1;
  68.  
  69.        byte = length = 0;
  70.        // Se qui non metti le graffe l'istruzione length++ verrà eseguita solo 1 volta
  71.        while(byte != '\n') {
  72.          if(read(fd, &byte, 1) != 1)
  73.            return -1;
  74.          length++;
  75.        }
  76.     }
  77.  
  78.    lseek(fd, length * -1, SEEK_CUR); // Riporta la lettura del file indietro di length byte.
  79.    printf("[DEBUG] found a %d byte note for user id %d\n", length, note_uid);
  80.    return length;
  81. }
  82.  
  83. // Ricerca di nota in base a una parola chiave, 1 in caso di successo e 0 in caso contrario
  84.  
  85. int search_note(char *note, char *keyword) {
  86.     int i, keyword_length, match=0;
  87.  
  88.     keyword_length=strlen(keyword);
  89.     if(keyword_length == 0)
  90.         return 1;
  91.  
  92.     for(i=0;  i< strlen(note); i++) {
  93.        
  94.         if(note[i] == keyword[match])
  95.             match++;
  96.         else {
  97.             if(note[i] == keyword[0])
  98.                 match = 1;
  99.             else
  100.                 match = 0;
  101.         } // Fine If-Else
  102.        
  103.         if(match == keyword_length)
  104.             return 1;
  105.      } // Fine For
  106.  
  107.      return 0;
  108. }


Ultima modifica effettuata da GuglielmoS il 23/12/2009 alle 11:47
PM Quote
Avatar
gighen991 (Normal User)
Newbie


Messaggi: 20
Iscritto: 27/04/2009

Segnala al moderatore
Postato alle 11:48
Mercoledì, 23/12/2009
si lascia stare le graffe xk io sono un po troppo incasinato...lo so... ihihih... cmq non capisco che errore sia quello delle parentesi.. devo toglierle?

grazie ancora:k:

PM Quote
Avatar
GuglielmoS (Ex-Member)
Pro


Messaggi: 114
Iscritto: 27/11/2009

Segnala al moderatore
Postato alle 12:04
Mercoledì, 23/12/2009
Testo quotato

Postato originariamente da gighen991:

si lascia stare le graffe xk io sono un po troppo incasinato...lo so... ihihih... cmq non capisco che errore sia quello delle parentesi.. devo toglierle?

grazie ancora:k:  


No teoricamente dove te le ho aggiunte devi lasciarle. Se no il while non esegue tutte le istruzione che tu volevi. Infatti l'errore probabilmente era dovuto al fatto che tu non avevi messo la '{' dopo while ma l'avevi tenuta alla fine del ciclo, e quindi il compilatore dava i suoi errori.

PM Quote
Avatar
gighen991 (Normal User)
Newbie


Messaggi: 20
Iscritto: 27/04/2009

Segnala al moderatore
Postato alle 12:29
Mercoledì, 23/12/2009
ah!! grazie mille!!! ora provo subito e vedo se funziona!! =).. cmq bella la tua frase di Asimov.. io sto leggendo il libro di fisica.. è interessante e molto bravo come scrittore =)

PM Quote
Avatar
gighen991 (Normal User)
Newbie


Messaggi: 20
Iscritto: 27/04/2009

Segnala al moderatore
Postato alle 12:32
Mercoledì, 23/12/2009
grazieeeeeeeeeeeeeeeeeee!!! funziona tutto oraaaaa =) grazie mille!!!! ;)

PM Quote
Avatar
GuglielmoS (Ex-Member)
Pro


Messaggi: 114
Iscritto: 27/11/2009

Segnala al moderatore
Postato alle 13:01
Mercoledì, 23/12/2009
Testo quotato

Postato originariamente da gighen991:

grazieeeeeeeeeeeeeeeeeee!!! funziona tutto oraaaaa =) grazie mille!!!! ;)


Di niente! (li so anche io mi sono appassionato da poco ai suoi libri, però quella frase che ho trovato in internet mi è proprio piaciuta ;D).

PM Quote