m0_55448086 2021-03-23 12:15 采纳率: 100%
浏览 36
已采纳

before ‘]’ token 报错,请大神指点

#include <sys/types.h>        //定义了一些常用数据类型,比如size_t
#include <fcntl.h>            //定义了open、creat等函数,以及表示文件权限的宏定义
#include <unistd.h>            //定义了read、write、close、lseek等函数
#include <errno.h>            //与全局变量errno相关的定义
#include <sys/ioctl.h>        //定义了ioctl函数 
#include <stdio.h>


int open_file(char *filename,char *write_dat)
{
    int fd = -1;
    int res = 0;
    char read_buf[128] = {0};
    /* 写入文件操作示例 */
    //1. 打开文件
    fd = open(filename, O_RDWR | O_CREAT, 0664);
    if(fd < 0)
    {
        printf("%s file open fail,errno = %d.\r\n", filename, errno);
        return -1;
    }
    //2. 读取内容
    res = write(fd, write_dat, sizeof(write_dat));
    if(res < 0)
    {
        printf("write dat fail,errno = %d.\r\n", errno);
        return -1;
    }
    else
    {
        printf("write %d bytes:%s\r\n", res, write_dat);
    }
    //3. 关闭文件
    close(fd);
    /* 读取文件数据示例 */
    //1. 打开文件
    fd = open(filename, O_RDONLY);
    if(fd < 0)
    {
        printf("%s file open fail,errno = %d.\r\n", filename, errno);
        return -1;
    }
    //2. 写入内容
    res = read(fd, read_buf, sizeof(read_buf));
    if(res < 0)
    {
        printf("read dat fail,errno = %d.\r\n", errno);
        return -1;
    }
    else
    {
        printf("read %d bytes:%s\r\n", res, read_buf);
    }
    //3. 关闭文件
    close(fd);
}

int main(void)
{
    char main_filename[]  = "test.txt";
    char main_write_dat[] = "Hello World!";
    open_file(&main_filename[],&main_write_dat[]);    
    return 0;
}

  • 写回答

1条回答 默认 最新

  • 爱晚乏客游 2021-03-23 13:38
    关注

    main函数中改成open_file(main_filename, main_write_dat);

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)