0

i am trying to create an program to generate empty files. but when it try to run the program it crashes after taking inputs from the console .

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int create(char* filename)
{
    char filext[10];
    printf("\nEnter File Extension :");
    fgets(filext);
        FILE* fp;
        fp = fopen(strcat(filename,strcat(".",filext)),"w");
        if(!fp)
        {
            return 0;
        }
        fclose(fp);
        return 1;

}
int main(int argc , char* argv[])
{
    int f;
    int i;
    char buffer[33];
    if (argc == 3)
    {
        for(i = 0; i < atoi(argv[2]) ; i++)
        {
            f = create(strcat(argv[1],itoa(i,buffer,10)));
            if(f==0)
            {
                printf("error in creating files . check uac!!!");
            }
            else{
                printf("\nfile Created ...\n");
            }
        }
    }
    else{
        printf("syntax Error");
    }
    return 0;
}

when I try to run this program I get the following output

F:\selfcreatedtools\filegen>gcc gen.c

F:\selfcreatedtools\filegen>a level 100

Enter File Extension :php

after entering the extension the program crashes. i am a beginner in c programming.

  • 1
    You can't do `strcat(argv[1], …)` because there isn't any space in `argv[1]` for extra material. You have to allocate the space somehow. Similarly for other lines where you use `strcat()`. And [`gets()` is too dangerous to be used, ever!](http://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used). – Jonathan Leffler Jul 15 '17 at 17:52
  • `argv[1]` and `"."` have no extra space to combine strings. – BLUEPIXY Jul 15 '17 at 17:52
  • 1
    Use of `gets()` is dangerous. Try `fgets()`. – J...S Jul 15 '17 at 17:53
  • can you give me an example. – Ankit Mishra Jul 15 '17 at 17:53
  • @J...S thank you for the information. I have changed it. – Ankit Mishra Jul 15 '17 at 17:54
  • E.g `char path[FILENAME_MAX + 1]; snprintf(path, sizeof path, "%s%d", argv[1], i);`... – BLUEPIXY Jul 15 '17 at 17:58
  • 1
    Also, `strcat(".",filext)` attempts to modify a string literal; they're often stored in readonly memory and a crash is a very common consequence. Look up [`snprintf()`](http://pubs.opengroup.org/onlinepubs/9699919799/functions/snprintf.html). Also, prompting for the extension is probably not the best interface; that should be a command line argument too. – Jonathan Leffler Jul 15 '17 at 18:00
  • `char filext[10+1]="."; scanf("%9s", filext+1); /* check length of result then*/ strcat(filename, filext);` – BLUEPIXY Jul 15 '17 at 18:02
  • Please explain *program crashes*. What do you mean? Do you get an error message? Exit with no message? Freezes? What? – lurker Jul 15 '17 at 18:09
  • @lurker it just stops responding. and terminates – Ankit Mishra Jul 15 '17 at 18:15
  • thank you @JonathanLeffler for letting me know. I know prompting for extension was so unprofessional. but I was in a hurry. i am going to update it. – Ankit Mishra Jul 15 '17 at 18:17
  • thank you @BLUEPIXY for the help. – Ankit Mishra Jul 15 '17 at 18:18
  • @JonathanLeffler can you provide a fix for this program. – Ankit Mishra Jul 15 '17 at 18:24
  • It needs more than one fix. Yes, I could provide a fixed variant of your program, but I won't. I wouldn't learn very much by doing so, and you'd be deprived of the learning opportunity. Plus you'd have to simplify what I provide because it would probably do things you haven't thought of or learned about yet. – Jonathan Leffler Jul 15 '17 at 18:56
  • it's ok @JonathanLeffler.and thanks too. – Ankit Mishra Jul 15 '17 at 19:04

1 Answers1

1

Your main problem lies in the strcat(".",filext) part of
fp = fopen(strcat(filename,strcat(".",filext)),"w");

Try

strcat(filename, ".");
strcat(filename, filext);
fp = fopen(filename, "w");


And it might be better if the function definition header was made int create(char filename[SIZE]) (where SIZE is a value less than the size filename will be) instead of int create(char* filename) since you are using strcat() to modify the string in the user-defined function create(). You wouldn't want illegal memory accesses that would cause errors if the string encroaches upon the memory allotted to something else.

A similar problem is there with using strcat() to modify the string at argv[1] as pointed out by Jonathan Leffler for which BLUEPIXY has provided a solution in the comments.

J...S
  • 5,079
  • 1
  • 20
  • 35