-
reading 1 char at a time
I have a binary file (a rom actualy) and i want to read it 1 char at a time, so that value is converted to a char and saved to another. So if the rom contained
8A 8B 8C (Š‹Œ) it would be saved as abc i have the program set up to change and save just not to read 1 char at a time
-
http://www.cppreference.com/cppio_details.html#get
.... and you may want to open the stream in binary mode.
-
One solution is read().
Code:
ifstream file;
file.open("fileName", iso::read | iso::binary);
file.seekg(0, ios::end);
int fileSize = file.tellg();
file.seekg(0, ios::beg);
char *pChar = NULL;
while (fileSize)
{
file.read(pChar, 1);
...
--fileSize;
}
Kuphryn
-
Thanks
thanks i think i no what to do. and i learnt a bunch of stuff :)
-
Is it me...
It is me or does this prog instantly crash?
Code:
#include <fstream>
int main()
{
int a = 0;
char *buf;
ifstream file;
ofstream file1;
file1.open("C:/file1.txt", ios::in);
file.open("C:/file.txt", ios::binary);
file.seekg(0, ios::end);
int fileSize = file.tellg();
while (a<fileSize)
{
a=a+1;
file.read(buf, 1);
file1<<buf;
}
return 0;
}
I cant see a thing wrong with the bits i fully understand lol
-
I myself pulled that same mistake not that long ago really.
The problem is thusly:
char* buf does not point to anything.
in order to use it like this:
you have to make it point to a location in memory, where read() can *put* the data.
umm.. kuphryn.. you didn't initialize your pointer either did you? :P
unless I missed something..
Of course since you are only reading one char at a time, would it not be easier to just use
and
which is better for single-character read anyway...
one other note, you left file at seekg(0, ios::end) didn't you?
You might want to set it back to the beggining of the file before you try to read stuff :)
note that in kuphryn's post the line that says
Code:
file.seekg(0, ios::beg)
-
LOL
so now i have:
Code:
#include <fstream>
int main()
{
int a = 0;
char buf;
ifstream file;
ofstream file1;
file1.open("C:/file1.txt", ios::in);
file.open("C:/file.txt", ios::binary);
file.seekg(0, ios::end);
int fileSize = file.tellg();
file.seekg(0, ios::beg);
while (a<fileSize)
{
a=a+1;
file.get(buf);
file1<<buf;
}
return 0;
}
doesnt crash out :) but doesnt work..
-
I perfer to give a general solution over a code that works. One reason is I want the person to understand the simple concept. Another reason is that it saves me time from looking up references and such.
krappykoder: I hope you understand the general solution everyone has mentioned. The algorithm is the same.
Kuphryn
-
>>doesnt crash out but doesnt work..
Well, what does it do then? Any errors etc, what symptoms?
>>file1.open("C:/file1.txt", ios::in);
Always check that the open worked.
-
yea
i have learned quite alot from these posts i had no idea on how to do this b4 now i only have 1 thign wrong with it...it dont work
-
bug
It does about the same as it did when the cursor was at the end: exits
i check wether it opend by seeing if the file was made lol
-
odd..
its doing something, but not writing to the second file
-
>>file1.open("C:/file1.txt", ios::in);
>>file1<<buf;
So, you opened it with ios::in, and then tried to write to it??
[edit]
Oh, you did this:
>>ifstream file;
how confusing!
-
lol
it wrote! 00 00 00 00 00 00 00 in fact 275kb worth of 00 00 00
:) its starting to work!
Id have a hard time trying to figure out what the problem is as i have no idea :)
-
first off it's probably a good idea to name your variables something less confusing...
for instance it would be easier to call the infile "in" and the outfile "out"..
anyways Hammer is right; you opened your output file *as* an input file by putting in this line:
Code:
file1.open("C:\file.txt", ios::in);
which makes it an in file.. I don't know why it worked though because since it's an ofstream, you shouldn't be able to open it as an infile.. hmm..
anyway my tips for you are;
name the variables something easier to remember
once you do that it should be allot easier to figure out the problem.