Portál AbcLinuxu, 29. října 2025 18:34
Ahoj vsem. Omlouvam se za lama dotaz a predem rikam, ze nejde o skolni ulohu, jen me tpo zajima. Pujcil jsem si od kamose knizku o programovani v C a rad bych se na nedco zeptal. Chtel bych nacist soubor jako vstup a nahradit hledany retezec necim jinym. V unixu se to dela programem sed, ale tady nevim.
Tohle by melo nacist vstup a to mi chodi
#include <stdio.h>
main ()
{
int a;
a = getchar();
while (a !=EOF) {
putchar(a);
}
}
ale musim tam nekam "napasovat" neco jako
if (a == 'hledany retezec') ....
Dekuji za radu vsem, Tomas
V zasade lze asi pouzit dva zpusoby:
No a pak uz bych jen doporucil nenacitat vstup po znacich, ale rovnou po radcich pres (nestandardni funkci) getline.
$ cat str_replace.c |./str_replace '<' '<' |./str_replace '>' '>' $ echo -e "abcd abcd abcd abcd\naaa\nbbb" |./str_replace b X aXcd aXcd aXcd aXcd aaa XXX
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LINE_MAX 1023
#define MATCH_MAX 1024
static int str_replace(char *p_str, size_t str_max, char *p_old, char *p_new)
{
int i;
int len;
int line_new_len;
int str_len = strlen(p_str);
int old_len = 0;
int new_len = strlen(p_new);
int seek = 0;
char line_new[str_max+1];
char *p_match[MATCH_MAX];
p_match[0] = p_str;
for (i = 1; i < MATCH_MAX; ++i) {
p_match[i] = NULL;
}
for (i = 1; i < MATCH_MAX; ++i) {
if ((p_match[i] = strstr(p_match[i-1]+old_len, p_old)) == NULL) {
break;
}
if (i == 1) {
old_len = strlen(p_old);
}
}
line_new_len = (str_len + (new_len - old_len) * (i-1));
if (line_new_len >= str_max) {
return -1;
}
memset(line_new, '\0', str_max+1);
for (i = 1; i < MATCH_MAX; ++i) {
if (p_match[i] == NULL) {
if (i > 1) {
strncpy(line_new+seek, p_new, new_len);
seek += new_len;
}
strcpy(line_new+seek, p_match[i-1]+old_len);
break;
}
else {
if ((len = p_match[i] - p_match[i-1]) > 0) {
if (i > 1) {
strncpy(line_new+seek, p_new, new_len);
seek += new_len;
len -= old_len;
strncpy(line_new+seek, p_match[i-1]+old_len, len);
seek += len;
}
else {
strncpy(line_new+seek, p_match[i-1], len);
seek += len;
}
}
}
}
memset(p_str, '\0', str_max+1);
strncpy(p_str, line_new, str_max);
return 0;
}
int main(int argc, char *argv[])
{
char line[LINE_MAX+1];
if (argc != 3) {
printf("Usage: %s OLD NEW\n", argv[0]);
exit(EXIT_SUCCESS);
}
memset(line, '\0', LINE_MAX+1);
while (fgets(line, LINE_MAX+1, stdin) != NULL) {
if (str_replace(line, LINE_MAX, argv[1], argv[2]) == -1) {
printf("output buffer is small\n");
exit(EXIT_FAILURE);
}
printf("%s", line);
}
exit(EXIT_SUCCESS);
}
Ještě by se Ti mohla hodit ukázka jak pracovat s regex.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <regex.h>
/*
* Example:
*
* YYYY-MM-DDTHH:MM:SS.sss
*
* PATTERN = '^([0-9]{4})-([0-9]{2})-([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2}.[0-9]*)$'
* STRING = '2009-07-14T20:36:01.123'
*
* result:
*
* match start = 0, match stop = 23
* year = 2009
* month = 07
* day = 14
* hour = 20
* minute = 36
* second = 01.123
*/
#define MATCH_SIZE 7
#define STRING_MAX 63
static void substr_copy(char *in, char *out, int begin, int end)
{
int size = end - begin;
if (size > STRING_MAX) {
size = STRING_MAX;
}
memset(out, 0, STRING_MAX+1);
strncpy(out, in+begin, size);
}
int main(int argc, char *argv[])
{
int retcode;
char year[STRING_MAX+1];
char month[STRING_MAX+1];
char day[STRING_MAX+1];
char hour[STRING_MAX+1];
char minute[STRING_MAX+1];
char second[STRING_MAX+1];
char *pattern;
char *string;
regmatch_t match[MATCH_SIZE];
regex_t re;
if (argc != 3) {
printf("Usage: %s PATTERN STRING\n", argv[0]);
exit(EXIT_SUCCESS);
}
pattern = argv[1];
string = argv[2];
if ((retcode = regcomp(&re, pattern, REG_EXTENDED)) != 0) {
fprintf(stderr, "regcomp() failure: %i\n", retcode);
exit(EXIT_FAILURE);
}
if (regexec(&re, string, MATCH_SIZE, match, 0) == REG_NOMATCH) {
fprintf(stderr, "regexec() pattern '%s' not found in string '%s'\n", pattern, string);
exit(EXIT_FAILURE);
}
regfree(&re);
substr_copy(string, year, match[1].rm_so, match[1].rm_eo);
substr_copy(string, month, match[2].rm_so, match[2].rm_eo);
substr_copy(string, day, match[3].rm_so, match[3].rm_eo);
substr_copy(string, hour, match[4].rm_so, match[4].rm_eo);
substr_copy(string, minute, match[5].rm_so, match[5].rm_eo);
substr_copy(string, second, match[6].rm_so, match[6].rm_eo);
printf("match start = %i, match stop = %i\n", match[0].rm_so, match[0].rm_eo);
printf("year = %s\n", year);
printf("month = %s\n", month);
printf("day = %s\n", day);
printf("hour = %s\n", hour);
printf("minute = %s\n", minute);
printf("second = %s\n", second);
exit(EXIT_SUCCESS);
}
Případně by Tě ještě mohla zajímat fce g_regex_replace () z GLib: Perl-compatible regular expressions a nebo knihovna The Better String Library.
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.