所以我对C编程很新,并尝试将字符串替换为我读取的文件中的日期,然后将其写入其他文件。 但问题是,当我将其写入文件时,字符串保持不变。
我想要的是从文件中读取它:
<html>
<head>
<!--#include file=”date”-->
</head>
<body>
</body>
</html>
输出文件
<html>
<head>
Sat Nov 3 14:43:53 2012
</head>
<body>
</body>
</html>
我收到错误:在date_change中从不兼容的指针类型
传递参数1代码
//系统日期替换功能
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;
}
//主
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)){
}
// -------------------------------- // 获取系统日期并尝试将其替换为date_change函数
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;
}
答案 0 :(得分:3)
您的代码并未尝试修改传递给它的缓冲区。相反,您已经创建了一个您正在编写的静态数组,然后您将返回该静态数组(而不实际查看返回值)。
答案 1 :(得分:0)
我会逐行阅读,因为你使用的是文本文件:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<time.h>
//System date replacement function
void *date_change(char* s, const char* str, const char* date) {
static char new_line[2000];
static char new_line2[2000];
//should replace all <!--#echo var=\"date\" --> to the system date
char *date_str = strstr(s,str);
if ( date_str == NULL )
{
return s;
}
int prefix_length = date_str - s;
strncpy(new_line,s,prefix_length);
new_line[prefix_length] = '\0';
strcat(new_line,date);
strcat(new_line,s + prefix_length + strlen(str));
strcpy(new_line2,new_line);
while ( ( date_str = strstr(new_line,str) ) != NULL)
{
prefix_length = date_str - new_line;
strncpy(new_line2,new_line,prefix_length);
new_line2[prefix_length] = '\0';
strcat(new_line2,date);
strcat(new_line2,new_line + prefix_length + strlen(str));
strcpy(new_line,new_line2);
}
return new_line2;
}
//main
int main(int argc, char *argv[])
{
(void) argc;
FILE *f;
f = fopen(argv[1], "r");
if(errno != 0)
{
perror("Hiba");
exit(1);
}
// --------------------------------
// 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[50];
strftime(date, sizeof(date), "%c", time); //format time as string
FILE *g = fopen("data.txt", "w");
//read from file
char c[1000];
//const char *search_string = "<!--#echo var=\"date\" -->";
const char *search_string = "<!--#include file=”date” -->";
while( fgets(c,sizeof(c),f) > 0 ){
char *new_line = date_change(c, search_string, date);
fputs(new_line, g);
}
fclose(g);
fclose(f);
}
输入文件:
<html>
<head>
<!--#include file="date"--> <p>this is important</p><!--#include file="date"--> the end
</head>
<body>
</body>
<!--#include file="date"-->
</html>
<!--#include file="date"--> <!--#include file="date"--> <!--#include file="date"--> <!--#include file="date"-->
输出文件:
<html>
<head>
Sat Nov 3 10:22:06 2012 <p>this is important</p>Sat Nov 3 10:22:06 2012 the end
</head>
<body>
</body>
Sat Nov 3 10:22:06 2012
</html>
Sat Nov 3 10:22:06 2012 Sat Nov 3 10:22:06 2012 Sat Nov 3 10:22:06 2012 Sat Nov 3 10:22:06 2012