简体   繁体   English

使用C中的用户定义名称编写输出文件

[英]Write an output file with a user defined name in C

I am new to C and am trying to define an output filename before the program runs. 我是C的新手,我试图在程序运行之前定义输出文件名。 I am getting a Bus error Here is my code: 我收到一个总线错误这是我的代码:

#include <stdio.h>

int main (int argc, const char * argv[]) {
    char fname[128];
    printf("Enter the file name\n");
    scanf("%123s",fname);
    strcat("/Users/user/Desktop/learn/", fname);
    strcat(fname, ".txt");

    FILE *fp;
    fp=fopen(fname,"a");
    fprintf(fp, "Testing... OK I think it worked\n");
    return 0;
}
  1. You didn't #include <string.h> for strcat . 对于strcat你没有#include <string.h>
  2. The first argument to strcat must be a pointer, not a string literal. strcat的第一个参数必须是指针,而不是字符串文字。
  3. strcat itself isn't safe, use strncat instead. strcat本身并不安全,请改用strncat
  4. Don't forget to check the result of scanf and fopen . 不要忘记检查scanffopen的结果。
  5. And close fp when you're done with it. 当你完成它时关闭fp
  6. The signature of main should be int main(int argc, char * argv[]) . main的签名应该是int main(int argc, char * argv[])

The use of scanf is also generally discouraged, use fscanf & sscanf instead. 一般不鼓励使用scanf ,而是使用fscanfsscanf

You are using a string literal as the destination pointer in the first call to strcat. 在第一次调用strcat时,您使用字符串文字作为目标指针。 so you are concatonating "/Users/user/Desktop/learn/" with fname and storing the result where ever "/Users/user/Desktop/learn/" was stored, which might not even be writable. 因此,您使用fname将“/ Users / user / Desktop / learn /”连接起来,并将结果存储在“/ Users / user / Desktop / learn /”存储的位置,甚至可能无法写入。

That's not how strcat() works. 这不是strcat()工作原理。

I see two approaches: 我看到两种方法:

  • Use fname correctly, together with the file name inputting: 正确使用fname ,并输入文件名:

     char fname[128]; strcpy(fname, "/Users/user/Desktop/learn/"); // ok as long as you don't make the 128 too small char * input = fname + strlen(fname); // now points after the final / printf("Enter the file name\\n"); scanf("%123s", input); // the 123 is probably not correct strncat(fname, ".txt", sizeof fname); 

    and use it. 并使用它。

    Currently, this approach is still suffering from the fact that input is limited to 123 bytes, which might be too large, so better forget it for now. 目前,这种方法仍然受到以下事实的影响:输入限制为123字节,这可能太大,所以最好暂时忘记它。 It is just for getting the idea. 这只是为了得到这个想法。

    Maybe fgets() might be better: 也许fgets()可能更好:

     fgets(input, sizeof(fname)-strlen(fname), stdin); 
  • Use command line parameters, which would be my favourite approach: 使用命令行参数,这将是我最喜欢的方法:

     // first check if argc is >= 2, ie if the caller has supplied an argument char fname[128]; strcpy(fname, "/Users/user/Desktop/learn/"); strncat(fname, argv[1], sizeof fname); strncat(fname, ".txt", sizeof fname); 

Try this, this is worked for me.. 试试这个,这对我有用..

http://cboard.cprogramming.com/c-programming/124576-whats-mean-char*-const*-argv.html

#include <stdio.h>
#include <string.h>
int main (int argc, const char*const* argv[]) 
{

char fname[128];
char path[] = "/home/abc/test/";
printf("Enter the file name\n");
scanf("%123s",fname);
strcat(fname,".txt");
strcat(path,fname);
FILE *fp;
fp=fopen(path,"a");    
fprintf(fp, "Testing... OK I think it worked\n");
fclose(fp);    
return 0;
}

Thanks for everyone's comments. 感谢大家的评论。 This was working code: 这是工作代码:

#include <stdio.h>
#include <string.h>
int main (int argc, const char*const* argv[]) 
{
char fname[128];
strcpy(fname, "/Users/user/Desktop/learn/"); 
char * input = fname + strlen(fname); 
printf("Enter the file name\n");
scanf("%s",  input); 
strncat(fname, ".txt", sizeof fname);
printf("The output pathway and file will be called %s\n", fname);
FILE *fp;
fp=fopen(fname,"a");    
fprintf(fp, "Testing... OK I think it worked\n");
fclose(fp);    
return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM