2013-04-18 28 views
1

我是C新手,正在尝试在程序运行之前定义输出文件名。我得到一个总线错误 这里是我的代码:使用C中的用户定义名称编写输出文件

#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

您的'main()'签名是错误的。 – 2013-04-18 21:31:04

+2

对于同样是新手语言的新成员来说,这是相当苛刻的对待。这不像他要求你做他的作业什么的。对于一个隐含的问题,这是一个有效的问题,我们经验丰富的人应该能够回答并给出一些指导,说明他如何能够更好地提出问题以及如何学习语言。 –

回答

1

您正在使用一个字符串作为第一次调用strcat的目标指针。所以你可以通过fname连接“/ Users/user/Desktop/learn /”并将结果存储在永远存储在“/ Users/user/Desktop/learn /”中的地方,甚至可能不可写。

+0

我意识到这有很多错误,但感谢您的意见。我正在参加的这门课有我们向前迈进,期望熟悉C – Davis

3
  1. 您没有#include <string.h>对于strcat
  2. strcat的第一个参数必须是一个指针,而不是字符串文字。
  3. strcat本身不安全,请使用strncat代替。
  4. 不要忘记检查scanffopen的结果。
  5. 并且当你完成它时关闭fp
  6. main的签名应该是int main(int argc, char * argv[])

采用scanf一般也气馁,使用fscanf & sscanf代替。

0

这不是strcat()的工作方式。

我看到两种方法:

  • 使用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); 
    

    ,并使用它。

    目前,这种方法仍然受到输入限制为123字节这一事实的影响,这可能太大,所以现在最好忘记它。这只是为了得到这个想法。

    也许fgets()可能会更好:

    fgets(input, sizeof(fname)-strlen(fname), stdin); 
    
  • 使用命令行参数,这将是我最喜欢的办法:

    // first check if argc is >= 2, i. e. 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); 
    
+0

谢谢!第一种方法奏效。在尝试使用命令行参数之前,我需要做的更好。 – Davis

+0

@Davis在这种情况下,“使用命令行参数变得更好”是什么意思?这听起来像是一件大事 - 事实并非如此。 – glglgl

+0

这是一个家庭作业的任务,所以我使用了第一种方法,第二种方法需要更多关于c的知识,我还没有。 – Davis

0

试试这个,这是为我工作..

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

感谢大家的意见。这是工作代码:

#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; 
} 
+0

如果输入长路径名称,则会发生intresting事件。应该记录后验,这可能在幸运的情况下工作,但确实使用scanf()(从不这样做!),并且不正确地使用strncat()的最后一个参数。它也不会在fopen()之后检查strncat之后的错误。坏。 –

相关问题