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

テキストファイルを使用しているので、1行ずつ読みます。

#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 に答える