Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.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/8/file/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
使用C在Ubuntu中创建文件_C_File - Fatal编程技术网

使用C在Ubuntu中创建文件

使用C在Ubuntu中创建文件,c,file,C,File,在Ubuntu中使用C创建文件。 我试过那个代码,但它不起作用 #include<stdio.h> #include<string.h> int main() { char a[50]; char command[150]; printf("Enter The File's Name"); gets(a); strcpy("touch "); strcat("a"); system(command); return 0; } #包括

在Ubuntu中使用C创建文件。 我试过那个代码,但它不起作用

#include<stdio.h> 
#include<string.h>
int main() 
{ 
  char a[50];
  char command[150];
  printf("Enter The File's Name");
  gets(a);
  strcpy("touch ");
  strcat("a");
  system(command);
  return 0;
}
#包括
#包括
int main()
{ 
chara[50];
char命令[150];
printf(“输入文件名”);
获得(a);
strcpy(“触摸”);
strcat(“a”);
系统(指挥部);
返回0;
}

您的strcpy应该是
strcpy(命令,“触摸”)
并且您的strcat应该是
strcat(命令,a)

但是有更好的方法来创建一个空文件


  • 您忘记将
    命令传递给
    strcpy
    strcat
  • 您在调用
    strcat
    时使用的是字符串
    “a”
    ,而不是变量
    a
  • 替换

      strcpy("touch ");
      strcat("a");
    


    我认为您试图以这种方式编写代码,尽管正如Jeff告诉您的那样,这不是创建文件的最佳方式-

    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
       char a[50],command[150];
       printf("Enter the file name: ");
       gets(a);
       sprintf(command,"touch %s",a);
       system(command);
       return 0;
    }
    
    #包括
    #包括
    int main()
    {
    字符a[50],命令[150];
    printf(“输入文件名:”);
    获得(a);
    sprintf(命令,“触摸%s”,a);
    系统(指挥部);
    返回0;
    }
    
    请打开编译器上的警告并阅读它们。您提供的代码根本不应该编译。您忘记将
    命令
    传递给
    strcpy
    strcat
    。非常感谢您的帮助。那最好的办法是什么呢?有几种。没有“最佳”方法,但有一种方法是FILE*f=fopen(“filename”,“w”);(并确保完成后关闭该文件)。我建议你自己买一本关于C编程的好书来帮助你入门。非常感谢。那么,创建文件的最佳方法是什么呢?对我来说,就是使用文件指针……我总是使用文件指针,因为在平台上命令可能会有所不同,因为触摸屏在windows中不起作用
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
       char a[50],command[150];
       printf("Enter the file name: ");
       gets(a);
       sprintf(command,"touch %s",a);
       system(command);
       return 0;
    }