LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 08-22-2005, 12:25 AM   #1
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,133

Rep: Reputation: 45
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
 
Old 08-22-2005, 05:19 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
homework?
 
Old 08-22-2005, 06:15 AM   #3
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,133

Original Poster
Rep: Reputation: 45
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...
 
Old 08-22-2005, 06:48 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
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
 
Old 08-22-2005, 08:41 AM   #5
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,133

Original Poster
Rep: Reputation: 45
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?
 
Old 08-22-2005, 09:04 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
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);
}
 
Old 08-22-2005, 02:57 PM   #7
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,133

Original Poster
Rep: Reputation: 45
Thx a lot... but do u know why this
buffer_for_process+=strchr(buffer_for_process,'\n');
doesnt work?
 
Old 08-23-2005, 04:24 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
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!
 
Old 08-24-2005, 10:12 AM   #9
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,133

Original Poster
Rep: Reputation: 45
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?
 
Old 08-24-2005, 11:33 AM   #10
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
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
 
Old 08-24-2005, 04:02 PM   #11
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,133

Original Poster
Rep: Reputation: 45
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
 
Old 08-24-2005, 10:56 PM   #12
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,133

Original Poster
Rep: Reputation: 45
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
 
Old 08-25-2005, 04:31 AM   #13
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
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
 
Old 08-25-2005, 05:15 AM   #14
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,133

Original Poster
Rep: Reputation: 45
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?
 
Old 08-25-2005, 05:49 AM   #15
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
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);
    }
}
$
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
printer printing vertical lines at beginning and end of lines makhand Linux - Hardware 0 09-02-2005 03:03 PM
truncate the first character alaios Linux - General 1 02-10-2005 05:55 AM
Should I truncate an inode? akurtis Red Hat 0 08-02-2004 10:34 PM
how to use truncate sureshbabu Linux - Newbie 1 10-29-2003 05:22 AM
truncate command butthead Linux - General 1 05-14-2002 04:40 AM


All times are GMT -5. The time now is 01:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration