'getline' questions


 
Thread Tools Search this Thread
Top Forums Programming 'getline' questions
# 1  
Old 07-08-2008
'getline' questions

I have a C program I've written that includes a 'getline' call that is used to read a line at a time from a file and process the information in each line. A potential problem comes up since I don't know the maximum length of each line; right now I'm using a constant of 256, but that might not be long enough when run in the real world. The description of getline says that it will 'realloc' the string if it's not long enough, so I've tried running the program lowering the aforementioned constant to a value less than most of the lines in my input file and passing that value into 'getline' as the 2nd parameter 'n'. When I run it using the 'ddd' debugger, I get a segmentation fault after a few lines had been read on a 'free' call. I also ran it outside of 'ddd' with 'valgrind' and the program runs all the way through with no seg fault, though it says that I "definitely lost" a little over a hundred bytes, something that didn't happen before. I wonder if I should just set the char pointer to NULL and 'n' to zero and let getline do all the allocation, though passing it an already-allocated string that's not long enough should work, too, according to the man page. I'm also unclear about the purpose of the 'n' parameter and what the value is that's returned there; it returns the number of bytes read, but not through that parameter. Any words of wisdom would be appreciated.
# 2  
Old 07-08-2008
Kinda hard to tell why its segfaulting without seeing any code. Make sure you are passing a **buf not a *buf.

But as far as the 'n' parameter its there so you can pass in the size of the buffer that you pre-allocated. It will contain the size of the buffer you get back. Thus if you passed in a buffer too small it will have the new size. If you passed in null, it will have the size that was allocated for it.

The return value of the function returns characters read (not necessarily size of the buffer) not counting the last null byte if there.
# 3  
Old 07-08-2008
Thanks for the reply. I think I know why the segfault happened, having to do with the logic of my program and the fact that I changed a bit of it to try giving 'getline' a smaller character string; I won't bore you with all the details.

While looping through a file, reading one line at a time, I should just need to call 'free' once, after the loop is through, correct? It's doing a 'realloc' when it needs more room, and not allocating a whole new string each time. I'm wondering about how much more space it reallocs when it's necessary. I just ran my program again checking the return value of 'n', size of buffer returned, and the value of bytes that were read (my initial value of the size of line is 25, just picking one that would be too small most of the time):

bytes read size of buffer returned
7 25
56 57
36 57
97 114
70 114
23 114
39 114

Apparently, once the buffer being passed back gets a certain size, it won't get reallocated to a smaller size, what with it staying at 114. I'm wondering why when the line was 96 bytes it didn't return a buffer of size 97 (allowing for the null) instead of jumping to 114.

Thanks again.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk with if, getline, and another if

Howdy Folks, It seems like it is always awk that confuses the heck out of me and I even have books and examples. I have this line: awk '{if (/clientIP/)(SRV = $NF); if ($2 ~ /BUNDLE-GIM/) getline; if ($2 ~ /r100595/) {print SRV,"BUNDLE-GIM",$2}}' post.txt to parse this text: <api... (4 Replies)
Discussion started by: port43
4 Replies

2. Shell Programming and Scripting

awk getline

Hi, I have an awk script with the following function in it . function cmd( c ) { while( ( c | getline foo) > 0 ){ return foo ; close( c ); } } c =... (4 Replies)
Discussion started by: MetaMan
4 Replies

3. Shell Programming and Scripting

nawk getline

I'm running the script below and get the output below against a file with lineA=aaa lineB=bbb lineC=ccc lineD=ddd I get output: lineC=ccc lineD=ddd I need the output to be: lineB=bbb lineC=ccc lineD=ddd cat filename | nawk '/lineA=aaa/ { getline; do { getline (3 Replies)
Discussion started by: toor13
3 Replies

4. Programming

getline()

I can not get 'getline()' to compile. I have tried. string curLine; //= compiler error char* curLine; //=compiler error char curLine; //=compiler error Every example I see uses a string as a getline(); parameter. It does not work for me on Fedora14 with gcc-c++. Thank you so much. This... (1 Reply)
Discussion started by: sepoto
1 Replies

5. Homework & Coursework Questions

Print questions from a questions folder in a sequential order

1.) I am to write scripts that will be phasetest folder in the home directory. 2.) The folder should have a set-up,phase and display files I have written a small script which i used to check for the existing users and their password. What I need help with: I have a set of questions in a... (19 Replies)
Discussion started by: moraks007
19 Replies

6. Shell Programming and Scripting

Using getline in awk

I am using awk and want to use getline from a file like below getline x < file However file consists of two columns and I only want to store $2 Any way I can do this? ---------- Post updated at 06:54 AM ---------- Previous update was at 06:45 AM ---------- Done something like this.... (1 Reply)
Discussion started by: kristinu
1 Replies

7. Shell Programming and Scripting

awk getline

How do you make the getline function return to the original line? The example below should make it clear where I am currently going wrong. Thanks AWK SCRIPT: ------------- awk -F '-' '{ tmpLine = "EMPTY" print "CURRENT LINE :"$0 getline tmpLine print "NEXT LINE :"tmpLine }'... (1 Reply)
Discussion started by: garethsays
1 Replies

8. Shell Programming and Scripting

awk getline help maybe?

hello collegues, I am attempting to use awk to search file1 (serverlist.csv) from each row with file2 (supported.txt). If the is no entry exists in serverlist then output to a file called notsupp.out if there is an entry output to supp.out I can do this with basic shell scripting however... (0 Replies)
Discussion started by: chlawren
0 Replies

9. UNIX for Dummies Questions & Answers

utility of getline here?

hi , I got a script that I don't understand. awk '{ command="echo "$1 command | getline echome close(command) print $0 "\t" echome }' < user.list It reads the file user.list user.list London John Bridge Peter and sends the stream to the awk command. I don't understand what is... (3 Replies)
Discussion started by: remid1985
3 Replies

10. Shell Programming and Scripting

getline with a unique

I have an inputfile that I trying to awk with a getline(Solaris v8 ksh). However I need to have the results be a unique. Is there a comparable command within awk to return a unique result? Or can you break out of the awk to add a uniq. I am looking for something more complex then a pipe... (6 Replies)
Discussion started by: gozer13
6 Replies
Login or Register to Ask a Question