缓冲一组从文件中的行并将其存储在C中的数组 [英] buffering a set of lines from a file and storing it in an array in C

查看:85
本文介绍了缓冲一组从文件中的行并将其存储在C中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是做一个非常低效的方式,但它的排序工作

This might be a very inefficient way to do it, but its sort of working

这code通过文件读取,存储在一个全局数组时间8行文本(想一个更好的选择,这样做,如果可能的话),并分派作进一步处理。

This code reads through a file, stores 8 line of text at a time in a global array (Would like a better option to do this if possible ) and dispatches for further processing.

这里的code

int count = 0;             //global
char *array_buffer[8];     //global

void line(char *line_char)
{

    int lent = strlen(line_char);
    array_buffer[count] = line_char;

    printf("%d\n",count);

    if (count == 8)
    {

        int row,col;
        for(row = 0; row<count; row++){
            printf("%d\n",row);
            for(col = 0; col<=lent; col++) {
                printf("%c", array_buffer[row][col]);
            }
        printf("\n");   
        }
    count = 0;
    }

    count++;
}



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

    clock_t start = clock();

    FILE *fp = fopen(argv[1], "r");
    if(fp == NULL )
    {
        printf("Couldn't open file %s",argv[1]);
    }
    char buff[512];

    while (fgets(buff, 512, fp) != NULL )
    {
        line(buff);             /*sending out an array having one line*/
    }

    return 0;
}

的问题是,在打印出来array_buffer的内容,其打印出的最后一行中的缓冲器的8倍。 (即8号线的在每个周期读)。它的pretty明显,

The issue is that while printing out the contents of array_buffer, its printing out the last line in the buffer 8 times. (i.e. the 8th line its reading in every cycle). Its pretty obvious that

array_buff[0]
....
array_buff[7]

都指向8号线的地址

all point to the address of line 8

解决这个任何帮助吗?我知道这可能不是缓冲一点事都没有正确的方法!

any help in solving this ? I know it might not be the correct way to buffer something at all !

推荐答案

您有一个陈旧的指针,在这里我将解释

You have a stale pointer, here I will explain

while (fgets(buff, 512, fp) != NULL )
{
    //buff updated
    line(buff);
    //...
    //inside of the line function
        somepointertopointers[currIndex]=buff;

现在它正在考虑在BUFF的位置,因此,所有的元素都在看同一个位置,你需要复制的字符,或者使一个较长的缓冲,并确保你正在更新的指针看位置,就可以使8个独立的char []指针以及

now it is looking at the location at buff, so all of the elements are looking at the same location, you need to copy the chars, or make a longer buffer and make sure you are updating the location the pointer is looking at, you can make 8 separate char[] pointers as well

这会给你你想要的结果。

This will give you the result you want

buff[512][8];
char** curr = buff;
while(fget(*curr,512,fp)!= NULL)
{
     line(*curr);
     curr++;
}

或者您可以分配传递缓冲

or alternatively you could allocate the buffer that is passed

#def BUFF_SIZE 512
#def BUFF_ARRAY_LEN 8


//put this somewhere before calling line 
//to initialize your array_buffer
for(i=0;i<BUFF_ARRAY_LEN;i++)
{
    array_buffer[i]=NULL;
}

...

//update in function line
//makes more sense to just use 
//the max len of a line
if(array_buffer[count] == NULL)
    array_buffer[count]=(char*)malloc(sizeof(char)*BUFF_SIZE);
strcpy(array_buffer[count],line_char);

...

//you will also need to
//clean up after you are 
//done with the memory
for(i=0;i<BUFF_ARRAY_LEN;i++)
{
    free(array_buffer[i]);
}

这篇关于缓冲一组从文件中的行并将其存储在C中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆