LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   truncate lines with c (https://www.linuxquestions.org/questions/programming-9/truncate-lines-with-c-355681/)

alaios 08-21-2005 11:25 PM

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 :)

bigearsbilly 08-22-2005 04:19 AM

homework? ;)

alaios 08-22-2005 05:15 AM

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...

bigearsbilly 08-22-2005 05:48 AM

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
so, truncate the first line just copy the string after the 1st \n.
Truncate the last line, replace the last \n with '\0'

blank lines are \n\n.


use something like 'strtok' maybe

alaios 08-22-2005 07:41 AM

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);
        buffer_for_process=strchr(buffer,'\n');
        printf("After strtok  call  %s \n",buffer_for_process);
        buffer_for_process=strchr(buffer_for_process,'\n');
        printf("After strtok call %s \n",buffer_for_process);

This prints the following
Code:

To buffer variable has section1]
parameter1=value1
parameter2=value2

[
After strtok has
parameter1=value1
parameter2=value2

[
After strtok has
parameter1=value1
parameter2=value2

[

As u can see the second time the strtok function isnt able
to remove the \n character...
Why?

bigearsbilly 08-22-2005 08:04 AM

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:



        printf("To buffer variable has:\n%s \n",buffer);
        buffer_for_process=strchr(buffer,'\n');
        ++buffer_for_process;

        printf("After strtok  call:\n%s \n",buffer_for_process);

        buffer_for_process=strchr(buffer_for_process,'\n');
        ++buffer_for_process;

        printf("After strtok  call:\n%s \n",buffer_for_process);
}


alaios 08-22-2005 01:57 PM

Thx a lot... but do u know why this
buffer_for_process+=strchr(buffer_for_process,'\n');
doesnt work?

bigearsbilly 08-23-2005 03:24 AM

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!

alaios 08-24-2005 09:12 AM

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?

bigearsbilly 08-24-2005 10:33 AM

you got strtok round the wrong way...
Quote:

strtok("a",buffer);
char *strtok(char *SOURCE, const char *DELIMITERS)
Code:

#include <stdio.h>


char  buffer[] = "line1\n\
line2 (line 3 is blank)\n\
\n\
line4";

main() {
    char *p;

    puts("BEFORE:");
    puts (buffer);

    puts("AFTER:");
    if (p=strtok(buffer, "\n")) {
    puts(p);
    }

    while (p = strtok(NULL, "\n")){
      puts(p);
    }
 
}

Code:

BEFORE:
line1
line2 (line 3 is blank)

line4
AFTER:
line1
line2 (line 3 is blank)
line4

a blank line is "\n\n"

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 ;)

alaios 08-24-2005 03:02 PM

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

alaios 08-24-2005 09:56 PM

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

bigearsbilly 08-25-2005 03:31 AM

Works for me on solaris and cygwin winXP ?

Quote:

puts.c:16: warning: assignment makes pointer from integer without a cast
puts.c:20: warning: assignment makes pointer from integer without a cast

oops, cos i forgot to: #include <string.h>
try it now.



Quote:

Do u know alternative ways for removing blank lines?
why C? Fun? Homework?
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

alaios 08-25-2005 04:15 AM

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?

bigearsbilly 08-25-2005 04:49 AM

Try this for getting rid of blanks:

Code:

#include <stdio.h>
#include <string.h>

#define BUFSZ 1024

int do_it(char * buffer) {

    char *p;


    if (p=strtok(buffer, "\n")) {
    puts(p);
    }


    while (p = strtok(NULL, "\n")){
        puts(p);
    }






}






int main (void)
{
    char buffer[BUFSZ];

    while (fgets(buffer, (sizeof buffer) -1, stdin)) {

        do_it(buffer);

    }

}

result on itself:
Code:

$ 1 < 1.c
#include <stdio.h>
#include <string.h>
#define BUFSZ 1024
int do_it(char * buffer) {
    char *p;
    if (p=strtok(buffer, "\n")) {
    puts(p);
    }
    while (p = strtok(NULL, "\n")){
        puts(p);
    }
}
int main (void)
{
    char buffer[BUFSZ];
    while (fgets(buffer, (sizeof buffer) -1, stdin)) {
        do_it(buffer);
    }
}
$



All times are GMT -5. The time now is 12:38 AM.