Ankit Mishra
Ankit Mishra

Reputation: 31

c file handling program is unable to create more than 2 files

This program doesn't create more than two empty files.

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

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

}
int main(int argc , char* argv[])
{
    int f;
    int i;
    char* j;
    char buffer[33];
    char filename[100];
    if (argc == 3)
    {
        for(i = 0; i < atoi(argv[2]) ; i++)
        {
            j = itoa(i,buffer,10);
            strcpy(filename,strcat(argv[1],j));
            f = create(filename);
            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 excute the file i get the following output.

F:\selfcreatedtools\filegen>gcc gen.c

F:\selfcreatedtools\filegen>a wander 4

Enter File Extension :php

file Created ...

Enter File Extension :mag

file Created ...

F:\selfcreatedtools\filegen>

as I pass argv[2] = 4 in the command line . according to the program loop has to iterate for 4 times. and provide 4 files as output. but my program generates 2 files and the loop stops iterating after that.

advance thanks for the solutions. and i am a beginner.

Upvotes: 0

Views: 71

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 753455

Here's a solution which doesn't prompt for the extension — you can provide it on the command line. It uses my error reporting functions, available in stderr.c and stderr.h from https://github.com/jleffler/soq/tree/master/src/libsoq.

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

static int create(const char *base, int numlen, int number, const char *dot, const char *extn)
{
    int rc = 0;
    char filename[1024];
    snprintf(filename, sizeof(filename), "%s%.*d%s%s", base, numlen, number, dot, extn);
    FILE *fp = fopen(filename, "w");
    if (fp == 0)
    {
        err_sysrem("failed to create file '%s': ", filename);
        rc = -1;
    }
    else
        fclose(fp);
    return rc;
}

int main(int argc, char **argv)
{
    err_setarg0(argv[0]);
    if (argc < 3 || argc > 4)
        err_usage("base number [ext]");
    char *base = argv[1];
    int number = atoi(argv[2]);
    const char *dot = ".";
    char *extn = argv[3];
    if (extn == 0)
    {
        dot = "";
        extn = "";
    }
    if (*extn == '.')
        dot = "";
    char buffer[32];
    int numlen = snprintf(buffer, sizeof(buffer), "%d", number - 1);
    int rc = EXIT_SUCCESS;
    for (int i = 0; i < number; i++)
    {
        if (create(base, numlen, i, dot, extn) != 0)
            rc = EXIT_FAILURE;
    }
    return rc;
}

It uses snprintf() to manage the string concatenation.

Example runs (the program was called gen29):

$ gen29 base 10
created: [base0]
created: [base1]
created: [base2]
created: [base3]
created: [base4]
created: [base5]
created: [base6]
created: [base7]
created: [base8]
created: [base9]
$ gen29 base 10 .ext
created: [base0.ext]
created: [base1.ext]
created: [base2.ext]
created: [base3.ext]
created: [base4.ext]
created: [base5.ext]
created: [base6.ext]
created: [base7.ext]
created: [base8.ext]
created: [base9.ext]
$ gen29 base 13 xtn
created: [base00.xtn]
created: [base01.xtn]
created: [base02.xtn]
created: [base03.xtn]
created: [base04.xtn]
created: [base05.xtn]
created: [base06.xtn]
created: [base07.xtn]
created: [base08.xtn]
created: [base09.xtn]
created: [base10.xtn]
created: [base11.xtn]
created: [base12.xtn]
$

If you really want to do the prompting in the file creation, be my guest and make the modifications, but IMO such a program is less useful than one that doesn't prompt.

Upvotes: 1

Related Questions