【问题标题】:change string array content with function in C用C中的函数更改字符串数组内容
【发布时间】:2012-10-23 22:13:04
【问题描述】:

所以我是 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;
}

【问题讨论】:

  • 请尝试使用缩进 - 使代码更易于阅读。
  • 您没有使用 date_change 返回的值。
  • @EdHeal 在他的缩进中你反对什么?我觉得很好。不是我会使用的确切样式,但也不是不稳定的。
  • 你有一些问题,其中之一是你没有为 read() 定义
  • @mah - 根本没有缩进。大括号之间的代码应该缩进以使其可读(更容易看到 if 语句何时开始和结束,while 循环也是如此。

标签: c arrays string function replace


【解决方案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

【讨论】:

  • 还是把原来的内容写到date.txt中,不替换应该的内容
  • @Vaner 它对我有用。确保 C 程序中的字符串值与文件中的值完全匹配。您发布的行与文件中的行是否相同?尝试使用我在上面发布的行作为输入。
  • @Vaner 您刚刚发布的内容与您在问题中提出的内容不符。您在问题中的“日期”之后有一个空格(我在我的数据和程序中使用了它),而您只是在“日期”之后没有空格的行上方写了一个空格。
  • 我更新了问题帖。输入文件看起来是这样的。但即使输入文件是 1 行,也应该替换 include=date。
  • 是的,我再次检查以匹配文件和您的程序版本。还是不行。将原始输入写入新文件。
【解决方案2】:

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

【讨论】:

  • 要么更改您的 date_changed() 函数以将更改发送回原始缓冲区,或者更改您的 write() 语句以写出由 date_changed() 返回的缓冲区 [这也需要您分配函数返回一个变量]。
  • 我在 date_change 中从不兼容的指针类型传递参数 1 时出错
  • @Vader 更新为多行。
猜你喜欢
  • 1970-01-01
  • 2014-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-10
相关资源
最近更新 更多