使用C在Ubuntu中创建文件
在Ubuntu中使用C创建文件。 我试过那个代码,但它不起作用使用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; } #包括
#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;
}