将char数组复制到字符指针数组中

时间:2014-04-10 01:41:16

标签: c pointers strcpy arrays

我在尝试将char数组的内容复制到C中的char指针数组时遇到问题。我的代码如下所示:

# include<stdio.h>
# include<stdlib.h>
# include<string.h>

# define RECORD_SIZE 300
# define BUFFER_SIZE 3

/* Structure for representing a single
 * password entry */
typedef struct Record {
    char * sitename;
    char * username;
    char * password;
    struct Record * next;
} Record;

/* Declare function prototypes */
int isCorrectKey(char *,char *);
int isValidOperation(char);
int isValidSyntax(char *);
void getFields(char *,char * []);
void listEntries();
int updateEntries(char *);
int deleteEntries(char *);
int entryExists(char *);
void init(char *,char *,char *);
void readDB();
void writeToDB(char *);
void encrypt(char *,char * [],char *);
void decrypt(char *,char * [],char *);

/* Declare global variables */
char * database;
Record * records;

// Pre-condition: A character pointer to plaintext
// A character pointer to an empty buffer,
// A character pointer to the user input key
// Post-condition: The plaintext is encrypted and
// inserted into the buffer
void encrypt(char * text,char * buffer[BUFFER_SIZE],char * key) {
    int i = 0;

    for(;i < strlen(text);i++) {
        buffer[i] = ((char *) (text[i]+3));
    }
    buffer[i] = 0;
}

// Pre-condition: A character pointer to ciphertext
// A character pointer to an empty buffer
// A character pointer tothe user input key
// Post-condition: The ciphertext is decrypted and
// inserted into the buffer
void decrypt(char * text,char * buffer[BUFFER_SIZE],char * key) {
    int i = 0;

    for(;i < strlen(text);i++) {
        buffer[i] = ((char *) (text[i]-3));
    }
    buffer[i] = 0;
}

// Pre-condition: The database variable must be set
// The records variable must exist
// Post-condition: The value in database variable is
// used as filename to read the data. The data that
// is read and used to initialize the records variable
void readDB() {
    // Open the file in read-only mode
    FILE * f = fopen(database,"r");
    char buffer[BUFFER_SIZE];
    int index = 0;

    if(f == NULL) {
        // Print error if file is invalid
        printf("Sorry. Unable to find password database.");
    } else {
        char c = 0;
        // Read the file
        while((c = fgetc(f)) != EOF) {
            if(c != '\n') {
                buffer[index++] = c;
            } else {
                buffer[index] = 0; // terminate each entry with null
                index = 0;
            }
        }
        fclose(f); // Close the file handle
    }
}

// Pre-condition: The database variable must be set
// A character pointer to some text must be provided
// as input
// Post-condition: The value in database variable is
// used as filename to write the data. 
void writeToDB(char * text) {
    // Open the file in append mode
    FILE * f = fopen(database,"a");
    int index = 0;

    if(f == NULL) {
        // Print error if file is invalid
        printf("Sorry. Unable to find password database.");
    } else {
        fputs(text,f); // Write to the file
        fclose(f); // Close the file handle
    }
}

// Pre-condition: A character pointer to the user input key
// A character pointer to the actual key
// The records variable must be set
// Post-condition: Returns 1 if the key value in the records variable
// has been properly decrypted.
// Returns 0 otherwise.
int isCorrectKey(char * uKey,char * mkey) {
    return strcmp("ThisIsTheSecretKey",records->password);
}

// Pre-condition: A character indicating an operation value
// Post-condition: Returns 1 if the operation value is supported
// Returns 0 otherwise.
int isValidOperation(char operation) {
    return operation == 'L' || operation == 'U' || operation == 'D';
}

// Pre-condition: A character pointer to a user input command string
// Post-condition: Returns 1 if the syntax of the command is correct
int isValidSyntax(char * command) {
    return 0;
}

// Pre-condition: A character pointer to a comma delimited string
// Post-condition: The string is split into segments and stored into the buffer
void getFields(char * record,char * buffer[BUFFER_SIZE]) {
    int i = 0;
    int buffer_i = 0;
    int record_len = strlen(record);
    char tmp_buffer[RECORD_SIZE+1];
    int tmp_i = 0;

    for(;i < record_len;i++) {
        if(record[i] != ',') {
            tmp_buffer[tmp_i++] = record[i];
        } else {
            tmp_buffer[tmp_i] = 0;
            strcpy(buffer[buffer_i++],tmp_buffer);
            tmp_i = 0;
        }
    }
}

int main(int argc,char * argv[]) {
    //database = "test.txt";
    //readDB();
    char * buffer[BUFFER_SIZE];
    getFields("google,geek,pass123",buffer);

    int i = 0;

    for(;i<BUFFER_SIZE;i++) {
        printf(buffer[i]);
        printf("\n");
    }

    return 0;
}

从我看到的,有问题的行在getFields()函数中:

strcpy(buffer[buffer_i++],tmp_buffer);

我正在尝试将tmp_buffer的内容复制到缓冲区的索引中。我的程序一直在崩溃。我不知道为什么。有人可以帮帮我吗?感谢。

2 个答案:

答案 0 :(得分:2)

您没有初始化char *缓冲区以指向任何内容,例如buffer[0] = malloc(SIZE)

答案 1 :(得分:1)

您需要为缓冲区中的三个条目中的每个条目分配内存。就像现在一样,你正在将字符串复制到虚空中。

你需要这样的东西:

for(i=0;i<BUFFER_SIZE;i++) {
    buffer[i] = malloc(MAX_STR_SIZE);
}

然后在最后完成时释放它们:

for(i=0;i<BUFFER_SIZE;i++) {
    free(buffer[i]);
}
相关问题