Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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
Posted
Comments
Sergey Alexandrovich Kryukov 31-Dec-13 2:36am    
What's the problem?
—SA

1 solution

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:
C++
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:
C++
int lineIndex = 0;
lets us know what line we are doing each time, so then:
C++
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.
C++
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:
C++
while(lineIndex > 0)
   {
   lineIndex--;
   if (lines[lineIndex])
      {
      free(lines[lineIndex]);
      }
   }


So what we end up with is:
C++
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?
 
Share this answer
 
Comments
shan bala 4-Jan-14 1:52am    
hi Griff,

thanks a lot, It really works, i just modify it according to my application
Thanks,
shan
OriginalGriff 4-Jan-14 2:01am    
You're welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900