Thread: Simple Question

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    11

    Simple Question

    Hi,

    In my program, I am using argument. I am entering id number and directory path for the files as an argument. I am using this argument and concat to filename. Then I will look for file, if it found than procceed otherwise give message that file is not found. I am getting difficulty.

    Here is what I use:

    #define MAX 25

    FILE *fst1, *fst2, *fst3;
    FILE *outfile;
    char Store1[MAX];
    char Store2[MAX];


    printf(" argv is %s\n",argv[2]);

    if(strcpy(Store1,argv[2])!= NULL)
    printf(" path is %s\n",Store1);
    if(strcat(Store1,"/file1")!= NULL)
    printf(" Store1 full path is %s\n",Store1);

    printf(" argv is %s\n",argv[2]);
    if(strcpy(Store2,argv[2])!= NULL)
    printf(" path is %s\n",Store2);
    if(strcat(Store2,"/file2")!= NULL)
    printf(" Store2 full path is %s\n",Store2);

    fst1 = fopen(Store1, "rb");
    fst2 = fopen(Store2, "rb");
    fst3 = fopen("file3", "rb");

    if (!fst1)
    {
    printf ("File is not found or can not be opened for reading\n");
    }
    else
    { ......



    Here is output

    %cc test.c
    % a.out 1 /usr/home
    argv is /usr/home
    path is /usr/home
    Store1 full path is /usr/home/file1
    argv is /usr/home
    path is /usr/home
    Store2 full path is /usr/home/file2
    File is not found or can not be opened for reading

    My files is in /usr/home directory; however, I am getting this message "File is not found or can not be opened for reading". If I do for one file - Store1. it works fine.

    printf(" argv is %s\n",argv[2]);

    if(strcpy(Store1,argv[2])!= NULL)
    printf(" path is %s\n",Store1);
    if(strcat(Store1,"/file1")!= NULL)
    printf(" Store1 full path is %s\n",Store1);

    only this part. Then it works fine. It found file and it goes else stmt.

    I will be really appreciate, if you can help me.

  2. #2
    Registered User
    Join Date
    Dec 2002
    Posts
    11
    Thanks for the suggestion.

    I tried that.

    for Store1, it gives me
    Store1: No such file or directory

    for others, it doesn't give me any message.

    I checked again in that directory. File is still there, and also read-write permission.

    If I remove Store2 stuff from my program, then it founds Store1.

    So I think, when I copy argv[2] to Store2, it changes Store1 value.

    Please help me.

  3. #3
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Originally posted by John22
    So I think, when I copy argv[2] to Store2, it changes Store1 value.
    John,

    I see nothing in your code that would change the value of Store1 after you print its value. I know you've checked this, but it looks as if /usr/home/file1 does not exist.

    I have two thoughts: Is the file in /usr/home or in /usr/home/john22 (or whatever your login name is)? Also, file names in *nix are case sensitive, so file1 != File1. Can you paste the output of this unix command for us?

    ls -alF /usr/home/file1

    Thanks.
    Jason Deckard

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    11
    Hi Guys,
    Thanks for your response. I solved my problem. Instead of using strcat and strcpy, I used sprintf(buff,"%s/file1",argv[x]); for all files and it worked fine.

    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 08:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-20-2005, 11:41 PM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM