Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在UNIX环境中使用C编程语言创建空文件时遇到问题_Unix_Systems Programming - Fatal编程技术网

在UNIX环境中使用C编程语言创建空文件时遇到问题

在UNIX环境中使用C编程语言创建空文件时遇到问题,unix,systems-programming,Unix,Systems Programming,我最近开始在UNIX环境中编程。我需要编写一个程序,用这个命令创建一个空文件,文件名和大小在终端中给定 gcc foo.c -o foo.o ./foo.o result.txt 1000 这里result.txt表示新创建文件的名称,1000表示文件的大小(字节) 我确信lseek函数会移动文件偏移量,但问题是,每当我运行程序时,它都会创建一个具有给定名称的文件,但是文件的大小是0 这是我的小程序的代码 #include <unistd.h> #include <stdi

我最近开始在UNIX环境中编程。我需要编写一个程序,用这个命令创建一个空文件,文件名和大小在终端中给定

gcc foo.c -o foo.o 
./foo.o result.txt 1000
这里result.txt表示新创建文件的名称,1000表示文件的大小(字节)

我确信lseek函数会移动文件偏移量,但问题是,每当我运行程序时,它都会创建一个具有给定名称的文件,但是文件的大小是0

这是我的小程序的代码

#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
int main(int  argc, char **argv)
{
    int fd;
    char *file_name;
    off_t bytes;
    mode_t mode;

    if (argc < 3)
    {
        perror("There is not enough command-line arguments.");
        //return 1;
    }

    file_name = argv[1];
    bytes = atoi(argv[2]);
    mode = S_IWUSR | S_IWGRP | S_IWOTH;

    if ((fd = creat(file_name, mode)) < 0)
    {
        perror("File creation error.");
        //return 1;
    }
    if (lseek(fd, bytes, SEEK_SET) == -1)
    {
        perror("Lseek function error.");
        //return 1;
    }
    close(fd);
    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main(int argc,字符**argv)
{
int-fd;
字符*文件名;
关字节;
模式t模式;
如果(argc<3)
{
perror(“没有足够的命令行参数”);
//返回1;
}
文件名=argv[1];
字节=atoi(argv[2]);
模式=S|U IWUSR | S|U IWGRP | S|U IWOTH;
如果((fd=creat(文件名,模式))<0)
{
perror(“文件创建错误”);
//返回1;
}
if(lseek(fd,字节,SEEK_SET)=-1)
{
perror(“Lseek函数错误”);
//返回1;
}
关闭(fd);
返回0;
}

如果不允许您使用任何其他功能来帮助创建“空白”文本文件,为什么不在
create
上更改文件模式,然后循环并写入:

int fd = creat(file_name, 0666);

for (int i=0; i < bytes; i++) {
    int wbytes = write(fd, " ", 1);
    if (wbytes < 0) {
        perror("write error")
        return 1;
    }
}

根据通常的定义,“空”文件的大小为零。你说的“空”是什么意思?@williampersell我指的是里面没有任何内容的文件,但是它有一个特定的大小。如果它里面没有内容,那么它的特定大小是零。@williampersell我只允许使用读/写/打开/创建/lseekfunctions@ShahnurIsgandarli不允许使用
string.h
stdlib.h
中的其他函数?
if ((fd = creat(file_name, 0666)) < 0) // XXX edit to include write
{
    perror("File creation error.");
    //return 1;
}
if (lseek(fd, bytes - 1, SEEK_SET) == -1) // XXX seek to bytes - 1
{
    perror("Lseek function error.");
    //return 1;
}

// add this call to write a single byte @ position set by lseek.
if (write(fd, " ", 1) == -1)
{
    perror("Write function error.");
    //return 1;
}

close(fd);
return 0;