向前移动数组元素并插入新元素

时间:2015-01-30 02:11:13

标签: c scanf fgets

基本上我要做的是将数组中的所有元素向前移动,然后将新任务插入用户输入的任何位置。问题是fgets以某种方式将用户输入的新字符串放入两个位置。因此,当我打印完整个任务时,我会得到这个:

new task
new task(wake should be here)
2. eat
3. class
4. homework

instead of

new task
1. wake
2. eat
3. class
4. homework

我不知道为什么会这样。当我像这样设置新任务时:tasks[task_location] = "new task"。清单正确。有没有人知道我做错了什么我尝试了scanf之类的其他事情,但我仍然遇到同样的问题。

#define elements_in_array 200
char** tasks;
FILE* fp;//fopen null check etc standard stuff i did too
tasks = (char**)malloc(elements_in_array * sizeof(char*));
for(i=0;i<elements_in_array;i++)
    {
        tasks[i] = malloc(tasks_entered * sizeof(char));
    }
while(!feof(fp))// counts lines
{
    c = fgetc(fp);
    if(c == '\n')
    {
        tasks_entered+=1;
        total_tasks = tasks_entered;
    }
}
rewind(fp);
for(i=0;i<tasks_entered;i++)//reads strings into array
{
    fgets(tasks[i],elements_in_array,fp);
}

printf("Where would you like to add this task?\n");
printf("Add task to #: ");
scanf("%d", &task_Location);
if(tasks_entered > total_tasks)
        {
            //i know i didnt realloc correctly but works for now
            tasks = realloc(tasks,tasks_entered*2);
            total_tasks = tasks_entered*2;
        }
for(i=tasks_entered-1;i>=task_Location-1;i--)
{
    tasks[i+1] = tasks[i];
}   
fgets (tasks[task_location], elements_in_array, stdin);
tasks_entered+=1;

1 个答案:

答案 0 :(得分:0)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#define elements_in_array 200 //Not necessary because the dynamically allocated

int main(void){
    FILE* fp;
    fp = fopen("data.txt", "r");

    //Pre-read
    int c, count_char = 0, max_line_size = 0;
    int total_tasks, tasks_entered = 0;
    while((c = fgetc(fp)) != EOF){
        if(c == '\n'){
            tasks_entered += 1;
            if(max_line_size < count_char){
                max_line_size = count_char;
            }
            count_char = 0;//reset
        } else {
            count_char += 1;
        }
    }
    if(count_char > 0){
        tasks_entered += 1;
        if(max_line_size < count_char){
            max_line_size = count_char;
        }
    }
    char** tasks;
    total_tasks = tasks_entered;
    tasks = malloc(total_tasks * sizeof(char*));
    int buff_size = max_line_size + 2 + 16;//2: \n + \0, 16: reserve
    char *buffer = malloc(buff_size);
    rewind(fp);
    int i;
    for(i=0;i<tasks_entered;i++){
        fgets(buffer, buff_size, fp);
        int len = strlen(buffer);
        if(buffer[len-1] == '\n')
            buffer[len-=1] = '\0';
        tasks[i] = malloc(len + 1);
        strcpy(tasks[i], buffer);
        printf("%d.%s\n", i+1, tasks[i]);
    }
    fclose(fp);

    char *new_task, *p;
    printf("enter new task : ");
    fgets(buffer, buff_size, stdin);
    if(p = strchr(buffer, '\n'))
        *p = 0;//remove newline
    new_task = strdup(buffer);//malloc and copy

    int task_Location;
    printf("Where would you like to add this task?\n");
    printf("Add task to #(1-%d): ", tasks_entered+1);
    do {
        scanf("%d", &task_Location);
    } while(task_Location > total_tasks + 1);

    task_Location -= 1;//to zero origin
    tasks = realloc(tasks, (tasks_entered+1)*sizeof(char*));
    for(i=tasks_entered-1;i>=task_Location;i--){
        tasks[i+1] = tasks[i];
    }
    tasks[task_Location] = new_task;
    tasks_entered += 1;
    total_tasks = tasks_entered;
    for(i=0;i<tasks_entered;++i)
        printf("%d.%s\n", i+1, tasks[i]);

    //deallocate
    return 0;
}