Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Passing a file path as argument and reading it

learnClearnC Posts: 9Member
Hi all,
I need to write an utility that takes a file name or file path as argument, open and read the file. Then I need to convert it to binary and write it to another file.
I could not figure how I could pass a file path (ex:C:/usr/temp/Cprograms/prog1.c). When a path like this is given how do I read it and open the file.
For writing to the file can I use fwrite(). Is there any other method to do this.

Thanks



Comments

  • dwccgcdwccgc Posts: 576Member
    : Hi all,
    : I need to write an utility that takes a file name or file path as argument, open and read the file. Then I need to convert it to binary and write it to another file.
    : I could not figure how I could pass a file path (ex:C:/usr/temp/Cprograms/prog1.c). When a path like this is given how do I read it and open the file.
    : For writing to the file can I use fwrite(). Is there any other method to do this.
    :
    : Thanks
    :
    :
    :
    :
    [blue]
    [b]I need to write an utility that takes a file name or file path as argument[/b]
    This should get you started
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html/_pluslang_Argument_Definitions.asp
    [/blue]
  • learnClearnC Posts: 9Member
    : : Hi all,
    : : I need to write an utility that takes a file name or file path as argument, open and read the file. Then I need to convert it to binary and write it to another file.
    : : I could not figure how I could pass a file path (ex:C:/usr/temp/Cprograms/prog1.c). When a path like this is given how do I read it and open the file.
    : : For writing to the file can I use fwrite(). Is there any other method to do this.
    : :
    : : Thanks
    : :
    : :
    : :
    : :
    : [blue]
    : [b]I need to write an utility that takes a file name or file path as argument[/b]
    : This should get you started
    : http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html/_pluslang_Argument_Definitions.asp
    : [/blue]
    :
    Thanks..When I pass the file path name as an argument how could I strip off the extension and the directory path. I just need the filename.
  • istrasciistrasci Posts: 107Member
    : : : Hi all,
    : : : I need to write an utility that takes a file name or file path as argument, open and read the file. Then I need to convert it to binary and write it to another file.
    : : : I could not figure how I could pass a file path (ex:C:/usr/temp/Cprograms/prog1.c). When a path like this is given how do I read it and open the file.
    : : : For writing to the file can I use fwrite(). Is there any other method to do this.
    : : :
    : : : Thanks
    : : :
    : : :
    : : :
    : : :
    : : [blue]
    : : [b]I need to write an utility that takes a file name or file path as argument[/b]
    : : This should get you started
    : : http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclang/html/_pluslang_Argument_Definitions.asp
    : : [/blue]
    : :
    : Thanks..When I pass the file path name as an argument how could I strip off the extension and the directory path. I just need the filename.
    :

    Use strstr() function for C-style strings (char* -- man strstr) or use the member function substr() if you are using C++ strings (std::string -- see http://www.msoe.edu/eecs/cese/resources/stl/string.htm)
  • stoberstober Posts: 9,765Member ✭✭✭
    : :
    : Thanks..When I pass the file path name as an argument how could I strip off the extension and the directory path. I just need the filename.
    :

    copy it into another buffer
    [code]
    int main(int argc, char* argv[])
    {
    char buffer[255];
    // locate end of path, which is the last slash in the string
    char *ptr = strrchr(argv[0],'\');
    // copy remainder of string
    strcpy(buffer,ptr+1);
    // strip file extension
    ptr = strchr(buffer,'.');
    // if the extension exists, truncate it
    if(ptr != 0)
    *ptr = 0;


    }
    [/code]
  • learnClearnC Posts: 9Member
    : : :
    : : Thanks..When I pass the file path name as an argument how could I strip off the extension and the directory path. I just need the filename.
    : :
    :
    : copy it into another buffer
    : [code]
    : int main(int argc, char* argv[])
    : {
    : char buffer[255];
    : // locate end of path, which is the last slash in the string
    : char *ptr = strrchr(argv[0],'\');
    : // copy remainder of string
    : strcpy(buffer,ptr+1);
    : // strip file extension
    : ptr = strchr(buffer,'.');
    : // if the extension exists, truncate it
    : if(ptr != 0)
    : *ptr = 0;
    :
    :
    : }
    : [/code]
    :

    Thanks a Lot.
Sign In or Register to comment.