Oppure

Loading
23/12/09 10:22
gighen991
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

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


il codice del programma è il seguente

#include<stdio.h>
#include<string.h>
#include<fcntl.h>
#include<sys/stat.h>
#include "hacking.h"


#define FILENAME "/var/notes"


int print_notes(int, int, char *);
int find_user_note(int, int);
int search_note(char *, char *);
void fatal(char *);


int main(int argc, char *argv[]) {
    int userid, printing=1, fd;
    char searchstring[100];
if(argc > 1)                               // se esiste un arg,
    strcpy(searchstring, argv[1]);         // quella è la stringa di ricerca;
else
    searchstring[0] = 0;                    // la stringa di ricerca è vuota.

userid = getuid();
fd = open(FILENAME, O_RDONLY);              // apre il file in sola lettura
if( fd == -1)
   fatal("in main() while opening file for reading");

while(printing)
   printing = print_notes(fd, userid, searchstring);
 printf("-------[end of note data ]--------\n");
 close(fd);
}

// Stampa le note per un determinato id utentecorrispondenti a una
// Stringa opzionale
// Restituisce 0 alla fine del file o 1 se ci sono altre note;
int print_notes(int fd, int uid, char *searchstring){
   int note_length;
   char byte=0;
   char note_buffer[100];

   note_length = find_user_note(fd, uid);
   if(note_length == -1)
      return 0;

     read(fd, note_buffer, note_length);
     note_buffer[note_length] = 0;

   if(search_note(note_buffer, searchstring))
      printf("%s", note_buffer);
      return 1;
}

// Trova la nota successiva per un determinato id utente;
// Restituisce -1 alla fine del file;
// Altrimenti restituisce la lunghezza del file
int find_user_note(int fd, int user_uid) {
    int note_uid = -1;
    unsigned char byte;
    int length;

    while(note_uid != user_uid) {
       if(read(fd, &note_uid, 4) != 4)
           return -1;
       if(read(fd, &byte, 1) != 1)
           return -1;

       byte = length = 0;
       while(byte != '\n')
         if(read(fd, &byte, 1) != 1)
           return -1;
         length++;
    }
}

   lseek(fd, length * -1, SEEK_CUR); // Riporta la lettura del file indietro di length byte.
   printf("[DEBUG] found a %d byte note for user id %d\n", length, note_uid);
   return length;
}

// Ricerca di nota in base a una parola chiave, 1 in caso di successo e 0 in caso contrario

int search_note(char *note, char *keyword) {
    int i, keyword_length, match=0;

    keyword_length=strlen(keyword);
  if(keyword_length == 0)
    return 1;

for(i=0;  i< strlen(note); i++) {
   if(note[i] == keyword[match])
      match++;
else {
   if(note[i] == keyword[0])
      match = 1;
else
   match = 0;
}
if(match == keyword_length)
    return 1;
}
 return 0;
}


Grazie anticipatamente a tutti per l'aiuto :k:
aaa
23/12/09 10:43
GuglielmoS
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.
#include<stdio.h>
#include<string.h>
#include<fcntl.h>
#include<sys/stat.h>
#include "hacking.h"

#define FILENAME "/var/notes"

int print_notes(int, int, char *);
int find_user_note(int, int);
int search_note(char *, char *);
void fatal(char *);

int main(int argc, char *argv[]) {
    int userid, printing=1, fd;
    char searchstring[100];

    if(argc > 1)                               // se esiste un arg,
	strcpy(searchstring, argv[1]);         // quella è la stringa di ricerca;
    else
	searchstring[0] = 0;                    // la stringa di ricerca è vuota.

    userid = getuid();
    fd = open(FILENAME, O_RDONLY);              // apre il file in sola lettura
    if( fd == -1)
       fatal("in main() while opening file for reading");

    while(printing)
       printing = print_notes(fd, userid, searchstring);
    
    printf("-------[end of note data ]--------\n");
    close(fd);
}

// Stampa le note per un determinato id utentecorrispondenti a una
// Stringa opzionale
// Restituisce 0 alla fine del file o 1 se ci sono altre note;
int print_notes(int fd, int uid, char *searchstring){
   int note_length;
   char byte=0;
   char note_buffer[100];

   note_length = find_user_note(fd, uid);
   if(note_length == -1)
      return 0;

   read(fd, note_buffer, note_length);
   note_buffer[note_length] = 0;

   if(search_note(note_buffer, searchstring))
      printf("%s", note_buffer);
   return 1;
}

// Trova la nota successiva per un determinato id utente;
// Restituisce -1 alla fine del file;
// Altrimenti restituisce la lunghezza del file
int find_user_note(int fd, int user_uid) {
    int note_uid = -1;
    unsigned char byte;
    int length;

    while(note_uid != user_uid) {
       if(read(fd, &note_uid, 4) != 4)
           return -1;
       if(read(fd, &byte, 1) != 1)
           return -1;

       byte = length = 0;
       // Se qui non metti le graffe l'istruzione length++ verrà eseguita solo 1 volta
       while(byte != '\n') {
         if(read(fd, &byte, 1) != 1)
           return -1;
         length++;
       }
    }

   lseek(fd, length * -1, SEEK_CUR); // Riporta la lettura del file indietro di length byte.
   printf("[DEBUG] found a %d byte note for user id %d\n", length, note_uid);
   return length;
}

// Ricerca di nota in base a una parola chiave, 1 in caso di successo e 0 in caso contrario

int search_note(char *note, char *keyword) {
    int i, keyword_length, match=0;

    keyword_length=strlen(keyword);
    if(keyword_length == 0)
	return 1;

    for(i=0;  i< strlen(note); i++) {
	
        if(note[i] == keyword[match])
            match++;
	else {
	    if(note[i] == keyword[0])
	        match = 1;
	    else
		match = 0;
	} // Fine If-Else
	
        if(match == keyword_length)
	    return 1;
     } // Fine For

     return 0;
}
Ultima modifica effettuata da GuglielmoS 23/12/09 10:47
aaa
23/12/09 10:48
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:
aaa
23/12/09 11:04
GuglielmoS
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.
aaa
23/12/09 11:29
gighen991
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 =)
aaa
23/12/09 11:32
gighen991
grazieeeeeeeeeeeeeeeeeee!!! funziona tutto oraaaaa =) grazie mille!!!! ;)
aaa
23/12/09 12:01
GuglielmoS
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).
aaa