Hi,
I have a program which is hard coded, I need to enter a value(n2 in the below code) in the program, and the program processes the file according to the value n2.

how I compile now is as follows:
cc -o test testprog.c
./test [filename]

What I need to do is, modify the program in such a way that, the value n2 can be given as an argument at command line.

Can anyone please help me with this?

Here is the full code:

#include  "check_2ndkj.h"
int CS_write();
main(int argc,char *argv[])
{
int i_FValue;
FILE    *fp_MYFILE;
    if(argc != 2)
    {
        printf("\tErr -> command line\n");
        printf("\t MYFileName\n");
        exit(NO);
    }
    else
    {
        printf("\tCommand accepted  -> check_K source %s\n",argv[1]);
    }
/*                fopen                        */
    if(NULL==(fp_MYFILE=fopen(argv[1],"r")))
    {
        printf("file open Err! -> %s\n",argv[1]);
        exit(NO);
    }
/*CS_write()                    */
    i_FValue    =    CS_write(fp_MYFILE);
    if(i_FValue==NO)
    {
        printf("\tErr CS_write( )\n");
        exit(NO);
    }
/*            fclose                                    */
    fclose(fp_MYFILE);
}
/*        CS_write( )            */
CS_write(fp_MYFILE)
FILE    *fp_MYFILE;
{
unsigned char    data[128];
int i_KjNum,i_NetworkNum,i_PolygonNum,i_TNum,i_ZNum,i_SNum,i_KNum;
int i_cnt;
int n1,l1,n2,l2,n3;
int iSin,iIpo;
int i_1;
FILE *fp_out;
    if(NULL==(fp_out=fopen("Uxxxxxx","w")))
    {
        printf("file open Err! -> Uxxxxxx\n");
        exit(NO);
    }
    printf("\tCS_write_Sin( )\n");
    iSin=0;
    iIpo=0;
/*  store    */
    fread(data,sizeof(data),1,fp_MYFILE);
    memcpy(&i_KjNum,data+91,sizeof(i_KjNum));
    memcpy(&i_NetworkNum,data+95,sizeof(i_NetworkNum));
    memcpy(&i_PolygonNum,data+99,sizeof(i_PolygonNum));
    memcpy(&i_TNum,data+103,sizeof(i_TNum));
    memcpy(&i_ZNum,data+107,sizeof(i_ZNum));
    memcpy(&i_SNum,data+111,sizeof(i_SNum));
    memcpy(&i_KNum,data+115,sizeof(i_KNum));
    i_1=i_KNum-1;
    memcpy(&(data[115]),&i_1,sizeof(i_KNum));
    fwrite(data,128,1,fp_out);
/*check*/
printf("%d %d %d %d %d %d %d \n", i_KjNum,i_NetworkNum,i_PolygonNum,i_TNum,i_ZNum,i_SNum,i_KNum);

for(i_cnt=0;i_cnt<i_KjNum+i_NetworkNum+i_PolygonNum+i_TNum+i_ZNum+i_SNum;i_cnt++)
    {
        memset(data,0x00,128);
        fread(data,sizeof(data),1,fp_MYFILE);
        fwrite(data,128,1,fp_out);
    }
    for(i_cnt=0;i_cnt<i_KNum;i_cnt++)
    {
        fread(data,sizeof(data),1,fp_MYFILE);
        if((data[0]=='8')&&(data[1]=='2'))
        {
            memcpy(&l1,data+2,sizeof(int));
            memcpy(&l2,data+6,sizeof(int));
            memcpy(&n1,data+13,sizeof(int));
            memcpy(&n2,data+17,sizeof(int));
            memcpy(&n3,data+21,sizeof(int));
/*            if (n2 == 1854) */
/*            if (n2 == 1830)*/ 
            if (n2 == 41388    
{         
printf("\t<Sin> 1stNode [%d] 1stLink [%d] 2ndNode [%d] 2ndLink [%d] 3rdNode [%d]\n",n1,l1,n2,l2,n3);
            }
            else
            {
                fwrite(data,128,1,fp_out);
            }
            iSin++;
        }
        if((data[0]=='8')&&(data[1]=='1'))
        {
            fwrite(data,128,1,fp_out);
            iIpo++;
        }
    }
    printf("\t iIpo [%d] iSin [%d]\n",iIpo,iSin);
    fclose(fp_out);
    return(OK);
}

thanx
squirrel

Recommended Answers

All 28 Replies

I hope this is an old program you have that you need to change because it is written in ancient original K&R style.

There are several changes you have to make -- and below may or may not be all of them
(1) change if(argc != 2) to if(argc != 3) in function main()

(2) add variable n2 to main() and pass it as a parameter to CS_write(). Then somewhere in main set value of n2 = atoi(argv[2])

(3) In function CS_write(), add a new parameter n2, delete n2 as a local variable.

(4) delete this line: memcpy(&n2,data+17,sizeof(int));

And how does the program read the value n2?

do you know anything at all about computers and programming? just add the value of n2 on the command line after the file name

./test [filename] [n2]

example:
./test myfile.txt 25 <Enter>

>>how does the program read the value n2?

It doesn't read it at all. see the example in my example above and in my previous post -- the value of n2 is in argv[2]

Thankx for the help. And sorry for the stupid question. I have just started my carrer in programming, so its all confusing.

Thankx once again.

I have done all the changes u have mentioned, but the output is not coming in the correct way. This is the program after making the changes. Can u please suggest wat went wrong?

#include  "check_2ndkj.h"
int CS_write();
int n2;
main(int argc,char *argv[])
{
int i_FValue;
FILE    *fp_MYFILE;

n2=atoi(argv[2])
    if(argc != 3)
    {
        printf("\tErr -> command line\n");
        printf("\t MYFileName\n");
        exit(NO);
    }
    else
    {
        printf("\tCommand accepted  -> check_K source %s\n",argv[1]);
    }
/*                fopen                        */
    if(NULL==(fp_MYFILE=fopen(argv[1],"r")))
    {
        printf("file open Err! -> %s\n",argv[1]);
        exit(NO);
    }
/*CS_write()                    */
    i_FValue    =    CS_write(fp_MYFILE);
    if(i_FValue==NO)
    {
        printf("\tErr CS_write( )\n");
        exit(NO);
    }
/*            fclose                                    */
    fclose(fp_MYFILE);
}
/*        CS_write( )            */
CS_write(fp_MYFILE)
FILE    *fp_MYFILE;
{
unsigned char    data[128];
int i_KjNum,i_NetworkNum,i_PolygonNum,i_TNum,i_ZNum,i_SNum,i_KNum;
int i_cnt;
int n1,l1,l2,n3;
int iSin,iIpo;
int i_1;
FILE *fp_out;
    if(NULL==(fp_out=fopen("Uxxxxxx","w")))
    {
        printf("file open Err! -> Uxxxxxx\n");
        exit(NO);
    }
    printf("\tCS_write_Sin( )\n");
    iSin=0;
    iIpo=0;
/*  store    */
    fread(data,sizeof(data),1,fp_MYFILE);
    memcpy(&i_KjNum,data+91,sizeof(i_KjNum));
    memcpy(&i_NetworkNum,data+95,sizeof(i_NetworkNum));
    memcpy(&i_PolygonNum,data+99,sizeof(i_PolygonNum));
    memcpy(&i_TNum,data+103,sizeof(i_TNum));
    memcpy(&i_ZNum,data+107,sizeof(i_ZNum));
    memcpy(&i_SNum,data+111,sizeof(i_SNum));
    memcpy(&i_KNum,data+115,sizeof(i_KNum));
    i_1=i_KNum-1;
    memcpy(&(data[115]),&i_1,sizeof(i_KNum));
    fwrite(data,128,1,fp_out);
/*check*/
printf("%d %d %d %d %d %d %d \n", i_KjNum,i_NetworkNum,i_PolygonNum,i_TNum,i_ZNum,i_SNum,i_KNum);

for(i_cnt=0;i_cnt<i_KjNum+i_NetworkNum+i_PolygonNum+i_TNum+i_ZNum+i_SNum;i_cnt++)
    {
        memset(data,0x00,128);
        fread(data,sizeof(data),1,fp_MYFILE);
        fwrite(data,128,1,fp_out);
    }
    for(i_cnt=0;i_cnt<i_KNum;i_cnt++)
    {
        fread(data,sizeof(data),1,fp_MYFILE);
        if((data[0]=='8')&&(data[1]=='2'))
        {
            memcpy(&l1,data+2,sizeof(int));
            memcpy(&l2,data+6,sizeof(int));
            memcpy(&n1,data+13,sizeof(int));
          /*  memcpy(&n2,data+17,sizeof(int)); */
            memcpy(&n3,data+21,sizeof(int));
/*            if (n2 == 1854) */
/*            if (n2 == 1830)*/ 
 /*           if (n2 == 41388    
{         
printf("\t<Sin> 1stNode [%d] 1stLink [%d] 2ndNode [%d] 2ndLink [%d] 3rdNode [%d]\n",n1,l1,n2,l2,n3);
            }
            else
            {
                fwrite(data,128,1,fp_out);
            } */
            iSin++;
        }
        if((data[0]=='8')&&(data[1]=='1'))
        {
            fwrite(data,128,1,fp_out);
            iIpo++;
        }
    }
    printf("\t iIpo [%d] iSin [%d]\n",iIpo,iSin);
    fclose(fp_out);
    return(OK);
}

thankx
squirrel

Put the n2 = atoi(argv[2]); after you check to see if the program got enough arguments (the if(argc != 3) ).

Oh yeah . . . and add a semicolon. :)

1. you forgot to add n as a parameter

i_FValue    =    CS_write(fp_MYFILE,n2);

2.

CS_write(fp_MYFILE, n2)
FILE    *fp_MYFILE;
int n2;
{

And how does the program read the value n2?

The best way to answer these type of questions is to just try them:

int main(int argc, char *argv[])
{
    int param;

    printf("argc = %d \n", argc);
    for (param=0; param < argc; param++)
    {
        printf("%2d)  %s \n", param, argv[param]);
    }
    return 0;
}

Write a small program to test only the problem in question to understand how the specific technique works. Then once you understand it, go back to the program you're working on and incorporate what you've learned. This way you may in fact write 5 small programs instead of just one big one but you'll understand the specifics better and faster. You will have your information in minutes instead of hours or days...

thanks for the help.

Hi, Thanks for all the help u guyz have given.

The below program reads a file and produces a new file as an output. The output file is named FILENEW. What I want is, if the input file is test, the output file should be named test_new, If the input file is myprog, the output file should be named as myprog_new.

Can u help

#include  "check_2ndkj.h"
int CS_write();
main(int argc,char *argv[])
{
int i_FValue;
FILE    *fp_MYFILE;
    if(argc != 2)
    {
        printf("\tErr -> command line\n");
        printf("\t MYFileName\n");
        exit(NO);
    }
    else
    {
        printf("\tCommand accepted  -> check_K source %s\n",argv[1]);
    }
/*                fopen          */
    if(NULL==(fp_MYFILE=fopen(argv[1],"r")))
    {
        printf("file open Err! -> %s\n",argv[1]);
        exit(NO);
    }
/*CS_write()                    */
    i_FValue    =    CS_write(fp_MYFILE);
    if(i_FValue==NO)
    {
        printf("\tErr CS_write( )\n");
        exit(NO);
    }
/*            fclose            */
    fclose(fp_MYFILE);
}
/*        CS_write( )           */
CS_write(fp_MYFILE)
FILE    *fp_MYFILE;
{
unsigned char    data[128];
int i_KjNum,i_NetworkNum,i_PolygonNum,i_TNum,i_ZNum,i_SNum,i_KNum;
int i_cnt;
int n1,l1,n2,l2,n3;
int iSin,iIpo;
int i_1;
FILE *fp_out;
[B]    if(NULL==(fp_out=fopen("FILENEW","w")))
    {
        printf("file open Err! -> FILENEW\n");
        exit(NO);
    }[/B]
    printf("\tCS_write_Sin( )\n");
    iSin=0;
    iIpo=0;
/*  store    */
    fread(data,sizeof(data),1,fp_MYFILE);
    memcpy(&i_KjNum,data+91,sizeof(i_KjNum));
    memcpy(&i_NetworkNum,data+95,sizeof(i_NetworkNum));
    memcpy(&i_PolygonNum,data+99,sizeof(i_PolygonNum));
    memcpy(&i_TNum,data+103,sizeof(i_TNum));
    memcpy(&i_ZNum,data+107,sizeof(i_ZNum));
    memcpy(&i_SNum,data+111,sizeof(i_SNum));
    memcpy(&i_KNum,data+115,sizeof(i_KNum));
    i_1=i_KNum-1;
    memcpy(&(data[115]),&i_1,sizeof(i_KNum));
    fwrite(data,128,1,fp_out);
/*check*/
printf("%d %d %d %d %d %d %d \n", i_KjNum,i_NetworkNum,i_PolygonNum,i_TNum,i_ZNum,i_SNum,i_KNum);

for(i_cnt=0;i_cnt<i_KjNum+i_NetworkNum+i_PolygonNum+i_TNum+i_ZNum+i_SNum;i_cnt++)
    {
        memset(data,0x00,128);
        fread(data,sizeof(data),1,fp_MYFILE);
        fwrite(data,128,1,fp_out);
    }
    for(i_cnt=0;i_cnt<i_KNum;i_cnt++)
    {
        fread(data,sizeof(data),1,fp_MYFILE);
        if((data[0]=='8')&&(data[1]=='2'))
        {
            memcpy(&l1,data+2,sizeof(int));
            memcpy(&l2,data+6,sizeof(int));
            memcpy(&n1,data+13,sizeof(int));
            memcpy(&n2,data+17,sizeof(int));
            memcpy(&n3,data+21,sizeof(int));

            if (n2 == 41388 )   
        {         
        printf("\t<Sin> 1stNode [%d] 1stLink [%d] 2ndNode [%d] 2ndLink [%d] 3rdNode [%d]\n",n1,l1,n2,l2,n3);
                }
                else
                {
                fwrite(data,128,1,fp_out);
                }
            iSin++;
        }
        if((data[0]=='8')&&(data[1]=='1'))
        {
            fwrite(data,128,1,fp_out);
            iIpo++;
        }
    }
    printf("\t iIpo [%d] iSin [%d]\n",iIpo,iSin);
    fclose(fp_out);
    return(OK);
}

just use strcpy() and strcat() to create the new filename. you will have to truncate the file extension first. Now you will have to pass new_filename as another parameter to CS_write() function so that it can use it in the fopen() function instead of the hard-coded text.

char new_filename[512];
strcpy(new_filename,argv[1]);
// locate the file extension, if there is one
char *ext = strrchr(new_filename,'.');
// now truncate it
if(ext != NULL)
   *ext = 0;
// add _new
strcat(new_filename,"_new");
// add the file extension
ext = strrchr(argv[1],'.');
if(ext != NULL)
   strcat(new_filename,ext);

Thanks Ancient Dragon.

Is it like below wat u said, if there are no extensions to be added:

#include  "check_2ndkj.h"
int CS_write();
[B]char MyFile[512];[/B]
main(int argc,char *argv[])
{
int i_FValue;
FILE    *fp_MYFILE;
    if(argc != 2)
    {
        printf("\tErr -> command line\n");
        printf("\t MYFileName\n");
        exit(NO);
    }
    else
    {
        printf("\tCommand accepted  -> check_K source %s\n",argv[1]);
    }
[B]strcpy(MyFile,argv[1]);
strcat(MyFile,"_new");[/B]
/*                fopen          */
    if(NULL==(fp_MYFILE=fopen(argv[1],"r")))
    {
        printf("file open Err! -> %s\n",argv[1]);
        exit(NO);
    }
/*CS_write()                    */
    i_FValue    =    CS_write(fp_MYFILE);
    if(i_FValue==NO)
    {
        printf("\tErr CS_write( )\n");
        exit(NO);
    }
/*            fclose            */
    fclose(fp_MYFILE);
}
/*        CS_write( )           */
CS_write(fp_MYFILE)
FILE    *fp_MYFILE;
{
unsigned char    data[128];
int i_KjNum,i_NetworkNum,i_PolygonNum,i_TNum,i_ZNum,i_SNum,i_KNum;
int i_cnt;
int n1,l1,n2,l2,n3;
int iSin,iIpo;
int i_1;
FILE *fp_out;
    if(NULL==(fp_out=fopen([B]MyFile[/B],"w")))
    {
        printf("file open Err! -> [B]MyFile[/B]\n");
        exit(NO);
    }

>>Is it like below wat u said:

did you test it to verify whether it works or not ?

Yes, its not working. That is the reason I asked

Simplify first, then move on.

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

int main(int argc, char *argv[])
{
   if ( argc > 1 )
   {
      char filename[FILENAME_MAX];
      strcpy(filename, argv[1]);
      strcat(filename, "_new");
      printf("filename = \"%s\"\n", filename);
   }
   return 0;
}

/* my output
filename = "myprog_new"
*/

the input file is in some other location i.e.,
./myprog /data/tool2006/Myfile
so, when i do strcpy and strcat and give a printf statement the new filename is shown as /data/tool2006/Myfile_new
But I need only the file name not the whole path.

And how do i modify the unction CS_write()

int CS_write();
[B]char MyFile[512];[/B]
main(int argc,char *argv[])
{
int i_FValue;
FILE    *fp_MYFILE;
    if(argc != 2)
    {
        printf("\tErr -> command line\n");
        printf("\t MYFileName\n");
        exit(NO);
    }
    else
    {
        printf("\tCommand accepted  -> check_K source %s\n",argv[1]);
    }
[B]strcpy(MyFile,argv[1]);
strcat(MyFile,"_new");[/B]
/*                fopen          */
    if(NULL==(fp_MYFILE=fopen(argv[1],"r")))
    {
        printf("file open Err! -> %s\n",argv[1]);
        exit(NO);
    }
/*CS_write()                    */
    i_FValue    =    CS_write(fp_MYFILE);
    if(i_FValue==NO)
    {
        printf("\tErr CS_write( )\n");
        exit(NO);
    }
/*            fclose            */
    fclose(fp_MYFILE);
}
/*        CS_write( )           */
[B]CS_write(fp_MYFILE)
FILE    *fp_MYFILE;
{
unsigned char    data[128];
int i_KjNum,i_NetworkNum,i_PolygonNum,i_TNum,i_ZNum,i_SNum,i_KNum;
int i_cnt;
int n1,l1,n2,l2,n3;
int iSin,iIpo;
int i_1;
FILE *fp_out;
    if(NULL==(fp_out=fopen(MyFile,"w")))
    {
        printf("file open Err! -> MyFile[/B][B]\n");
        exit(NO);
    }[/B]
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[])
{
   if ( argc > 1 )
   {
      char newname[FILENAME_MAX];
      char *filename = strrchr(argv[1], '/');
      if ( filename )
      {
         strcpy(newname, filename + 1);
         strcat(newname, "_new");
         printf("newname = \"%s\"\n", newname);
      }
   }
   return 0;
}

/* my output
H:\Projects\Toolkit\stdc\test>test /data/tool2006/Myfile
newname = "Myfile_new"
*/

And how do i modify the unction CS_write()

Pass a file name as a char*, not a FILE*.


[further edit]Why are you using the ancient K&R syntax?

Thankx but I did not understand this

H:\Projects\Toolkit\stdc\test>test /data/tool2006/Myfile

Thankx but I did not understand this

My equivalent of this:

./myprog /data/tool2006/Myfile

ok. But the problem is the output is generated at the location of the input file, insated of the current directory

the input file is in some other location i.e.,
./myprog /data/tool2006/Myfile
so, when i do strcpy and strcat and compile the program, the output is generated in the path - /data/tool2006/Myfile_new

it is taking the whole path for strcpy, instead of only file name. how do i cpy only the file name

Please note the code I posted. It takes /data/tool2006/Myfile as an input filename and creates Myfile_new as an output filename. Isn't that what you are trying to do?

yes, but the file Myfile_new is generated in the path /data/tool2006/
I want it in the current directory, the directory from where Iam running the program.

Are you using the code I posted? What directory are you running it from? Are you sure it's not a leftover file? A little help here.

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

void foo(const char *readfilename, const char *writefilename)
{
   printf("readfilename  = \"%s\"\n", readfilename);
   printf("writefilename = \"%s\"\n", writefilename);

}

int main(int argc, char *argv[])
{
   if ( argc > 1 )
   {
      char newname[FILENAME_MAX];
      char *filename = strrchr(argv[1], '/');
      if ( filename )
      {
         strcpy(newname, filename + 1);
         strcat(newname, "_new");
         foo(argv[1], newname);
      }
   }
   return 0;
}

/* my output
H:\Projects\Toolkit\stdc\test>test /data/tool2006/Myfile
readfilename  = "/data/tool2006/Myfile"
writefilename = "Myfile_new"
*/

I understand all this, and everything is fine.

H:\Projects\Toolkit\stdc\test>test /data/tool2006/Myfile
readfilename = "/data/tool2006/Myfile"
writefilename = "Myfile_new"

but when u do ls -lrt, is it showing Myfile_new to u?? becoz for me it is not. it is showing when i give this :
ls -ltr /data/tool2006/

A: I'm on Windows.
B: The code I posted does not create a file, just the name.
C: Post your latest code so we can have some idea of what your latest code looks like.

[edit]To create the file, add such code.

void foo(const char *readfilename, const char *writefilename)
{
   FILE *readfile, *writefile;
   printf("readfilename  = \"%s\"\n", readfilename);
   printf("writefilename = \"%s\"\n", writefilename);
   readfile  = fopen(readfilename, "r");
   writefile = fopen(writefilename, "w");
   if ( readfile && writefile )
   {
      /* ... */
      fclose(readfile);
      fclose(writefile);
   }
}

See -- here's a good example of where to have a Delete My Post button[code deleted -- duplicate of Dave's original code] See -- here's a good example of where to have a Delete My Post button

Thanx a lot, Ancient Dragon and Dave Sinkula.
The program is workin fine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.