Announcement

Collapse
No announcement yet.

Function write/rewrite in file

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Function write/rewrite in file

    Hi,

    I tried to write/rewrite into a file by a C function.
    The value to write is a counter ( number of second ).

    The purpose is to create a file containing a value of counter always updated.

    I have tried several approaches.

    1) With ltrx_output_stream_write_line/ltrx_output_stream_init_to_file functions :

    Code:
    int CounterPump(void)
    {
           // dir = "/http/"
    	_mkdir(dir,0777);
    	
    	char file[50];
    	strcpy(file,dir);
    	strncat(file,"counter.html",13);
    	
    	// int to char
    	char bufferTime[100];
    
           // TimeUsePump is a int of counter
    	if(TimeUsePump > 0 )
    	{
                   // ltoascci is a custom function
                   // return is a char (bufferTime) with null terminated
    		ltoascci(bufferTime, TimeUsePump);
    	}
    	
    	char Counter[50];
    	
    	strcpy(Counter,"test");
    	strcat(Counter,"\r\n");
    	
    
    	struct output_stream_to_file writeOfFile;
    
    	if(!ltrx_output_stream_init_to_file(&writeOfFile, file))
    	{
    		TLOG(TLOG_SEVERITY_LEVEL__ERROR,"Open \"%s\" failed ! \r\n",file);
    	}
    	else
    	{
    		off_t offset = 0;
    		lseek(writeOfFile.fd,offset,SEEK_SET);
    
    	        if(!ltrx_output_stream_write_line(&writeOfFile.outStream,bufferTime))
    		{
    			TLOG(TLOG_SEVERITY_LEVEL__ERROR,"Write counter pump failed !");
    		}
    		ltrx_output_stream_close(&writeOfFile.outStream);		
    				
    		TLOG(TLOG_SEVERITY_LEVEL__INFORMATIONAL,"Counter Pump : %d\r\n",Counter);
    	}
    
    	return 0;
    }
    2) With _open, _write, _close function :

    Code:
    const char convertInt[] = {'t','e','s','t','\0'};
    
    int CounterPump(void)
    {
    	int fd, result;
    		
    	_mkdir(dir,0777);
    	
    	char file[50];
    	strcpy(file,dir);
    	strncat(file,"counter.html",13);
    		
    	fd = _open(file, O_CREAT | O_RDWR | O_TRUNC );
    	if(fd > 0)
    	{
    		result = _write(fd,convertInt,8);
    		if( result < 0)
    		{
    			TLOG(TLOG_SEVERITY_LEVEL__ERROR,"Counter Pump Error %d: write file counter => %d \r\n",errno,result);
    			return -1;
    		}
    		else
    		{
    			TLOG(TLOG_SEVERITY_LEVEL__INFORMATIONAL,"Counter Pump Counter : %d \r\n",TimeUsePump);
    		}
    		close(fd);
    		return 0;
    	}
    	else
    	{
    		TLOG(TLOG_SEVERITY_LEVEL__ERROR,"Counter Pump Error %d: open file counter => %d \r\n",errno,fd);
    	}
    	
    	return -1;
    }
    I have tried with _write, write (without underscore), ltrx_output_stream_write_line/ltrx_output_stream_init_to_file, etc ..

    It Works fine when the argument (text to write ) passed is a hard coded string (like "test") but xpico wi-fi resets or return an error when the argument is a pointer to a char array.

    Thanks

  • #2
    In your first example, where you are using ltrx_output_stream_write_line, are you saying that if you pass bufferTime it fails, but if you pass Counter it works? If so, is it due to the ltoascii function? Could you reduce to code I could compile and reproduce the issue with?

    Mariano

    Comment

    Working...
    X