truncate lines with c
Good morning :)
I have the following string that contains the following text string='This is my text and this is only sample for this post ]'; How i can truncate the first and last line using c? and how i can truncate the empty lines? I search in string.h but cant find anything appropriate Thx :) |
homework? ;)
|
no not a homework... i just want the function for that....
I have searhed thorougly in the string.h nbut couldnt find anythinh... All the morning i am googling for this issue but still cant find anything... |
well, as you know lines are delimited by the \n character.
Code:
This is my text\nand this\n\nis only sample\nfor this post\n\n Truncate the last line, replace the last \n with '\0' blank lines are \n\n. use something like 'strtok' maybe |
thx for your answer
Thx a lot for your answer
Let me show u what i have tried so far so u can help me little more Look the following code Code:
printf("To buffer variable has %s \n",buffer); Code:
To buffer variable has section1] to remove the \n character... Why? |
well, strchr is pointing at the first '\n'.
When you call it again it points to the same character again. You need to increment past it first.... Code:
|
Thx a lot... but do u know why this
buffer_for_process+=strchr(buffer_for_process,'\n'); doesnt work? |
i suppose because strchr gives the address of the char, not the offset.
so if you have buffer="blahblah\nblah" and buffer's adress is at 100 then b=strchr(buffer, '\n') will be 108. so buffer + b will be 208 (way off the end). when what you need is b+1: 109 to go past the newline. I hope that says it! |
Thx i need now sth more tricky... I am trying to remove the empty lines
I have tried to search for empty lines using strtok but witho no success printf("The buffer is %s \n",buffer); tempbuffer=strtok("a",buffer); printf("Tempbuffer is %s \n",tempbuffer); but after execution i get this The buffer is parameter1=value1 parameter2=value2 [ Tempbuffer is (null) How i can detect empty lines? |
you got strtok round the wrong way...
Quote:
Code:
#include <stdio.h> Code:
BEFORE: strtok ( buff , "\n\" ); while (strtok(NULL, "\n"); So if subsequent calls to strtok are NULL that means you've hit a blank line, so skip it. the trouble with strtok is it modifies the buffer. more for tokenisation. Maybe not best for splitting lines if a long file. read the man page thoroughly ;) |
Yea its a long long file.. What else should i use?
I have read the function inside string.h Most of them dont take into account the \n\n character.. Do u know alternative ways for removing blank lines? Thx a lot for your rime |
Thx a lot but the code doesnt seem to work for me
gcc -o puts puts.c puts.c: In function `main': puts.c:16: warning: assignment makes pointer from integer without a cast puts.c:20: warning: assignment makes pointer from integer without a cast alaios@taksideytis:~/code$ ./puts BEFORE: line1 line2 (line 3 is blank) line4 AFTER: line1 line2 (line 3 is blank) line4 |
Works for me on solaris and cygwin winXP ?
Quote:
try it now. Quote:
Is it just a filter? (i.e. just takes a file and prints out after making changes) if I was doing it I'd filter the file first: grep . file |
Not homework just project... i need to take a file and parse it.....
And parsing means remove lines that u dont want removes blank lines removes comments and then search for the variables and their values.....so do u have sth else in mind? |
Try this for getting rid of blanks:
Code:
#include <stdio.h> Code:
$ 1 < 1.c |
All times are GMT -5. The time now is 12:38 AM. |