Click here to Skip to main content
14,484,721 members

C / C++ / MFC

 
AnswerRe: PNG image shown is missing 1 pixel right and 1 pixel bottom Pin
sdancer7530-Jan-20 6:53
Membersdancer7530-Jan-20 6:53 
QuestionIGNORE THIS !!! file not recognized: File format not recognized Pin
Vaclav_29-Jan-20 5:45
MemberVaclav_29-Jan-20 5:45 
AnswerRe: file not recognized: File format not recognized Pin
k505429-Jan-20 6:03
Memberk505429-Jan-20 6:03 
GeneralRe: file not recognized: File format not recognized Pin
Vaclav_29-Jan-20 6:44
MemberVaclav_29-Jan-20 6:44 
GeneralRe: file not recognized: File format not recognized Pin
Vaclav_29-Jan-20 6:58
MemberVaclav_29-Jan-20 6:58 
GeneralRe: file not recognized: File format not recognized Pin
k505429-Jan-20 7:15
Memberk505429-Jan-20 7:15 
GeneralRe: file not recognized: File format not recognized Pin
Vaclav_29-Jan-20 15:39
MemberVaclav_29-Jan-20 15:39 
Questionreplace words in a file Pin
Member 1295754729-Jan-20 4:19
MemberMember 1295754729-Jan-20 4:19 
The task is to replace - looks for all occurrences of string from and replaces it with string to.
It is possible to specify one or more files on which to perform the replace
operation(s) in a single replace command.
and this is my code:
void replace(char* OldWord, char* NewWord)
{
char *result = NULL;
char buff[1000];
char c;
char line[1000];
FILE * f = fopen ("file1.txt", "r+");
int count=0;
while (fgets(buff, sizeof(buff), f))
{
result = replaceWord(buff, OldWord, NewWord);
//
}

//
//free(result);
}
char *replaceWord(const char *s, const char *oldW,const char *newW)
{
char *result;
int i, cnt = 0;
int newWlen = strlen(newW);
int oldWlen = strlen(oldW);
static int k=0;
k++;
char newline[1000];
FILE * fp;
// printf("%s\n",oldW);
//printf("%s\n",newW);
// Counting the number of times old word
// occur in the string
for (i = 0; s[i] != '\0'; i++)
{
if (strstr(&s[i], oldW) == &s[i])
{
cnt++;

// Jumping to index after the old word.
i += oldWlen - 1;
}
}

// Making new string of enough length
result = (char *)malloc(i + cnt * (newWlen - oldWlen) + 1);

i = 0;
while (*s)
{
// compare the substring with the result
if (strstr(s, oldW) == s)
{
strcpy(&result[i], newW);
i += newWlen;
s += oldWlen;
fp = fopen ("file1.txt", "w");
rewind(fp);
fseek(fp,i-newWlen,SEEK_SET);
fprintf(fp,"%s\n",result);
}
else
{
result[i++] = *s++;
}
}

result[i] = '\0';
fp = fopen ("file1.txt", "w");
fprintf(fp,"%s\n",result);
// printf("%d\n",k);
return result;
}
the replace function does not give the desired output
The command line looks like:
./a.out replace old_word new_word files.txt
must replace old_word with the new_word in all files.
AnswerRe: replace words in a file Pin
Victor Nijegorodov29-Jan-20 5:30
MemberVictor Nijegorodov29-Jan-20 5:30 
GeneralRe: replace words in a file Pin
Member 1295754729-Jan-20 5:34
MemberMember 1295754729-Jan-20 5:34 
GeneralRe: replace words in a file Pin
k505429-Jan-20 5:52
Memberk505429-Jan-20 5:52 
AnswerRe: replace words in a file Pin
k505429-Jan-20 5:42
Memberk505429-Jan-20 5:42 
SuggestionRe: replace words in a file Pin
David Crow29-Jan-20 17:04
MemberDavid Crow29-Jan-20 17:04 
AnswerRe: replace words in a file Pin
Stefan_Lang29-Jan-20 21:21
mveStefan_Lang29-Jan-20 21:21 
QuestionLinking libraries - follow-up- gcc++ won't link Pin
Vaclav_28-Jan-20 4:22
MemberVaclav_28-Jan-20 4:22 
AnswerRe: Linking libraries - follow-up- gcc++ won't link Pin
Richard MacCutchan28-Jan-20 5:04
mveRichard MacCutchan28-Jan-20 5:04 
AnswerRe: Linking libraries - follow-up- gcc++ won't link Pin
k505428-Jan-20 5:08
Memberk505428-Jan-20 5:08 
AnswerRe: Linking libraries - follow-up- gcc++ won't link Pin
CPallini28-Jan-20 5:13
mveCPallini28-Jan-20 5:13 
GeneralRe: Linking libraries - follow-up- gcc++ won't link Pin
k505428-Jan-20 6:47
Memberk505428-Jan-20 6:47 
GeneralRe: Linking libraries - follow-up- gcc++ won't link Pin
CPallini28-Jan-20 9:52
mveCPallini28-Jan-20 9:52 
AnswerRe: Linking libraries - follow-up- gcc++ won't link Pin
Vaclav_28-Jan-20 7:07
MemberVaclav_28-Jan-20 7:07 
GeneralRe: Linking libraries - follow-up- gcc++ won't link Pin
Richard MacCutchan28-Jan-20 9:22
mveRichard MacCutchan28-Jan-20 9:22 
AnswerSOLVED (?) Re: Linking libraries - follow-up- gcc++ won't link Pin
Vaclav_29-Jan-20 4:00
MemberVaclav_29-Jan-20 4:00 
GeneralRe: SOLVED (?) Re: Linking libraries - follow-up- gcc++ won't link Pin
Richard MacCutchan29-Jan-20 4:16
mveRichard MacCutchan29-Jan-20 4:16 
QuestionReverse engineering GCC output - need library file (name) and path. Pin
Vaclav_27-Jan-20 5:34
MemberVaclav_27-Jan-20 5:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.