所以我对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;
}//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)){
}//
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;
}发布于 2012-11-03 22:30:59
我会逐行阅读它,因为您使用的是文本文件:
#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发布于 2012-11-03 20:09:57
您的代码不会尝试修改传入的缓冲区。相反,您创建了一个写入的静态数组,然后返回该静态数组(而不实际查看返回值)。
https://stackoverflow.com/questions/13209015
复制相似问题