使用C在Ubuntu中创建文件

时间:2021-08-04 01:18:58

Create file using C in Ubuntu. I've tried that code and it doesn't work.

在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;
}

3 个解决方案

#1


Your strcpy should be strcpy(command, "touch") and your strcat should be strcat(command, a)

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

But there are much better ways to create an empty file...

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

#2


  1. You forgot to pass command to strcpy and strcat.
  2. 你忘了将命令传递给strcpy和strcat。

  3. You are using the string "a" in the call to strcat instead of the variable a.
  4. 您在调用strcat而不是变量a时使用字符串“a”。

Replace

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

by

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

#3


I think you were trying to do the code in this way, though as Jeff told you it is not the best way to create a file-

我认为你试图以这种方式编写代码,尽管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;
}

#1


Your strcpy should be strcpy(command, "touch") and your strcat should be strcat(command, a)

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

But there are much better ways to create an empty file...

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

#2


  1. You forgot to pass command to strcpy and strcat.
  2. 你忘了将命令传递给strcpy和strcat。

  3. You are using the string "a" in the call to strcat instead of the variable a.
  4. 您在调用strcat而不是变量a时使用字符串“a”。

Replace

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

by

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

#3


I think you were trying to do the code in this way, though as Jeff told you it is not the best way to create a file-

我认为你试图以这种方式编写代码,尽管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;
}