My coding looks like this

int main(int argc, char *argv[])/*command line input*/
{
int len;
char *t[10];
len=strlen(argv[argc-1]);
temp[0]=argv[argc-1]
if(strstr(t[0],"c"==NULL)/*to check for a c file input*/
printf("Not a c file");
else
printf("C file");
}
if my command line input in linux is ./a.out test.c
argv[0] contains ./a.out ans argv[1] contains test.c

my problem is to name a the output file as test.i. how to name a file with the same name as input file with .i extension

Hey I feel there are some errors in your posted code:

int main(int argc, char *argv[])/*command line input*/
{
int len;
char *t[10];
len=strlen(argv[argc-1]);
temp[0]=argv[argc-1]
if(strstr(t[0],"c"==NULL)/*to check for a c file input*/
printf("Not a c file");
else
printf("C file");
}

In line 6 and 7 it should be as

t[0]=argv[argc-1];
if(strstr(t[0],"c")==NULL)/*to check for a c file input*/

And also I didn't get the usage of your "len" variable.You haven't used that for any productive usage.

And ya in LINUX based platforms a.out is not just an output file. The source file without any errors is correctly compiled to create an object file. This object file when loaded and linked properly an executable file is created which can be run and is called by a general name in LINUX called a.out.

Hey thanks for reply.
My problem is not with the code .
a.out is created properly. but in the design of a preprocessor for C I'm in the process of coding to devolp a C preprocessor. Hence for a sample test.c file which is to be preprocessed to produce test.i.
i.e if my input file is test.c then i need to write a code which names my output file as test.i (same name as input whatever it may be).

could you help me to write a code to name a output file same as the input file given as a command line argument?

To change file name extension try this code:

/** Find dot extension pos in the file path.
 *  Returns dot pos or end of name if no dot
 *          or 0 if it's not a file name
 */
const char* getFileNameDotPos(const char* fname)
{
    int i, ilast, slen;
    char c;

    if (fname == 0) return 0; /* No name */
    slen = (int)strlen(fname);
    ilast = slen - 1;
    for (i = ilast; i >= 0; --i) {
        c = fname[i];
        if (c == '.' || c == '/' || c == '\\')
            break;  /* backslash for Windows */
    }
    return
    i < 0
      ? fname + slen         /* name, null ext */
      : c == '.'
          ? fname + i        /* dot pos found  */
          : i == ilast
              ? 0            /* directory name */
              : fname + slen;/* path, null ext */ 
}

/** Change (append) file path dot+extension.
 *  Returns 0 if it's impossible otherwise echo newname on success.
 */
char* setNewDotExt(char* newname, int maxnewsz, const char* dotext, 
                   const char* fname)
{
    int nsize, xsize;
    const char* pdot = getFileNameDotPos(fname);
    if (pdot == 0 || newname == 0)
        return 0;
    if (dotext == 0) /* strip off extension */
        dotext = "";
    xsize = (int)strlen(dotext) + 1;
    nsize = (int)(pdot - fname);
    if (nsize + xsize > maxnewsz)
        return 0;
    if (nsize)
        memmove(newname,fname,nsize);
    memmove(newname+nsize,dotext,xsize);
    return newname;
}

No warranties ;)

This article has been dead for over six months. Start a new discussion instead.