Click to See Complete Forum and Search --> : Need some help
Kohinoor24
September 24th, 2002, 10:36 AM
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::out|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..
PaulWendt
September 24th, 2002, 10:53 AM
If all you want to do is read every byte from an input-source to
an ofstream, this should work:
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
Kohinoor24
September 24th, 2002, 11:02 AM
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.
Kohinoor24
September 24th, 2002, 11:13 AM
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.
PaulWendt
September 24th, 2002, 12:42 PM
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:
const char* fileName = "c:\\directory\\subdirectory\\filename";
--Paul
PaulWendt
September 24th, 2002, 12:54 PM
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:
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
Paul McKenzie
September 24th, 2002, 01:00 PM
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
PaulWendt
September 24th, 2002, 01:38 PM
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.
jwbarton
September 24th, 2002, 03:51 PM
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
Kohinoor24
September 24th, 2002, 04:11 PM
Thanks for the advice Paul.It was a refreshing information about the slashes.
Kohinoor24
September 24th, 2002, 04:13 PM
I forgot to put this line.Thanks for answering my questions most of the times. :)
Paul McKenzie
September 28th, 2002, 07:03 PM
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
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.