CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: Need some help

  1. #1
    Join Date
    Oct 2001
    Posts
    745

    Need some help

    This is with reference to my earlier post
    I have a file now in say, path: F:\Develop\FILE -where "FILE" is the file I have to read from.

    This path is stored in

    string R_path;
    I wanted to read from this "FILE" & put the contents(binary) in an ofstream;

    I did something as follows:

    ofstream m_ResourceGroup;
    m_ResourceGroup.open("ResourceGroup",ios_base:ut|ios_base::binary);

    char buffer[10];//Here I have to know the size of file,Iam giving 10 arbitarily.
    ifstream r2(R_path.c_str(),ios::in|ios::binary);
    r2.read(buffer,sizeof(buffer));
    m_ResourceGroup.write(buffer,10);

    Is there a better way of writing it?,because I have to know the size of buffer in the way I have done it & if I "cout" it shows some junk values behind the data.
    The code shouldn't be windows dependent.
    Thanks in advance..

  2. #2
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    If all you want to do is read every byte from an input-source to
    an ofstream, this should work:
    Code:
    ofstream outputFile("ResourceGroup");
    ifstream inputFile(inputString.c_str();
    outputFile << inputFile.rdbuf();
    I'm missing out some very important functions; before you use
    fstreams, use the member function is_open to make sure they
    opened successfully.

    --Paul

  3. #3
    Join Date
    Oct 2001
    Posts
    745
    will this "<<" operator overloaded for binary ouput as well,because the file Iam reading from(contains Binary Data) & the file Iam writing to is also opened for binary output.

  4. #4
    Join Date
    Oct 2001
    Posts
    745

    1 more doubt

    I have 1 more question to ask u.
    In the program Arguments
    Iam giving the file path as follows:

    string ResourceFile = "F:\Develop\FILE"
    Iam looking for the seperator as follows:

    string sep = "\";
    int n = ResourceFile.find_last_of(sep);

    but it shows me an error when I give backslash.
    But when I give the file path in forward slash,everything works.
    what is the right way of giving it.It takes the file when giving the forward slash.

    Cant I give backslash here.

  5. #5
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347

    Re: 1 more doubt

    Originally posted by Kohinoor24
    I have 1 more question to ask u.
    In the program Arguments
    Iam giving the file path as follows:

    string ResourceFile = "F:\Develop\FILE"
    Iam looking for the seperator as follows:

    string sep = "\";
    int n = ResourceFile.find_last_of(sep);

    but it shows me an error when I give backslash.
    But when I give the file path in forward slash,everything works.
    what is the right way of giving it.It takes the file when giving the forward slash.

    Cant I give backslash here.
    You know how you do a newline in a C-string? You use "\n". A
    \ is just an escape sequence. Great, so what if you want to use
    a backslash? All you have to do is do TWO \'s. Thus, if you want
    to specify a file on your C-drive:
    Code:
    const char* fileName = "c:\\directory\\subdirectory\\filename";
    --Paul

  6. #6
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    Originally posted by Kohinoor24
    will this "<<" operator overloaded for binary ouput as well,because the file Iam reading from(contains Binary Data) & the file Iam writing to is also opened for binary output.
    I have problems getting this method to work with executables
    on my system so I'm guessing it's not the all-purpose approach
    that you need. If you just need to know how much the input
    contains, you can use the tellg() function:
    Code:
       ifstream is("someFile.dat");
       istream::pos_type beginPos = is.tellg();
       is.seekg(0, ios::end);
       istream::pos_type endPos = is.tellg();
       is.seekg(0, ios::beg);
       int numBytes = endPos - beginPos;
    
       // create a char buffer and reserve the proper amount of space.      
       vector<char> temp;
       temp.resize(numBytes);
    
       // copy the contents from is.
       is.read(&temp[0], temp.size());
    I'm sorry if I steered you the wrong way; I didn't realize that
    binary handling was significant; I should have read better.

    --Paul

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449
    To add to Paul's answer:

    When specifying a filename in C or C++, you should, if possible, stay away from the double backslash as the directory seperator. When specifying a file name in C or C++, a single forward slash is the directory seperator, regardless of what the OS considers a directory seperator.

    So instead of:
    "C:\\Program\\ThisThat\\Temp.txt"

    it should be

    "C:/Program/ThisThat/Temp.txt"

    Just a little fact that a lot of programmers are unaware of.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    Originally posted by Paul McKenzie
    it should be

    "C:/Program/ThisThat/Temp.txt"

    Just a little fact that a lot of programmers are unaware of.
    ... including this one. Thank you, Paul McKenzie.

  9. #9
    Join Date
    Jan 2001
    Posts
    253
    In Windows, the directory separator character is defined as the backslash character '\', which (as PaulWendt indicated) in C/C++ can be specified in a literal string as '\\'.

    When calling native API functions, you can generally use the forward slash character '/' instead of the backslash character when specifying a path to a file (as Paul McKenzie indicated).

    However, sometimes the backslash character is needed due to the input requirements of programs that you are spawning from the current program (or from the command line). Due to choices made early in the days of MSDOS, the forward slash character is often used to specify a program option (instead of using the '-' character). This will sometimes result in programs which are parsing the command line to not interpret the path correctly, but instead to break up the path into a series of nonsense options to the program. For instance, the dir command does not like the forward slash character in paths.

    Best regards,
    John

  10. #10
    Join Date
    Oct 2001
    Posts
    745
    Thanks for the advice Paul.It was a refreshing information about the slashes.

  11. #11
    Join Date
    Oct 2001
    Posts
    745
    I forgot to put this line.Thanks for answering my questions most of the times.

  12. #12
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by jwbarton
    In Windows, the directory separator character is defined as the backslash character '\', which (as PaulWendt indicated) in C/C++ can be specified in a literal string as '\\'.

    When calling native API functions, you can generally use the forward slash character '/' instead of the backslash character when specifying a path to a file (as Paul McKenzie indicated).

    However, sometimes the backslash character is needed due to the input requirements of programs that you are spawning from the current program (or from the command line). Due to choices made early in the days of MSDOS, the forward slash character is often used to specify a program option (instead of using the '-' character). This will sometimes result in programs which are parsing the command line to not interpret the path correctly, but instead to break up the path into a series of nonsense options to the program. For instance, the dir command does not like the forward slash character in paths.

    Best regards,
    John
    You are right. That's why I stated "if possible". You can also add those times when you are getting the filename from a function you have no control over, and the filename may contain backslashes.

    Just as you stated, standard C and C++ functions / classes that deal with file I/O accept the forward slash. I would also suspect that the non-standard file functions such as opendir() / readdir() for UNIX, GetFirstFile / GetNextFile for Windows, also handle the forward slash.

    Regards,

    Paul McKenzie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured