3

因此,我对 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;
}
4

2 回答 2

3

您的代码不会尝试修改传递给它的缓冲区。相反,您创建了一个正在写入的静态数组,然后返回该静态数组(实际上不查看返回值)。

于 2012-11-03T12:09:57.100 回答
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
于 2012-11-03T14:30:59.373 回答