Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.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
linux下的C初学者。写函数不';我不能正常工作_C_Linux - Fatal编程技术网

linux下的C初学者。写函数不';我不能正常工作

linux下的C初学者。写函数不';我不能正常工作,c,linux,C,Linux,我想写一个C程序,让用户能够在文件中写东西。我的问题是,在制作和运行程序后,文件仍然是空的??你知道我该怎么解决这个问题吗 #include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> // the user should give a file to write the file int main (int argc , char**argv) {

我想写一个C程序,让用户能够在文件中写东西。我的问题是,在制作和运行程序后,文件仍然是空的??你知道我该怎么解决这个问题吗

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>


// the user should give a  file to write the file
int main (int argc , char**argv)
{
    int fd; // file descriptor
    char ret; // the character
    int offset;
    if(argc != 2) {
        printf("You have to give the name or the path of the file to work with \n");
        printf("Exiting the program \n")
        return -1;
    }



    fd = open (argv[1], O_WRONLY/*write*/|O_CREAT/*create if not found */, S_IRUSR|S_IWUSR/*user can read and write*/);
    if (fd == -1) {
        printf("can'T open the file ");
        return -1;
    }

    printf("At wich position you want to start ");
    scanf("%d",&offset);
    lseek(fd,offset,SEEK_SET);
    while(1) {
        ret = getchar();
        if(ret == '1') {
            printf("closing the file");
            close (fd);
            return 1;
        }
        else
            write (fd,red, sizeof(char));
    }

    return 0;
}
#包括
#包括
#包括
#包括
//用户应该提供一个文件来写入该文件
int main(int argc,字符**argv)
{
int fd;//文件描述符
char ret;//字符
整数偏移量;
如果(argc!=2){
printf(“您必须提供要使用的文件的名称或路径\n”);
printf(“退出程序\n”)
返回-1;
}
fd=open(argv[1],O_WRONLY/*write*/|O_CREAT/*如果未找到,则创建*/,S_IRUSR | S|u IWUSR/*用户可以读写*/);
如果(fd==-1){
printf(“无法打开文件”);
返回-1;
}
printf(“在您想要开始的位置”);
扫描频率(“%d”,偏移量(&F);
lseek(fd、偏移、寻道集);
而(1){
ret=getchar();
如果(ret=='1'){
printf(“关闭文件”);
关闭(fd);
返回1;
}
其他的
写入(fd、red、sizeof(char));
}
返回0;
}

提前感谢您的帮助。

我注意到一个错误,调用write函数:

write (fd,red, sizeof(char));
应该是:

write (fd, &red, sizeof(char));
你忘了在红色之前写下需要的地址

写入的语法:

这将在运行时导致代码中出现错误

编辑:在写入函数中,您正在使用未定义的
red
,我认为它应该是代码中的
ret
变量。将其更正为
write(fd,&ret,sizeof(char))

第二,你忘了
if
printf(“退出程序”)之后
,但我也认为这是错误的,因为您说您遇到了运行时错误


旁注:如果您使用的是gcc编译器,那么您可以使用
gcc-Wall-pedantic
生成警告

我做了一些更改,应该可以:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

int main (int argc , char**argv) 
{
   int fd; // file descriptor 
   char ret; // the character 
   int offset; 
   if(argc != 2){
     printf("You have to give the name or the path of the file to work with \n");
     printf("Exiting the program \n"); **//There was ';' missing here**
     return -1;
  }
  fd = open (argv[1], O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR);
  if (fd == -1) {
     printf("can'T open the file ");
     return -1;
  }

  printf("At wich position you want to start ");
  scanf("%d",&offset);
  lseek(fd,offset,SEEK_SET);
  while(1){
     ret = getchar();
     if(ret == '1'){
     printf("closing the file");
     close (fd);
     return 1;
  }
  else 
     write (fd,&ret, sizeof(char)); **//red has been changed to &ret**
}

  return 0;
#包括
#包括
#包括
#包括
int main(int argc,字符**argv)
{
int fd;//文件描述符
char ret;//字符
整数偏移量;
如果(argc!=2){
printf(“您必须提供要使用的文件的名称或路径\n”);
printf(“退出程序\n”);**//此处缺少“;”**
返回-1;
}
fd=开放(argv[1],O|u WRONLY | O|u CREAT,S|u IRUSR | S|u IWUSR);
如果(fd==-1){
printf(“无法打开文件”);
返回-1;
}
printf(“在您想要开始的位置”);
扫描频率(“%d”,偏移量(&F);
lseek(fd、偏移、寻道集);
而(1){
ret=getchar();
如果(ret=='1'){
printf(“关闭文件”);
关闭(fd);
返回1;
}
其他的
写入(fd,&ret,sizeof(char));***//红色已更改为&ret**
}
返回0;
}应该是:

write (fd,&ret, sizeof(char));

write将指针指向内存位置,由于ret是单个字符,因此需要向其传递指针。

使用
fopen
并测试null。另外,您使用的编译器是什么?查看如何开机警告。复制粘贴您正在使用的代码。你给我们看的那一行没有编译,值得注意的是你修改了哪些行,并解释为什么需要它们。这将有助于o/p下次学习如何避免这些错误。当然可以。我是新来的。我会让它成为一种练习。谢谢你的反馈。