Coder Social home page Coder Social logo

Comments (1)

acassis avatar acassis commented on May 30, 2024

@ryancb4 I think you are messing with basic concepts of the C language, when you use out_string[1] to are referencing to the value at position 1, instead of a pointer to the position 1. This way, only write(outfd, out_string, 22); is correct.

You can try it in a Linux machine to see the issue:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

#define ARRAYSIZE   22

static char out_string[] = {0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
                            0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52,
                            0x53, 0x54, 0x55, 0x56};

int main(void)
{
  int fd;
  int ret;

  fd = open("/tmp/test.txt", O_RDWR | O_CREAT);
  if (fd < 0)
    {
      int error = errno;
      printf("Error %d opening /tmp/test.txt\n", error);
      return -1;
    }

  ret = write(fd, out_string, ARRAYSIZE);
  if (ret < ARRAYSIZE)
    {
      printf("Error writting array to the file\n");
      goto close_exit;
    }

  printf("Data written to the file\n");

close_exit:

  close(fd);
  return 0;
}

Replace write(fd, out_string, ARRAYSIZE); with write(fd, out_string[1], 19); and see what happen! Type: try to use &out_string[1] and see what happens! ;-)

from nuttx-apps.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.