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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
|
08-21-2005, 11:25 PM
|
#1
|
|
Senior Member
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,109
Rep:
|
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 
|
|
|
|
08-22-2005, 04:19 AM
|
#2
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
homework? 
|
|
|
|
08-22-2005, 05:15 AM
|
#3
|
|
Senior Member
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,109
Original Poster
Rep:
|
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...
|
|
|
|
08-22-2005, 05:48 AM
|
#4
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
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
|
|
|
|
08-22-2005, 07:41 AM
|
#5
|
|
Senior Member
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,109
Original Poster
Rep:
|
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?
|
|
|
|
08-22-2005, 08:04 AM
|
#6
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
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);
}
|
|
|
|
08-22-2005, 01:57 PM
|
#7
|
|
Senior Member
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,109
Original Poster
Rep:
|
Thx a lot... but do u know why this
buffer_for_process+=strchr(buffer_for_process,'\n');
doesnt work?
|
|
|
|
08-23-2005, 03:24 AM
|
#8
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
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!
|
|
|
|
08-24-2005, 09:12 AM
|
#9
|
|
Senior Member
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,109
Original Poster
Rep:
|
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?
|
|
|
|
08-24-2005, 10:33 AM
|
#10
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
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 
|
|
|
|
08-24-2005, 03:02 PM
|
#11
|
|
Senior Member
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,109
Original Poster
Rep:
|
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
|
|
|
|
08-24-2005, 09:56 PM
|
#12
|
|
Senior Member
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,109
Original Poster
Rep:
|
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
|
|
|
|
08-25-2005, 03:31 AM
|
#13
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
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
|
|
|
|
08-25-2005, 04:15 AM
|
#14
|
|
Senior Member
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,109
Original Poster
Rep:
|
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?
|
|
|
|
08-25-2005, 04:49 AM
|
#15
|
|
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,211
Rep: 
|
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);
}
}
$
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 02:54 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|