So I am really new to C programming and trying to replace a string to a date from a file i read and then write it to an other file. But the problem is that when i write it into the file the string remains the same.
What I want is to read this from a file:
<html>
<head>
<!--#include file=”date”-->
</head>
<body>
</body>
</html>
Output file
<html>
<head>
Sat Nov 3 14:43:53 2012
</head>
<body>
</body>
</html>
I get an error: passing argument 1 in date_change from incompatible pointer type
Code
//System date replacement function
void *date_change(char** s, char* str, char* date){
static char buffer[4096];
char *p;
if(!(p = strstr(*s, str))) // <!--#echo var=\"date\"--> find this
return *s;
strncpy(buffer, *s, p-*s); //
buffer[p-*s] = '\0';
sprintf(buffer+(p-*s), "%s%s", date, p+strlen(str));
return buffer;
}
//main
int main(int argc, char *argv[]){
int f;
f = open(argv[1], O_RDONLY);
if(errno != 0){
perror("Hiba");
exit(1);
}
//read from file
char c[1000];
while(read(f,&c, 1000)){
}
// --------------------------------// Get the System date and trying to replace it with the date_change function
time_t mytime;
mytime = time(NULL);
struct tm *time = localtime(&mytime);
char date[20];
strftime(date, sizeof(date), "%c", time); //format time as string
char* date_str;
int g = open("data.txt", O_WRONLY | O_CREAT, 0600);
//should replace all <!--#echo var=\"date\" --> to the system date
while(date_str = strstr(c, "<!--#echo var=\"date\"-->")){
date_change(&c, date_str, date);
}
write(g, c, strlen(c));
close(g);
// -------------------------------- //
close(f);
return 0;
}
Your code is not trying to modify the buffer that was passed into it. Instead, you've created a static array that you are writing into, and you're then returning that static array (without actually looking at the return value).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With