1

I am new to C and am trying to define an output filename before the program runs. 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;
}
2
  • 1
    Your signature of main() is wrong. Commented Apr 18, 2013 at 21:31
  • 2
    That's pretty harsh treatment for a new member who is also new to the language. It's not like he's asking you to do his homework or something. It's a valid problem with an implicit question that we seasoned people should be able to answer and give some guidance on how he can become better at asking questions and how he can learn the language. Commented Apr 18, 2013 at 21:40

5 Answers 5

3
  1. You didn't #include <string.h> for strcat.
  2. The first argument to strcat must be a pointer, not a string literal.
  3. strcat itself isn't safe, use strncat instead.
  4. Don't forget to check the result of scanf and fopen.
  5. And close fp when you're done with it.
  6. The signature of main should be int main(int argc, char * argv[]).

The use of scanf is also generally discouraged, use fscanf & sscanf instead.

Sign up to request clarification or add additional context in comments.

Comments

1

You are using a string literal as the destination pointer in the first call to 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.

1 Comment

I realize this has a lot of mistakes, but thanks for your comments. This class I am taking has us jumping way forward with an expectation of familiarity with C
0

That's not how strcat() works.

I see two approaches:

  • Use fname correctly, together with the file name inputting:

    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. It is just for getting the idea.

    Maybe fgets() might be better:

    fgets(input, sizeof(fname)-strlen(fname), stdin);
    
  • Use command line parameters, which would be my favourite approach:

    // 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);
    

4 Comments

Thank you! The first method worked. I need to get better with command line parameters before I try that one.
@Davis What does "get better with command line parameters" mean in this context? This sounds like it was a big deal - which it isn't.
This was for a homework assignment, so I used the first method which worked and the second type requires a little more knowledge of c which I don't have yet.
@Davis I d on't think so. Handling user input in C is definitely more error-prone and challenging and requires more knowledge and care than just handling command line parameters. The latter ones are already in memory, while the former must be written into memory first.
0

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

Comments

-1

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

1 Comment

Intresting things happen if you type in long path names. It should be recorded for posteriority, that this might work in fortunate circumstances, but does use scanf() (never do this!) and does not use the last parameter of strncat() properly. It also does not check for errors after strncat, after fopen(). Bad.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.