-
simple string editing
I would like to know how to print characters after ')' has been read... I need this to extract the data from a file that contains 100's of .pls files which are used for connecting to web streams, here's what 1 .pls file looks like...
[playlist]
numberofentries=3
File1=http://64.236.34.97:80/stream/1065
Title1=(#1 - 871/18508) D I G I T A L L Y - I M P O R T E D - Vocal Trance - a fusion of trance, dance, and chilling vocals
Length1=-1
File2=http://64.236.34.67:80/stream/1065
Title2=(#2 - 719/14952) D I G I T A L L Y - I M P O R T E D - Vocal Trance - a fusion of trance, dance, and chilling vocals
Length2=-1
File3=http://64.236.34.4:80/stream/1065
Title3=(#3 - 842/16389) D I G I T A L L Y - I M P O R T E D - Vocal Trance - a fusion of trance, dance, and chilling vocals
Length3=-1
Version=2
What i want to get is everything after the Title1=(#1 - 871/18508)
At first I had thought of using strncpy to do this. but if the number of characters in parenthesis changes, that causes a problem.
I also would like to take the address from this by grabbing everything after File1=
I am assuming this is exactly the same as taking everything after ')'
Thank you very much
-
You can use strchr() to find a specific character within a string.
-
I am having a hard time understanding how to get the output i want.
I want to say this...
Code:
temp_var = strchr(string, "=");
however what do I declare temp_var as? and how would I print the output. strchr returns a pointer. that is what is confuseing me.
-
Another possible approach...
Code:
#include <stdio.h>
int main(void)
{
static const char filename[] = "file.txt";
FILE *file = fopen(filename, "r");
if ( file != NULL )
{
int ch;
while ( (ch = getc(file)) != EOF )
{
if ( ch == ')' )
{
do {
ch = getc(file);
if ( ch == EOF )
{
goto done;
}
putchar(ch);
} while ( ch != '\n' );
}
}
done:
fclose(file);
}
else
{
perror(filename);
}
return 0;
}
/* file.txt
[playlist]
numberofentries=3
File1=http://64.236.34.97:80/stream/1065
Title1=(#1 - 871/18508) D I G I T A L L Y - I M P O R T E D - Vocal Trance - a fusion of trance, dance, and chilling vocals
Length1=-1
File2=http://64.236.34.67:80/stream/1065
Title2=(#2 - 719/14952) D I G I T A L L Y - I M P O R T E D - Vocal Trance - a fusion of trance, dance, and chilling vocals
Length2=-1
File3=http://64.236.34.4:80/stream/1065
Title3=(#3 - 842/16389) D I G I T A L L Y - I M P O R T E D - Vocal Trance - a fusion of trance, dance, and chilling vocals
Length3=-1
Version=2
*/
/* my output
D I G I T A L L Y - I M P O R T E D - Vocal Trance - a fusion of trance, dance, and chilling vocals
D I G I T A L L Y - I M P O R T E D - Vocal Trance - a fusion of trance, dance, and chilling vocals
D I G I T A L L Y - I M P O R T E D - Vocal Trance - a fusion of trance, dance, and chilling vocals
*/
-
Code:
while ( (ch = getc(file)) != EOF )
{
if ( ch == ')' )
{
do {
ch = getc(file);
if ( ch == EOF )
{
goto done;
}
putchar(ch);
} while ( ch != '\n' );
}
}
I guess it would not be a good idea to use goto.. instead u can use a break
Yes you will have to adjust the getc(file)..
instead of using it in the while loop you can have it as a statement before it ..
and in the while loop you can just compare (ch!=EOF)
The code given by dave is gr8.. i'm just giving another way of solving the problem..