-
October 11th, 2012, 05:47 AM
#1
I wish to replace spaces with underscores in a sting
Hello experts
maybe this is simple for you,
I have the following function.
void replacestring() {
quality = quality->Replace(" ", "_");
}
I wish to replace spaces with underscores in the string "quality"
When I compile I get the error message:
config_files.c: In function ‘replacestring’:
config_files.c:79: error: request for member ‘Replace’ in something not a structure or union
Here is the code,
config_files..c
-
October 11th, 2012, 07:15 AM
#2
Re: I wish to replace spaces with underscores in a sting
There is no 'replacestring' in the file you posted.
Post a minimum example here that reproduces the problem. Do not attach files, post the code using code tags.
-
October 11th, 2012, 08:18 AM
#3
Re: I wish to replace spaces with underscores in a sting
thankyou so much for your reply, sorry I send the wrong version of this file
here is the code;
HTML Code:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <libintl.h>
#ifndef __GTK_H__
#include <gtk/gtk.h>
#endif
#ifndef __GLOBAL_H__
#include "global.h"
#endif
#ifndef __CONSTANTS_H__
#include "constants.h"
#endif
#ifndef __CONFIG_FILES_H__
#include "config_files.h"
#endif
gboolean darkice_config_store(char opt, char *save_path) {
int i;
char adddate;
char *bitrate;
char *bitratemode;
char *samplerate;
char *quality;
char *bitspersample;
char *channel;
char *buffersize;
char *device;
FILE *f_darkice_cfg;
adddate = (char ) (gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON (checkbutton_record) ))? '1': '0';
bitrate = (char *) gtk_entry_get_text ( GTK_ENTRY(GTK_COMBO(combo_bitrate)->entry));
bitratemode = (char *) gtk_entry_get_text ( GTK_ENTRY(GTK_COMBO(combo_bitratemode)->entry));
samplerate = (char *) gtk_entry_get_text ( GTK_ENTRY(GTK_COMBO(combo_samplerate)->entry));
quality = (char *) gtk_entry_get_text ( GTK_ENTRY(GTK_COMBO(combo_quality)->entry));
bitspersample = (char *) gtk_entry_get_text ( GTK_ENTRY(GTK_COMBO(combo_bitspersample)->entry));
channel = (char *) gtk_entry_get_text ( GTK_ENTRY(GTK_COMBO(combo_channel)->entry));
buffersize = (char *) gtk_entry_get_text ( GTK_ENTRY(GTK_COMBO(combo_buffersize)->entry));
device = (char *) gtk_entry_get_text ( GTK_ENTRY(GTK_COMBO(combo_device)->entry));
if (!( f_darkice_cfg = (opt != 0)? fopen(darkice_cfg, "w") : fopen(save_path, "w"))) {
printf("Error: Cannot open %s\n", (opt != 0)? darkice_cfg: save_path);
return FALSE;
}
/* for compatibility issues, mountpoint can start with slash */
i = 0;
void replacestring() {
quality = quality->Replace(" ", "_");
}
/* Entry writes */
fprintf(f_darkice_cfg, "[general]");
fprintf(f_darkice_cfg, "\nduration = ");
fprintf(f_darkice_cfg, "0"); /* duration of encoding, in seconds. 0 means forever */
fprintf(f_darkice_cfg, "\nbufferSecs = ");
fprintf(f_darkice_cfg, "%s", buffersize); /* size of internal slip buffer, in seconds */
fprintf(f_darkice_cfg, "\nreconnect = ");
fprintf(f_darkice_cfg, "%s", "yes");
fprintf(f_darkice_cfg, "\n\n[input]");
fprintf(f_darkice_cfg, "\ndevice = ");
fprintf(f_darkice_cfg, "%s", "ameter"); /* OSS, ALSA or JACK */
fprintf(f_darkice_cfg, "\nsampleRate = ");
fprintf(f_darkice_cfg, "%s", samplerate); /* sample rate in Hz. try 11025, 22050 or 44100 */
fprintf(f_darkice_cfg, "\nbitsPerSample = ");
fprintf(f_darkice_cfg, "%s", bitspersample); /* bits per sample. try 16 */
fprintf(f_darkice_cfg, "\nchannel = "); /* channels. 1 = mono, 2 = stereo */
if (!strcmp(channel, "1 - Mono"))
fprintf(f_darkice_cfg, "1");
else
fprintf(f_darkice_cfg, "2");
fprintf(f_darkice_cfg, "\n\n[icecast2-0]"); /* icecast 2 version server */
/* write bitratemode */
fprintf(f_darkice_cfg, "\nbitrateMode = ");
if (!strcmp( gettext("Constant"), bitratemode))
fprintf(f_darkice_cfg, "cbr"); /* Constant bitrate mode */
else
if (!strcmp( gettext("Average"), bitratemode))
fprintf(f_darkice_cfg, "abr"); /* average bitrate mode */
else
if (!strcmp( gettext("Variable"), bitratemode))
fprintf(f_darkice_cfg, "vbr"); /* variable bitrate mode */
fprintf(f_darkice_cfg, "\nformat = ");
fprintf(f_darkice_cfg, "%s", "aacp"); /* bitrate of the stream sent to the server */
fprintf(f_darkice_cfg, "\nbitrate = ");
fprintf(f_darkice_cfg, "%s", bitrate); /* bitrate of the stream sent to the server */
fprintf(f_darkice_cfg, "\nquality = ");
fprintf(f_darkice_cfg, "%s", "1.0"); /* encoding quality */
fprintf(f_darkice_cfg, "\nserver = ");
fprintf(f_darkice_cfg, "%s", "127.0.0.1");
fprintf(f_darkice_cfg, "\nport = ");
fprintf(f_darkice_cfg, "%s", "8000"); /* port of the IceCast2 server, usually 8000 */
fprintf(f_darkice_cfg, "\npassword = ");
fprintf(f_darkice_cfg, "%s", "hackme"); /* source password to the IceCast2 server */
fprintf(f_darkice_cfg, "\nmountPoint = ");
fprintf(f_darkice_cfg, "%s", quality); /* mount point of this stream on the IceCast2 server */
fprintf(f_darkice_cfg, "%s", ".aac");
fprintf(f_darkice_cfg, "\nname = ");
fprintf(f_darkice_cfg, "%s", quality); /* name of the stream */
fprintf(f_darkice_cfg, "\ndescription = ");
fprintf(f_darkice_cfg, "%s", "TautokoFM 97.5 & 99.5"); /* description of the stream */
fprintf(f_darkice_cfg, "\nurl = ");
fprintf(f_darkice_cfg, "%s", ""); /* URL related to the stream */
fprintf(f_darkice_cfg, "\ngenre = ");
fprintf(f_darkice_cfg, "%s", ""); /* genre of the stream */
fprintf(f_darkice_cfg, "\npublic = ");
fprintf(f_darkice_cfg, "%s", "yes");
if (adddate == '1') {
fprintf(f_darkice_cfg, "\nlocalDumpFile = ");
fprintf(f_darkice_cfg, "%s", "/home/user/Recorded_Shows/");
fprintf(f_darkice_cfg, "%s", quality);
fprintf(f_darkice_cfg, "%s", ".aac");
fprintf(f_darkice_cfg, "\nfileAddDate = ");
fprintf(f_darkice_cfg, "%s", "yes");
}
fprintf(f_darkice_cfg, "\n");
fclose (f_darkice_cfg);
FILE *f_ameter_snd;
if (!( f_ameter_snd = (opt != 0)? fopen(ameter_snd, "w") : fopen(save_path, "w"))) {
printf("Error: Cannot open %s\n", (opt != 0)? ameter_snd: save_path);
return FALSE;
}
/* print ~/.asoundrc for ameter */
fprintf(f_ameter_snd, "pcm_scope.ameter {");
fprintf(f_ameter_snd, "\n type ameter");
fprintf(f_ameter_snd, "\n}");
fprintf(f_ameter_snd, "\npcm_scope_type.ameter {");
fprintf(f_ameter_snd, "\n lib /usr/lib/libameter.so");
fprintf(f_ameter_snd, "\n}");
fprintf(f_ameter_snd, "\npcm.ameter {");
fprintf(f_ameter_snd, "\n type meter");
fprintf(f_ameter_snd, "\n slave.pcm '");
fprintf(f_ameter_snd, "%s", device);
fprintf(f_ameter_snd, "%s", "'");
fprintf(f_ameter_snd, "\n scopes.0 ameter");
fprintf(f_ameter_snd, "\n}");
fprintf(f_ameter_snd, "\npcm.dsp0 ameter");
fprintf(f_ameter_snd, "\n");
fclose (f_ameter_snd);
FILE *f_add2;
if (!( f_add2 = (opt != 0)? fopen(add2, "w") : fopen(save_path, "w"))) {
printf("Error: Cannot open %s\n", (opt != 0)? add2: save_path);
return FALSE;
}
/* print /Stuff/add2.txt */
fprintf(f_add2, ":8000/");
fprintf(f_add2, quality);
fprintf(f_add2, ".aac");
fprintf(f_add2, "\n");
fclose (f_add2);
FILE *f_name1;
if (!( f_name1 = (opt != 0)? fopen(name1, "w") : fopen(save_path, "w"))) {
printf("Error: Cannot open %s\n", (opt != 0)? name1: save_path);
return FALSE;
}
/* print /Stuff/name1 */
fprintf(f_name1, quality);
fprintf(f_name1, "\n");
fclose (f_name1);
return TRUE;
}
Last edited by n_techo; October 12th, 2012 at 06:18 AM.
-
October 11th, 2012, 08:33 AM
#4
Re: I wish to replace spaces with underscores in a string
Sorry about that I send you the wrong version of this file
here is the code again.
Code:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <libintl.h>
#ifndef __GTK_H__
#include <gtk/gtk.h>
#endif
#ifndef __GLOBAL_H__
#include "global.h"
#endif
#ifndef __CONSTANTS_H__
#include "constants.h"
#endif
#ifndef __CONFIG_FILES_H__
#include "config_files.h"
#endif
gboolean darkice_config_store(char opt, char *save_path) {
int i;
char adddate;
char *bitrate;
char *bitratemode;
char *samplerate;
char *quality;
char *bitspersample;
char *channel;
char *buffersize;
char *device;
FILE *f_darkice_cfg;
adddate = (char ) (gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON (checkbutton_record) ))? '1': '0';
bitrate = (char *) gtk_entry_get_text ( GTK_ENTRY(GTK_COMBO(combo_bitrate)->entry));
bitratemode = (char *) gtk_entry_get_text ( GTK_ENTRY(GTK_COMBO(combo_bitratemode)->entry));
samplerate = (char *) gtk_entry_get_text ( GTK_ENTRY(GTK_COMBO(combo_samplerate)->entry));
quality = (char *) gtk_entry_get_text ( GTK_ENTRY(GTK_COMBO(combo_quality)->entry));
bitspersample = (char *) gtk_entry_get_text ( GTK_ENTRY(GTK_COMBO(combo_bitspersample)->entry));
channel = (char *) gtk_entry_get_text ( GTK_ENTRY(GTK_COMBO(combo_channel)->entry));
buffersize = (char *) gtk_entry_get_text ( GTK_ENTRY(GTK_COMBO(combo_buffersize)->entry));
device = (char *) gtk_entry_get_text ( GTK_ENTRY(GTK_COMBO(combo_device)->entry));
if (!( f_darkice_cfg = (opt != 0)? fopen(darkice_cfg, "w") : fopen(save_path, "w"))) {
printf("Error: Cannot open %s\n", (opt != 0)? darkice_cfg: save_path);
return FALSE;
}
/* for compatibility issues, mountpoint can start with slash */
i = 0;
void replacestring() {
quality = quality->Replace(" ", "_");
}
/* Entry writes */
fprintf(f_darkice_cfg, "[general]");
fprintf(f_darkice_cfg, "\nduration = ");
fprintf(f_darkice_cfg, "0"); /* duration of encoding, in seconds. 0 means forever */
fprintf(f_darkice_cfg, "\nbufferSecs = ");
fprintf(f_darkice_cfg, "%s", buffersize); /* size of internal slip buffer, in seconds */
fprintf(f_darkice_cfg, "\nreconnect = ");
fprintf(f_darkice_cfg, "%s", "yes");
fprintf(f_darkice_cfg, "\n\n[input]");
fprintf(f_darkice_cfg, "\ndevice = ");
fprintf(f_darkice_cfg, "%s", "ameter"); /* OSS, ALSA or JACK */
fprintf(f_darkice_cfg, "\nsampleRate = ");
fprintf(f_darkice_cfg, "%s", samplerate); /* sample rate in Hz. try 11025, 22050 or 44100 */
fprintf(f_darkice_cfg, "\nbitsPerSample = ");
fprintf(f_darkice_cfg, "%s", bitspersample); /* bits per sample. try 16 */
fprintf(f_darkice_cfg, "\nchannel = "); /* channels. 1 = mono, 2 = stereo */
if (!strcmp(channel, "1 - Mono"))
fprintf(f_darkice_cfg, "1");
else
fprintf(f_darkice_cfg, "2");
fprintf(f_darkice_cfg, "\n\n[icecast2-0]"); /* icecast 2 version server */
/* write bitratemode */
fprintf(f_darkice_cfg, "\nbitrateMode = ");
if (!strcmp( gettext("Constant"), bitratemode))
fprintf(f_darkice_cfg, "cbr"); /* Constant bitrate mode */
else
if (!strcmp( gettext("Average"), bitratemode))
fprintf(f_darkice_cfg, "abr"); /* average bitrate mode */
else
if (!strcmp( gettext("Variable"), bitratemode))
fprintf(f_darkice_cfg, "vbr"); /* variable bitrate mode */
fprintf(f_darkice_cfg, "\nformat = ");
fprintf(f_darkice_cfg, "%s", "aacp"); /* bitrate of the stream sent to the server */
fprintf(f_darkice_cfg, "\nbitrate = ");
fprintf(f_darkice_cfg, "%s", bitrate); /* bitrate of the stream sent to the server */
fprintf(f_darkice_cfg, "\nquality = ");
fprintf(f_darkice_cfg, "%s", "1.0"); /* encoding quality */
fprintf(f_darkice_cfg, "\nserver = ");
fprintf(f_darkice_cfg, "%s", "127.0.0.1");
fprintf(f_darkice_cfg, "\nport = ");
fprintf(f_darkice_cfg, "%s", "8000"); /* port of the IceCast2 server, usually 8000 */
fprintf(f_darkice_cfg, "\npassword = ");
fprintf(f_darkice_cfg, "%s", "hackme"); /* source password to the IceCast2 server */
fprintf(f_darkice_cfg, "\nmountPoint = ");
fprintf(f_darkice_cfg, "%s", quality); /* mount point of this stream on the IceCast2 server */
fprintf(f_darkice_cfg, "%s", ".aac");
fprintf(f_darkice_cfg, "\nname = ");
fprintf(f_darkice_cfg, "%s", quality); /* name of the stream */
fprintf(f_darkice_cfg, "\ndescription = ");
fprintf(f_darkice_cfg, "%s", "TautokoFM 97.5 & 99.5"); /* description of the stream */
fprintf(f_darkice_cfg, "\nurl = ");
fprintf(f_darkice_cfg, "%s", ""); /* URL related to the stream */
fprintf(f_darkice_cfg, "\ngenre = ");
fprintf(f_darkice_cfg, "%s", ""); /* genre of the stream */
fprintf(f_darkice_cfg, "\npublic = ");
fprintf(f_darkice_cfg, "%s", "yes");
if (adddate == '1') {
fprintf(f_darkice_cfg, "\nlocalDumpFile = ");
fprintf(f_darkice_cfg, "%s", "/home/user/Recorded_Shows/");
fprintf(f_darkice_cfg, "%s", quality);
fprintf(f_darkice_cfg, "%s", ".aac");
fprintf(f_darkice_cfg, "\nfileAddDate = ");
fprintf(f_darkice_cfg, "%s", "yes");
}
fprintf(f_darkice_cfg, "\n");
fclose (f_darkice_cfg);
FILE *f_ameter_snd;
if (!( f_ameter_snd = (opt != 0)? fopen(ameter_snd, "w") : fopen(save_path, "w"))) {
printf("Error: Cannot open %s\n", (opt != 0)? ameter_snd: save_path);
return FALSE;
}
/* print ~/.asoundrc for ameter */
fprintf(f_ameter_snd, "pcm_scope.ameter {");
fprintf(f_ameter_snd, "\n type ameter");
fprintf(f_ameter_snd, "\n}");
fprintf(f_ameter_snd, "\npcm_scope_type.ameter {");
fprintf(f_ameter_snd, "\n lib /usr/lib/libameter.so");
fprintf(f_ameter_snd, "\n}");
fprintf(f_ameter_snd, "\npcm.ameter {");
fprintf(f_ameter_snd, "\n type meter");
fprintf(f_ameter_snd, "\n slave.pcm '");
fprintf(f_ameter_snd, "%s", device);
fprintf(f_ameter_snd, "%s", "'");
fprintf(f_ameter_snd, "\n scopes.0 ameter");
fprintf(f_ameter_snd, "\n}");
fprintf(f_ameter_snd, "\npcm.dsp0 ameter");
fprintf(f_ameter_snd, "\n");
fclose (f_ameter_snd);
FILE *f_add2;
if (!( f_add2 = (opt != 0)? fopen(add2, "w") : fopen(save_path, "w"))) {
printf("Error: Cannot open %s\n", (opt != 0)? add2: save_path);
return FALSE;
}
/* print /Stuff/add2.txt */
fprintf(f_add2, ":8000/");
fprintf(f_add2, quality);
fprintf(f_add2, ".aac");
fprintf(f_add2, "\n");
fclose (f_add2);
FILE *f_name1;
if (!( f_name1 = (opt != 0)? fopen(name1, "w") : fopen(save_path, "w"))) {
printf("Error: Cannot open %s\n", (opt != 0)? name1: save_path);
return FALSE;
}
/* print /Stuff/name1 */
fprintf(f_name1, quality);
fprintf(f_name1, "\n");
fclose (f_name1);
return TRUE;
}
-
October 11th, 2012, 08:57 AM
#5
Re: I wish to replace spaces with underscores in a sting
I found the solution and am posting here for other people if they have the same problem, I needed to replace spaces with underscores, the program uses a gtkcombo box, where you can enter, a name for the mount point of darkice, and it does not like spaces.
here is the code,
Code:
quality = (g_strdelimit (quality, " ", '_'));
Thankyou for your time to reply to me, so I have spent my time to submit the solution, for others to see.
-
October 11th, 2012, 09:12 AM
#6
Re: I wish to replace spaces with underscores in a sting
What's the problem? Just write a loop that looks at each char in quality and if it's a space, change it to an _
-
October 11th, 2012, 10:29 PM
#7
Solved, I wish to replace spaces with underscores in a string
The code that I have submitted on this thread is a single function "darkice_config_store"
so when I used.
Code:
quality = (g_strdelimit (quality, " ", '_'));
then the above code became part of the "darkice_config_store" function.
When the start button is pushed on the darksnow gui then this function is activated, this function writes the config files for darkice.
Unfortunately the code,
Code:
quality = (g_strdelimit (quality, " ", '_'));
would back feed the text with "spaces replaced with underscores to the gtkcombo box",
so I tried
Code:
strcpy(name2, quality)
quality = (g_strdelimit (quality, " ", '_'));
But the code
Code:
strcpy(name2, quality)
kept crashing the gui and doing weird stuff, like if I enter lots of text and pushed start on the gui, it would crash and burn.
after spending all night on this and experimenting with varies String Utility Functions from http://www.gtk.org/api/2.6/glib/glib...Functions.html
I found the following code worked perfect.
Code:
name2 = g_strdup (quality);
name2 = g_strdelimit (name2, " ", '_');
So the string "name2" has the spaces replaced with under scores and the string "quality" is left with spaces,
Now I can use the string "quality" in the places of the darkice config file where spaces are allowed, like the stream name.
so now the complete code looks like this,
Code:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <libintl.h>
#ifndef __GTK_H__
#include <gtk/gtk.h>
#endif
#ifndef __GLOBAL_H__
#include "global.h"
#endif
#ifndef __CONSTANTS_H__
#include "constants.h"
#endif
#ifndef __CONFIG_FILES_H__
#include "config_files.h"
#endif
gboolean darkice_config_store(char opt, char *save_path) {
int i;
char adddate;
char *bitrate;
char *bitratemode;
char *samplerate;
char *quality;
char *bitspersample;
char *channel;
char *buffersize;
char *device;
char *name2;
FILE *f_darkice_cfg;
adddate = (char ) (gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON (checkbutton_record) ))? '1': '0';
bitrate = (char *) gtk_entry_get_text ( GTK_ENTRY(GTK_COMBO(combo_bitrate)->entry));
bitratemode = (char *) gtk_entry_get_text ( GTK_ENTRY(GTK_COMBO(combo_bitratemode)->entry));
samplerate = (char *) gtk_entry_get_text ( GTK_ENTRY(GTK_COMBO(combo_samplerate)->entry));
quality = (char *) gtk_entry_get_text ( GTK_ENTRY(GTK_COMBO(combo_quality)->entry));
bitspersample = (char *) gtk_entry_get_text ( GTK_ENTRY(GTK_COMBO(combo_bitspersample)->entry));
channel = (char *) gtk_entry_get_text ( GTK_ENTRY(GTK_COMBO(combo_channel)->entry));
buffersize = (char *) gtk_entry_get_text ( GTK_ENTRY(GTK_COMBO(combo_buffersize)->entry));
device = (char *) gtk_entry_get_text ( GTK_ENTRY(GTK_COMBO(combo_device)->entry));
name2 = g_strdup (quality);
/* replace any space for underscores*/
name2 = g_strdelimit (name2, " ", '_');
/* quality = g_strdelimit (quality, " ", '_'); */
if (!( f_darkice_cfg = (opt != 0)? fopen(darkice_cfg, "w") : fopen(save_path, "w"))) {
printf("Error: Cannot open %s\n", (opt != 0)? darkice_cfg: save_path);
return FALSE;
}
/* for compatibility issues, mountpoint can start with slash */
i = 0;
/* Entry writes */
fprintf(f_darkice_cfg, "[general]");
fprintf(f_darkice_cfg, "\nduration = ");
fprintf(f_darkice_cfg, "0"); /* duration of encoding, in seconds. 0 means forever */
fprintf(f_darkice_cfg, "\nbufferSecs = ");
fprintf(f_darkice_cfg, "%s", buffersize); /* size of internal slip buffer, in seconds */
fprintf(f_darkice_cfg, "\nreconnect = ");
fprintf(f_darkice_cfg, "%s", "yes");
fprintf(f_darkice_cfg, "\n\n[input]");
fprintf(f_darkice_cfg, "\ndevice = ");
fprintf(f_darkice_cfg, "%s", "ameter"); /* OSS, ALSA or JACK */
fprintf(f_darkice_cfg, "\nsampleRate = ");
fprintf(f_darkice_cfg, "%s", samplerate); /* sample rate in Hz. try 11025, 22050 or 44100 */
fprintf(f_darkice_cfg, "\nbitsPerSample = ");
fprintf(f_darkice_cfg, "%s", bitspersample); /* bits per sample. try 16 */
fprintf(f_darkice_cfg, "\nchannel = "); /* channels. 1 = mono, 2 = stereo */
if (!strcmp(channel, "1 - Mono"))
fprintf(f_darkice_cfg, "1");
else
fprintf(f_darkice_cfg, "2");
fprintf(f_darkice_cfg, "\n\n[icecast2-0]"); /* icecast 2 version server */
/* write bitratemode */
fprintf(f_darkice_cfg, "\nbitrateMode = ");
if (!strcmp( gettext("Constant"), bitratemode))
fprintf(f_darkice_cfg, "cbr"); /* Constant bitrate mode */
else
if (!strcmp( gettext("Average"), bitratemode))
fprintf(f_darkice_cfg, "abr"); /* average bitrate mode */
else
if (!strcmp( gettext("Variable"), bitratemode))
fprintf(f_darkice_cfg, "vbr"); /* variable bitrate mode */
fprintf(f_darkice_cfg, "\nformat = ");
fprintf(f_darkice_cfg, "%s", "aacp"); /* bitrate of the stream sent to the server */
fprintf(f_darkice_cfg, "\nbitrate = ");
fprintf(f_darkice_cfg, "%s", bitrate); /* bitrate of the stream sent to the server */
fprintf(f_darkice_cfg, "\nquality = ");
fprintf(f_darkice_cfg, "%s", "1.0"); /* encoding quality */
fprintf(f_darkice_cfg, "\nserver = ");
fprintf(f_darkice_cfg, "%s", "127.0.0.1");
fprintf(f_darkice_cfg, "\nport = ");
fprintf(f_darkice_cfg, "%s", "8000"); /* port of the IceCast2 server, usually 8000 */
fprintf(f_darkice_cfg, "\npassword = ");
fprintf(f_darkice_cfg, "%s", "hackme"); /* source password to the IceCast2 server */
fprintf(f_darkice_cfg, "\nmountPoint = ");
fprintf(f_darkice_cfg, "%s", name2); /* mount point of this stream on the IceCast2 server */
fprintf(f_darkice_cfg, "%s", ".aac");
fprintf(f_darkice_cfg, "\nname = ");
fprintf(f_darkice_cfg, "%s", quality); /* name of the stream */
fprintf(f_darkice_cfg, "\ndescription = ");
fprintf(f_darkice_cfg, "%s", "TautokoFM 97.5 & 99.5"); /* description of the stream */
fprintf(f_darkice_cfg, "\nurl = ");
fprintf(f_darkice_cfg, "%s", ""); /* URL related to the stream */
fprintf(f_darkice_cfg, "\ngenre = ");
fprintf(f_darkice_cfg, "%s", ""); /* genre of the stream */
fprintf(f_darkice_cfg, "\npublic = ");
fprintf(f_darkice_cfg, "%s", "yes");
if (adddate == '1') {
fprintf(f_darkice_cfg, "\nlocalDumpFile = ");
fprintf(f_darkice_cfg, "%s", "/home/user/Recorded_Shows/");
fprintf(f_darkice_cfg, "%s", name2);
fprintf(f_darkice_cfg, "%s", ".aac");
fprintf(f_darkice_cfg, "\nfileAddDate = ");
fprintf(f_darkice_cfg, "%s", "yes");
}
fprintf(f_darkice_cfg, "\n");
fclose (f_darkice_cfg);
FILE *f_ameter_snd;
if (!( f_ameter_snd = (opt != 0)? fopen(ameter_snd, "w") : fopen(save_path, "w"))) {
printf("Error: Cannot open %s\n", (opt != 0)? ameter_snd: save_path);
return FALSE;
}
/* print ~/.asoundrc for ameter */
fprintf(f_ameter_snd, "pcm_scope.ameter {");
fprintf(f_ameter_snd, "\n type ameter");
fprintf(f_ameter_snd, "\n}");
fprintf(f_ameter_snd, "\npcm_scope_type.ameter {");
fprintf(f_ameter_snd, "\n lib /usr/lib/libameter.so");
fprintf(f_ameter_snd, "\n}");
fprintf(f_ameter_snd, "\npcm.ameter {");
fprintf(f_ameter_snd, "\n type meter");
fprintf(f_ameter_snd, "\n slave.pcm '");
fprintf(f_ameter_snd, "%s", device);
fprintf(f_ameter_snd, "%s", "'");
fprintf(f_ameter_snd, "\n scopes.0 ameter");
fprintf(f_ameter_snd, "\n}");
fprintf(f_ameter_snd, "\npcm.dsp0 ameter");
fprintf(f_ameter_snd, "\n");
fclose (f_ameter_snd);
FILE *f_add2;
if (!( f_add2 = (opt != 0)? fopen(add2, "w") : fopen(save_path, "w"))) {
printf("Error: Cannot open %s\n", (opt != 0)? add2: save_path);
return FALSE;
}
/* print /Stuff/add2.txt */
fprintf(f_add2, ":8000/");
fprintf(f_add2, name2);
fprintf(f_add2, ".aac");
fprintf(f_add2, "\n");
fclose (f_add2);
FILE *f_name1;
if (!( f_name1 = (opt != 0)? fopen(name1, "w") : fopen(save_path, "w"))) {
printf("Error: Cannot open %s\n", (opt != 0)? name1: save_path);
return FALSE;
}
/* print /Stuff/name1 */
fprintf(f_name1, name2);
fprintf(f_name1, "\n");
fclose (f_name1);
return TRUE;
}
-
October 12th, 2012, 05:36 AM
#8
Re: Solved, I wish to replace spaces with underscores in a string
Originally Posted by n_techo
The code that I have submitted on this thread is a single function "darkice_config_store"
All of that just to replace spaces with an underscore???
This is no more than 3 or 4 lines of code:
Code:
#include <string.h>
#include <stdio.h>
/* assume pString is null terminated and is not NULL */
void ReplaceChars(char *pString, char origChar, char replaceChar)
{
while (*pString )
{
if ( *pString == origChar )
*pString = replaceChar;
++pString;
}
}
int main()
{
char str[] = "This is a test";
ReplaceChars( str, ' ', '_');
printf( "%s", str );
}
I don't know what that stuff is that you posted, but it sure isn't what someone reading this thread would expect to be the solution.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; October 12th, 2012 at 05:51 AM.
-
October 12th, 2012, 05:43 AM
#9
Re: I wish to replace spaces with underscores in a sting
this was my solution
Code:
name2 = g_strdup (quality);
name2 = g_strdelimit (name2, " ", '_');
all of 2 lines
I included all of the code of the file I was working on, this was all I needed to add to my existing code which I had included in my post, since I was using GTK, in my code I decided to use string utility functions from glib. GLib is kind of an "objects for C" have a look at .http://www.gtk.org/api/2.6/glib/glib...Functions.html
Thankyou for the function,
kind regards
Last edited by n_techo; October 12th, 2012 at 06:15 AM.
-
October 12th, 2012, 05:47 AM
#10
Re: I wish to replace spaces with underscores in a sting
Originally Posted by n_techo
this was my solution
Code:
name2 = g_strdup (quality);
name2 = g_strdelimit (name2, " ", '_');
all of 2 lines
I used standard 'C'. I have no idea what g_strdup() or g_strdelimit() are supposed to do, and neither would anyone else looking for a simple, standard solution. As long as the compiler is standard, my solution works regardless of compiler brand, compiler version, operating system, etc.
Second, if g_strdup() is supposed to be some sort of a clone of the standard strdup() function, who cleans up that memory that was allocated? If that is the case where g_strdup() is the same as strdup(), then all you did was go from the frying pan into the fire by introducing a memory leak (or a potential for memory leaks).
Regards,
Paul McKenzie
Last edited by Paul McKenzie; October 12th, 2012 at 05:53 AM.
-
October 12th, 2012, 06:36 AM
#11
Re: I wish to replace spaces with underscores in a sting
I must say truthfully I like your solution the best, and am most grateful for the time you spend explaining this stuff to me.
can I output the result to a different string with your solution like;
Code:
#include <string.h>
#include <stdio.h>
/* assume pString is null terminated and is not NULL */
void ReplaceChars(char *pString, char origChar, char replaceChar)
{
while (*pString )
{
if ( *pString == origChar )
*pString = replaceChar;
++pString;
}
}
int main()
{
char str1[] = "This is a test";
char str2;
ReplaceChars( str1, ' ', '_');
printf( "%s", str2 );
}
If so I will try your solution, thank you for sharing your knowledge.
Most
kind
Regards
Last edited by n_techo; October 12th, 2012 at 06:41 AM.
-
October 12th, 2012, 10:07 AM
#12
Re: I wish to replace spaces with underscores in a sting
You can simply pass the second string to the function as well:
Code:
#include <string.h>
#include <stdio.h>
/* assume pString is null terminated and is not NULL */
void ReplaceChars(char *dstString, const char *srcString, char origChar, char replaceChar)
{
while (*srcString )
{
if ( *srcString == origChar )
*dstString = replaceChar;
else
*dstString = *srcString;
++srcString, ++dstString;
}
*dstString ='\0'; // make sure dstString is null-terminated
}
int main()
{
char str1[] = "This is a test";
char str2[20]; // this value of 20 is completely arbitrary, but you must make sure that it is large enough to hold str1.
// You could as well malloc the space like so: char *str2 = malloc(20);
// but then you have to make sure you call free(str2) when you are done with it to avoid a memory leak
ReplaceChars( str1, ' ', '_');
printf( "%s", str2 );
}
HTH,
Richard
Last edited by Richard.J; October 12th, 2012 at 10:09 AM.
Reason: added null-termination of dstString
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|