每当我尝试将Strucs数组传递给函数时,我就会不断收到此错误。
10:错误:'RECORD'之前的预期声明说明符或'...' 113:错误:'编辑'的冲突类型 10:错误:之前的'edit'声明就在这里
关于如何使用strlen和strcmp关于参数以及如何在没有强制转换的情况下从int创建指针,我也收到了很多警告。这有什么不对吗?
有人能告诉我到目前为止我的代码有什么问题吗?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXLENGTH 51 //longest length of a single record + 1 =51 bytes
#define SIZEOFDB 1000
#define ENCRYPT 5
void add(int argc, char *argv[]);
void del (char *username);
void edit(char *nName, char *nPw, char *nType, char *tName, char *tPw, RECORD *arr);
int verify (char *username, char *password);
void parse (char *record, char *delim, char);
typedef struct rec
{
char name[22];
char pw[22];
char type[6];
}RECORD;
int main(int argc, char *argv[]){
int j;
char tempName[22];
char tempPw[22];
char tempType[6];
char newName[22];
char newPw[22];
char newType[6];
static const char filename[] = "password.csv";
FILE *file;
file = fopen(filename, "r+");
if (file == NULL){
//create csv file password.csv
printf("Just created password.csv");
file = fopen(filename, "w+");
}
size_t i =0, count, size = 1000;
RECORD *arr = malloc(size * sizeof *arr);
if (arr){
char line[51];
while(i<size && fgets(line, sizeof(line), file)){
sscanf(line, "%[^','],%[^','],%s", &arr[i].name, &arr[i].pw, &arr[i].type);
++i;
}
fclose(file);
if (strcmp(argv[1], "-menu") == 0){
//menu function
printf("MENU\n");
} else if(strcmp( argv[1], "-add") == 0){
//add function
strncpy(newName,argv[2],strlen(argv[2]));
strncpy(newPw,argv[3],strlen(argv[3]));
strncpy(newType,argv[4], strlen(argv[4]));
printf("ADD\n");
} else if(strcmp( argv[1], "-del") == 0){
//del function
strncpy(tempName,argv[2], strlen(argv[2]));
printf("DEL\n");
} else if(strcmp( argv[1], "-edit") == 0){
//edit funciton
strncpy(tempName,argv[2],strlen(argv[2]));
strncpy(tempPw,argv[3],strlen(argv[3]));
strncpy(tempType,argv[4],strlen(argv[4]));
strncpy(newName,argv[5], strlen(argv[5]));
strncpy(newPw,argv[6], strlen(argv[6]));
strncpy(newType, argv[7], strlen(argv[7]));
printf("EDIT\n");
} else if(strcmp( argv[1], "-verify") == 0){
//verify function
strncpy(tempName,argv[2],strlen(argv[2]));
strncpy(tempPw,argv[3],strlen(argv[3]));
printf("VERIFY\n");
} else {
printf("SYNTAX ERROR\n");
return 0;
}
for (j = 0; j < i; j++){
printf("%s,%s,%s\n", arr[j].name,arr[j].pw,arr[j].type);
}
}
free (arr);
return 0;
}
/*void add(char nName[],char nPw[], char nType[], record *arr,FILE *file; ){
int i,j;
if (arr){
char line[51];
while(i<size && fgets(line, sizeof(line), file)){
sscanf(line, "%[^','],%[^','],%s", &arr[i].name, &arr[i].pw, &arr[i].type);
++i
}
}*/
void edit(char *nName,char *nPw, char *nType, char *tName, char *tPw, RECORD *arr){
int i,j;
int size = (sizeof *arr)/(sizeof arr[0]);
for (i=0; i< size; i++){
if ((strcmp( arr[i].name, *tName) == 0 && strcmp (arr[i].pw, *tPw) == 0)){
for (j=0; j<size; j++){
if (strcmp(*nName, arr[j].name) == 0){
printf("ERROR USERNAME ALREADY EXISTS");
break;
}
}
strncpy(arr[i].name, *nName, strlen(*nName));
strncpy(arr[i].pw, *nPw, strlen(*nPw));
strncpy(arr[i].type, *nType, strlen(*nType));
}
}
}
由于
答案 0 :(得分:2)
在参数列表中使用之前,需要声明RECORD
结构。也就是说,你应该在函数的前向声明之前将它移动到它。
答案 1 :(得分:0)
尝试是这样的:
typedef struct rec
{
char name[22];
char pw[22];
char type[6];
}RECORD;
void add(int argc, char *argv[]);
void del (char *username);
void edit(char *nName, char *nPw, char *nType, char *tName, char *tPw, RECORD *arr);
int verify (char *username, char *password);
void parse (char *record, char *delim, char);
而不是:
void add(int argc, char *argv[]);
void del (char *username);
void edit(char *nName, char *nPw, char *nType, char *tName, char *tPw, RECORD *arr);
int verify (char *username, char *password);
void parse (char *record, char *delim, char);
typedef struct rec
{
char name[22];
char pw[22];
char type[6];
}RECORD;
答案 2 :(得分:0)
尝试在“编辑”
的函数声明之前放置“RECORD”的结构定义