2011-04-28 74 views
1

嗨,大家好,我在这里遇到了一个问题,结构是,我创建了一个结构,然后创建了一个捕获从该结构引用的员工详细信息的函数。现在问题出现时,我尝试调用主函数。请给我一些关于如何调用该函数的指针。代码如下:问题结构在c

typedef struct employeeType 
{ 
    char name; 
    int employeeNumber; 
    float salary; 
    float taxPercentage; 
}EMPLOYEE; 

void enterDetails(EMPLOYEE details) 
{ 
    FILE *file; 
    file = fopen("employees.txt","w"); 
    if(file == NULL) 
    { 
     printf("File error!!!"); 
     exit(0); 
    } 
    else 
    { 
     fprintf(file,"%s",details); 
    } 
    fclose(file); 

} 

void main() 
{ 
    enterDetails(); 
} 

我不知道是什么参数传递给函数在主

+1

您可能是指'int main(){}'。 – 2011-04-28 09:20:32

+0

@Cody:'int main(){}'不会做太多;-) – 2011-04-28 09:23:21

+0

Hrm,至少它编译。我认为这显然不是重点。 – 2011-04-28 09:24:32

回答

0
void main() 
{ 
    EMPLOYEE details; 
    // get the value of element of struct from scanf or from other way 
    printf("Enter Name : "); 
    scanf("%s", details.name); // same for others, change the format specifier according to their data type 
    enterDetails(details); 
} 

和struct应该像

typedef struct employeeType 
{ 
    char name[]; // should be an array or pointer, to store name 
    int employeeNumber; 
    float salary; 
    float taxPercentage; 
}EMPLOYEE; 
+0

@Gaurav我想提示详细信息并将其写入文件 – TaIra 2011-04-28 09:33:32

+0

@Talra:现在查看我的答案。 – Gaurav 2011-04-28 09:36:39

+0

@Gaurav:你的代码会导致崩溃。你已经将display.name声明为一个指针,但是在使用scanf之前并没有将它初始化为任何内存。可能更安全的去与阵列。 – forsvarir 2011-04-28 09:46:00

0

可以传递结构的指针

void main() 
{ 
    EMPLOYEE employee; 
    ..... 
    enterDetails(&employee); 
} 

void enterDetails(EMPLOYEE *details) 
{ 

} 
1

我已经注释你的代码与其他一些问题需要考虑

typedef struct employeeType 
{ 
    /* THIS IS ONLY ONE CHARACTER... SEEMS WRONG */ 
    /* should be 'char name[someMaxSize]', or 'char *name' */ 
    char name; 
    int employeeNumber; 
    float salary; 
    float taxPercentage; 
}EMPLOYEE; 

/* As pointed out by 'Cody Gray', this function is called 'enterDetails' 
* does it really need to have a parameter at all, or should it be responsible 
* for taking the details from the user? Is it an appropriately 
* named method for the task it's actually performing 
* (would saveDetails be better for example)? 
*/ 
void enterDetails(EMPLOYEE details) 
{ 
    FILE *file; 
    file = fopen("employees.txt","w"); 
    if(file == NULL) 
    { 
     printf("File error!!!"); 
     exit(0); 
    } 
    else 
    { 
     /* THIS IS PASSING A STRUCTURE AS A STRING */ 
       /* You probably want to write out the individual fields instead */ 
       /* fprintf(file, "%s,%d", details.name, details.employeeNumber); etc */ 
     fprintf(file,"%s",details); 
    } 
    fclose(file); 

} 

void main() 
{ 
    EMPLOYEE details; 
    /* populate details somehow then pass it in to the function*/ 
    enterDetails(details); 
} 

您可能还需要考虑细节传递到函数的指针,尽管这会改变你的函数签名,那岂不是你不会将太多信息推入堆栈。

如果用指针版本,那么去:

void enterDetails(EMPLOYEE details) 

将成为

void enterDetails(EMPLOYEE *details) 

和主会变成:

void main() 
{ 
    EMPLOYEE details; 
    /* populate details somehow then pass it in to the function as pointer */ 
    enterDetails(&details); 
} 

您还需要改变方式你在你的函数中使用细节,但正如我已经说过的,我相信你的fprintf调用已经被破坏了。

0

你需要传递一个参考,而不是一个值。如果你通过员工价值作为在过去后,它会被复制,拷贝将被修改,而不是原来

void enterDetails(EMPLOYEE* emp) { 
    // do stuffs 
} 

void main() { 
    EMPLOYEE emp; 
    enterDetails(&emp); 
} 
+0

他们写出一个文件,而不是填充结构,所以内容不会被函数修改。 – forsvarir 2011-04-28 09:27:11

+0

@forsvarir:所以如果写出一个文件,为什么'enterDetails'函数完全接受一个参数呢?它似乎应该创建一个结构的实例,填充它,保存到一个文件,然后返回。 – 2011-04-28 09:33:10

+0

@Cody Gray:一个合理的问题。它可能名字很差,意思是将详细信息输入到文件中,或者可能是你正确的,并且enterDetails函数应该询问信息以便能够将它写入文件中。认为OPs能够更好地回答他们的想法...... – forsvarir 2011-04-28 09:38:27

0

的第一个问题是你的结构不正确。由于只有一个字节,因此您不能在名称字段中存储员工的姓名。你必须使它成为一个数组(在这种情况下更简单)或指向已分配内存的指针。

如果你想使它成为一个数组,那么你应该定义数组的最大尺寸。在我们的例子中,我们只是将它设为100个字节,这将足以存储任何名称。

#define MAX_NAME 100 

typedef struct employeeType 
{ 
    char name[MAX_NAME]; 
    int employeeNumber; 
    float salary; 
    float taxPercentage; 
}EMPLOYEE; 

其次,你的功能命名很混乱。 enterDetails应该只填充你传递的结构。第三,你的输入细节应该接受一个指向EMPLOYEE结构的指针。如果你想将任何值传递给一个将会改变它的内容的函数,那么你只能使用指针(或者如果你使用C++,但基本上是一个指针)。所以enterDetails应该是,

void enterDetails(EMPLOYEE *details) 
{ 
    printf("\nEnter the employee's name "); 
    scanf("%s", details->name); // this isn't secure since it doesn't perform bound checking. 

    printf("\nEnter employee number "); 
    scanf("%d", &details->employeeNumber); 

    printf("\nEnter employee salary "); 
    scanf("%f", &details->salary); 

    printf("\nEnter tax percentage "); 
    scanf("%f", &details->taxPercentage); 

} 

最后,如果你想结构的内容存储到要给人阅读的,那么你应该格式化结构的内容和转储到一个文件的文件。

int writeToFile(EMPLOYEE *details) /* accepting the structure will work as well but it's faster and efficient to pass the structure's pointer */  
{ 
    FILE *file; 

    file = fopen("employees.txt","w"); 
    if(file == NULL) { 
     printf("File error!!!"); 
     return 0; 
    } 

    fprintf(file, "\nEmployee Name: %s", details->name); 
    fprintf(file, "\nEmployee Number: %d", details->employeeNumber); 
    fprintf(file, "\nSalary: %f", details->salary); 
    fprintf(file, "\nTax Percentage: %f", details->taxPercentage); 

    fclose(file) 
    return 1; 
} 

及主要

int main(void) 
{ 
    EMPLOYEE details; 

    enterDetails(&details); // passing the pointer here is a must 
    if (!writeToFile(&details)) { // passing the pointer since it's faster 
     printf("\nError writing to file"); 
     return 1; 
    } else { 
     printf("\nSuccess!"); 
     return 0; 
    } 
} 

而在你的情况,你不需要任何参数传递给主。但是如果你想知道如何传递参数,那么这里就是一个简单的例子。

int main(int argc, char **argv) 
{ 
    int i; 

    for (i = 0; i < argc; i++) 
     printf("\n%s", argv[i]); 

    return 0; 
} 
+0

您对scanf的电话号码呼叫不正确。这:scanf(“%d”,details-> employeeNumber);应该是传递数字的地址,而不是实际的数字,否则scanf不知道该把值放在哪里。 – forsvarir 2011-04-28 13:00:04

+0

@shebaw感谢它似乎工作,但是当我运行该程序时,它只提示输入名称和员工编号,当我按Enter键继续它暂停和终止。因此,不向文件写任何东西,问题是什么? – TaIra 2011-04-28 16:41:51

+0

@forsvarir你能给我举一个例子吗?请告诉我 – TaIra 2011-04-28 16:45:33