【问题标题】:moving array elements forward and inserting new element向前移动数组元素并插入新元素
【发布时间】:2015-01-30 02:11:13
【问题描述】:

基本上我要做的是将数组中的所有元素向前移动,然后将新任务插入用户输入的任何位置。问题是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;

【问题讨论】:

  • 如果tasks只是一个指针数组,那么在调用fgets之前需要为输入字符串分配内存。请在您的问题中添加tasks 的声明。
  • task[i]=malloc(tasks_entered * sizeof(char)); 对我来说似乎很可疑。你确定你想要tasks_entered吗?请改用tasks[i]=malloc(100)
  • 我在概念化动态数组分配时遇到了麻烦。我想做的是制作一个二维数组,我可以在其中存储字符串(短句的长度)。例如 arr[0] = "wake up", arr[1]= "eat some food" 等等。所以基本思想是计算文本文件中的行数,然后使数组足够大以容纳之前的行数在文本文件中。不幸的是,分配是使用动态分配的数组,这让我感到困惑。
  • 尝试删除 !feof(fp)move c = fgetc(fp) 那里。
  • 然后在为它分配内存之前将字符串读入task[i]

标签: c scanf fgets


【解决方案1】:
#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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-29
    • 2014-10-25
    • 1970-01-01
    • 2013-01-29
    • 2020-04-24
    • 1970-01-01
    • 2021-11-26
    相关资源
    最近更新 更多