从文件中读取一行并分配给缓冲区 [英] Read a line from file and assign to a buffer

查看:79
本文介绍了从文件中读取一行并分配给缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我是Linux C编程的新手



我对此有疑问如何读取一行并分配给缓冲区



我有一个名为test.txt的文件



我能够读取第一行并将其分配给缓冲区



我的疑问是如何读取第二行并将其分配给第二个缓冲区



test.txt 文件包含这两行



V_fps = 30

V_width = 320






我的代码是
< br $>


 #include< stdlib.h> / * exit,malloc,realloc,free * / 
#include< stdio.h>

int main()
{
FILE * file;
char * line = NULL;
size_t len = 0;
char read;
char * buf1,* buf2;
file = fopen(test.txt,r);

if(file == NULL)
返回1;

while((读= getline(& line,& len,file))!= -1){
// printf(检索长度%d的行:\ n,阅读);
buf1 = line;
// printf(%s,line);
printf(%s,buf1);
}

if(line)
free(line);

返回0;
}







建议我了解如何做到这一点

我想要o / p如下



buf1 = V_fps = 30

buf2 = V_width = 320

解决方案

您认为这稍微复杂一些。不难,但确实需要一些你可能还没有遇到过的概念。



所以,你有上面的代码(直接来自 getline文档 [ ^ ]并且您想要修改它。



幸运的是,getline的Linux版本将分配实际的内存本身,所以你不必担心这一点。

所以,让我们在某个地方声明要保留这些内容:

  char  **行[ 10 ]; 

为十行分配足够的空间 - 每行是一个字符集合(或指向字符的指针),所以我们需要一个指向字符的指针来保存它,我们需要一个集合其中)

现在,我们可以保存线路 - 但我们需要先做一点设置:

  int  lineIndex =  0 ; 

让我们知道每次我们在做什么,然后:

  while (lineIndex<  10 && (read = getline(& line,& len,file))!=  -   1 
{
...
line = NULL;
lineIndex ++;
}

将我们获取的行数限制为适合我们定义的行数的数字。我们还必须每次重置的值,否则getline将不会为我们分配空间。

  while (lineIndex<  10 &&(read = getline(& line,&) len,file))!=  -   1 
{
lines [lineIndex] = line;

buf1 = line;
printf( %s,buf1);

line = NULL;
lineIndex ++;
}

我们现在需要做的就是在完成后释放内存:

  while (lineIndex>  0 
{
lineIndex--;
if (lines [lineIndex])
{
free(lines [lineIndex]);
}
}





我们最终得到的结果是:

< pre lang =c ++> int main()
{
FILE * file;
char * line = NULL;
size_t len = 0 ;
char read;
char * buf1;
int lineIndex = 0 ;
char **行[ 10 ];

file = fopen( test.txt r);
if (file == NULL)
{
return < span class =code-digit> 1 ;
}

while (lineIndex< 10 && ;(read = getline(& line,& len,file))!= - 1
{
lines [lineIndex] =线;

buf1 = line;
printf( %s,buf1);

line = NULL;
lineIndex ++;
}

while (lineIndex> 0
{
lineIndex--;
if (lines [lineIndex])
{
free(lines [lineIndex]);
}
}

返回 0 ;
}





这有意义吗?


hi all,

I am new to Linux C programming

I have a doubt on how to read a line and assign to a buffer

I have a file called test.txt

I can able to read a first line and i assigned that to a buffer

My doubt is how can i read a second line and assign it to second buffer

test.txt file contains these two line

V_fps=30
V_width=320



My code is


#include <stdlib.h>     /* exit, malloc, realloc, free */
#include <stdio.h>

int main()
{
FILE *file;
char *line = NULL;
size_t len = 0;
char read;
char *buf1, *buf2;
file=fopen("test.txt", "r");

if (file == NULL)
    return 1;

while ((read = getline(&line, &len, file)) != -1) {
//    printf("Retrieved line of length %d :\n", read);
        buf1 = line;
   // printf("%s", line);
    printf("%s", buf1);
}

if (line)
    free(line);

return 0;
}




suggest me a n idea on how to do this
I want the o/p as below

buf1 = V_fps=30
buf2 = V_width=320

解决方案

This is slightly more complicated that you think. Not difficult, but it does need some concepts that you may not have met yet.

So, you have the code above (which you got directly from the getline documentation[^] and you want to modify it.

Fortunately, the Linux version of getline will allocate the actual memory itself, so you don;t have to worry about that bit.
So, let's declare somewhere to keep the lines:

char** lines[10];

That allocates enough space for ten lines - each line is a collection of characters (or a pointer-to-character), so we need a pointer-to-pointer-to-character to hold it, and we need a collection of them)
So now, we can save the lines - but we need to do a little setup first:

int lineIndex = 0;

lets us know what line we are doing each time, so then:

while (lineIndex < 10  && (read = getline(&line, &len, file)) != -1)
   {
   ...
   line = NULL;
   lineIndex++;
   }

Limits the number of lines we fetch to a number that will fit in the array of lines we defined. We also have to reset the value of line each time, or getline will not allocate the space for us.

while (lineIndex < 10  && (read = getline(&line, &len, file)) != -1)
   {
   lines[lineIndex] = line;

   buf1 = line;
   printf("%s", buf1);
   
   line = NULL;
   lineIndex++;
   }

All we need to do now, is release the memory when we are finished:

while(lineIndex > 0)
   {
   lineIndex--;
   if (lines[lineIndex])
      {
      free(lines[lineIndex]);
      }
   }



So what we end up with is:

int main()
    {
    FILE *file;
    char *line = NULL;
    size_t len = 0;
    char read;
    char *buf1;
    int lineIndex = 0;
    char** lines[10];

    file=fopen("test.txt", "r");
    if (file == NULL)
        {
        return 1;
        }

    while (lineIndex < 10  && (read = getline(&line, &len, file)) != -1)
        {
        lines[lineIndex] = line;

        buf1 = line;
        printf("%s", buf1);

        line = NULL;
        lineIndex++;
        }

    while(lineIndex > 0)
        {
        lineIndex--;
        if (lines[lineIndex])
            {
            free(lines[lineIndex]);
            }
        }

    return 0;
    }



Does that make sense?


这篇关于从文件中读取一行并分配给缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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