3

So I am really new to C programming and trying to replace a string to a date from a file i read and then write it to an other file. But the problem is that when i write it into the file the string remains the same.

What I want is to read this from a file:

<html>
<head>
<!--#include file=”date”-->
</head>
<body>
</body>
</html>

Output file

<html>
<head>
Sat Nov 3 14:43:53 2012
</head>
<body>
</body>
</html>

I get an error: passing argument 1 in date_change from incompatible pointer type

Code

//System date replacement function

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)){

    }

// --------------------------------// 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[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;
}
10
  • Please have a go at using indentation - makes the code easier to read. Commented Nov 3, 2012 at 12:07
  • You're not using the value returned from date_change. Commented Nov 3, 2012 at 12:10
  • @EdHeal what do you object to in his indentation? It looks fine to me. Not the exact style I would use, but not wonky either. Commented Nov 3, 2012 at 12:10
  • You have some problems, one of them is that you don't define <unistd.h> for read() Commented Nov 3, 2012 at 12:12
  • @mah - There is no indentation at all. The code in between braces should be indented to make it readable (easier to see when if statements start and end, ditto for while loops. Commented Nov 3, 2012 at 12:15

2 Answers 2

3

Your code is not trying to modify the buffer that was passed into it. Instead, you've created a static array that you are writing into, and you're then returning that static array (without actually looking at the return value).

Sign up to request clarification or add additional context in comments.

3 Comments

Either change your date_changed() function to send your changes back to the original buffer, or change your write() statement to write out the buffer returned by date_changed() [which would also require you assign that function's return to a variable].
and i get error passing argument 1 in date_change from incompatible pointer type
@Vader Updated for multiple on one line.
0

I would read it line by line since you are using a text file:

#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);
}

input file:

<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"-->

output file:

<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

12 Comments

still write the original content to the date.txt and does not replace what should
@Vaner It works for me. Make sure that the string value in the C program exactly matches the value in your file. Is the line that you posted identical to what is in the file? Try it with the lines that I posted above as input.
@Vaner What you just posted doesn't match what you put in your question. You have a space after "date" in your question (and I used that in my data and program) and you just wrote above a line without a space after "date".
i updated the question post. The input file looks as it is. But even if it the input file would be 1 line the include=date should be replaced.
yeah i double checked that to match in the file and your version of the program. Still doesn't work. Writes the original input to the new file.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.