2017-04-22 6 views
-3

Ich habe seltsame Fehler, die meisten von ihnen bezieht sich auf Zeilen 9-12 (Funktion Dekalitionen), und ich kann das Problem nicht finden. Bitte helfen Sie mir :) DankViele Fehler in C, "Neudefinition" Fehler und mehr

Der Code:

#include <stdlib.h> 
#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 
#define MAX 256 


void Get_Lost(char* str); 
void input_book(Book* B, FILE *in); 
void input_library(Library *L, FILE *in); 
void output_book(Book* B, FILE *out); 
void output_library(Library* L, FILE *out); 


typedef struct Book 
{ 
    char book_code[10]; 
    char *book_name; 
}Book; 

typedef struct Library 
{ 
    char library_name[MAX]; 
    int num_books; 
    Book *B; 
}Library; 

int index = 0;//global variable to check the number of 'enters' n the input book function 

int main() 
{ 
    FILE *in, *out; 
    Library Libr; 
    input_library(&Libr, in); 
    fclose(in); 
    output_library(&Libr, out); 
    fclose(out); 
    //for (i = 0; i<Libr.num_books; i++) 
    return 0; 
} 

void input_book(Book* B, FILE *in) 
{ 
    int counter = 0; //counts the number of enters (\n) to identify the book_code and book_name 
    char temp_book_name[MAX]; 
    in = fopen("input.txt", "rt"); 
    if (in == NULL) 
    { 
     Get_Lost("File not found"); 
    } 

    while (counter < 2 + index) 
    { 

     if (getc(in) == '\n') 
      counter++; 
     in++; 
    } 
    index++; //increasing the index for the next line 
    fgets(B->book_code, MAX, in); //put the book code in the structure 
    fgets(temp_book_name, MAX, in);//puts the book name in temprary var, in order to allocate memory dynamically afterwards 
    B->book_name = (char *)malloc(strlen(temp_book_name) * sizeof(char));/*dynamic allocation of memory 
                     for the book name using temp var*/ 
    if (B->book_name == NULL) 
    { 
     Get_Lost("No memory"); 
    } 
    B->book_name = temp_book_name; 
    fclose(in); 
} 

void input_library(Library *L, FILE *in) 
{ 
    int j; 
    Book B; 
    in = fopen("input.txt", "rt"); 
    if (in == NULL) 
    { 
     Get_Lost("File not found"); 
    } 
    fgets(L->library_name, MAX, in); 
    fgets(L->num_books, MAX, in); 
    L->B = (Book *)malloc(L->num_books * sizeof(Book));//dynamic allocation for the array of books in the library 
    for (j = 0; j < L->num_books; j++) 
    { 
     input_book(&L->B[j], in);//calling the input_book function 'num_books' times to fill all the Books 
    } 
    fclose(in); 
} 

void output_book(Book* B, FILE *out) 
{ 
    out = fopen("input.txt", "wt"); 
    if (out == NULL) 
    { 
     Get_Lost("File not found"); 
    } 
    fprintf(out, "%s ", B->book_code); 
    fprintf(out, "%s\n", B->book_name); 
    fclose(out); 
} 

void output_library(Library* L, FILE *out) 
{ 
    int k; 
    Book B; 
    out = fopen("input.txt", "wt"); 
    if (out == NULL) 
    { 
     Get_Lost("File not found"); 
    } 

    fprintf(out, "%s\n", L->library_name); 
    for (k = 0; k < L->num_books; k++) 
    { 
     output_book(&L->B[k], out);//calling the output_book function 'num_books' times to print the books' details 
    } 
    fclose(out); 
} 

void Get_Lost(char* str) 
{ 
    printf("\n%s", str); 
    exit(1); 
} 

FEHLER:

C2059: syntax error : ')' LINE 9 
Error 10 error C2059: syntax error : ')'  LINE 10 
Error 15 error C2059: syntax error : ')'  LINE 11 
Error 20 error C2059: syntax error : ')'  LINE 12 
Error 1 error C2143: syntax error : missing ')' before '*'  LINE 9  
Error 6 error C2143: syntax error : missing ')' before '*'  LINE 10  
Error 11 error C2143: syntax error : missing ')' before '*'  LINE 11  
Error 16 error C2143: syntax error : missing ')' before '*'  LINE 12  
Error 4 error C2143: syntax error : missing ';' before '*'  LINE 9  
Error 9 error C2143: syntax error : missing ';' before '*' LINE 10   
Error 14 error C2143: syntax error : missing ';' before '*'  LINE 11  
Error 19 error C2143: syntax error : missing ';' before '*'  LINE 12 
Error 2 error C2143: syntax error : missing '{' before '*'  LINE 9  
Error 7 error C2143: syntax error : missing '{' before '*' LINE 10 
Error 12 error C2143: syntax error : missing '{' before '*'  LINE 11  
Error 17 error C2143: syntax error : missing '{' before '*'  LINE 12  
C2371: 'FILE' : redefinition; different basic types LINE 9 
C2371: 'FILE' : redefinition; different basic types LINE 10  
C2371: 'FILE' : redefinition; different basic types LINE 11 
C2371: 'FILE' : redefinition; different basic types LINE 12 
C2371: 'input_library' : redefinition; different basic types  LINE 73 
C2371: 'output_library' : redefinition; different basic types LINE 105 
+3

Sie müssen Ihre Strukturen deklarieren, bevor Sie sie in Ihren Funktionsprototypen verwenden –

+0

Was denken Sie, dass das 'fgets (L-> num_books, MAX, in);' tun wird? –

+0

verdammt, vergessen, dass Strukturen Befors Funktionen erklärt werden .. –

Antwort

2

Ihre Struktur Definitionen bewegen, so dass sie vor Ihrer Funktionsdeklarationen sind. Auch mein Compiler mag die Verwendung von Index als Variablenname nicht, wie er in string.h definiert ist.

auch mehrere Warnungen gegeben sind, können Sie in und out, bevor Sie sie in Ihrer Funktion initialisieren sollte ruft die Warnungen zum Schweigen zu bringen. Sie können es so tun File *in = NULL, *out = NULL;, auch fgets erwartet eine char*, aber Sie passieren L->num_books, die eine int ist.

+0

änderte es zu "i", jetzt theres ein Laufzeitfehler in Zeile 34, wenn ich die erste Funktion anrufe. –

+0

@Ido Ich kann diesen Fehler nicht reproduzieren – nitronoid

+0

ok ich habe die problematischen fgets in fscanf geändert. verstehe immer noch nicht das Problem mit "in" und "out". Ich denke, ich erklärte sie in der Bettel des Haupt –

Verwandte Themen