|
-
May 17th, 2002, 06:01 PM
#1
Thread Starter
Lively Member
"Debug Assertion Failed" with fseek!!!
#include <fstream.h>
#include <stdlib.h>
#include <iostream.h>
#include <stdio.h>
void main(void) {
FILE *testafile;
char myfilechar;
testafile = fopen("D:\testfile.fil", "r");
myfilechar = (char) fseek(testafile, 4, 0);
fclose(testafile);
printf("%c", myfilechar);
}
When I was using fseek, an assertion error came up. Can someone help me? (I was using MSVC++6 Std, problems were fseek.c, line 146, and str != NULL.)
This is a strange little problem, so I would like some help.
Last edited by dubae524; May 17th, 2002 at 06:19 PM.
- Justin Patrick Butler
Comme je trouve. "As I find."
- Butler family quote
Beneficia sumptos procul superant. "The benefits far exceed the costs."
- Myself
-
May 17th, 2002, 06:18 PM
#2
Monday Morning Lunatic
fseek sets the position within the file. To read from it, use fread.
However, since you're in C++ the best way to read a file is through a stream:
Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ifstream input("D:\\testafile.fil"); // Also note that you need "\\"
string thing;
thing << input;
cout << "Read: " << input << endl;
return 0;
}
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 17th, 2002, 09:25 PM
#3
Thread Starter
Lively Member
OK, but what I'm trying to do with my program is to have it read from a specific address with-in the file. I already know this address, and the program will load from this specific address. (It will also save to it, but I am not dealing with that yet.) So, can you give me perhaps some C++ code to deal with it? since that is what you recommend.
- Justin Patrick Butler
Comme je trouve. "As I find."
- Butler family quote
Beneficia sumptos procul superant. "The benefits far exceed the costs."
- Myself
-
May 18th, 2002, 06:12 AM
#4
Monday Morning Lunatic
Code:
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
int main() {
// open the file
ifstream input("d:\\testafile.fil");
// absolute seek to position 4
input.seekg(4, ios_base::cur);
// allocate buffer to read into
char buf[4];
// clear data
memset(buf, 0, 4);
// read 4 bytes from the current position
input.read(buf, 4);
// echo them out
cout << "Read: ";
copy(buf, buf + 4, ostream_iterator<char>(cout, " "));
cout << endl;
return 0;
}
So it's fairly similar to using fseek, fread, etc., you just use it on a stream instead.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 18th, 2002, 04:10 PM
#5
Thread Starter
Lively Member
Still having problems
Firstly, mine apparently doesn't have the namespace thingy, nor does it have the algorithm header. All I want to do is load that one specific byte out of that one specific address, and nothing else. It will work with it in binary not ASCII. So is it possible that you can give me some assistance still?
- Justin Patrick Butler
Comme je trouve. "As I find."
- Butler family quote
Beneficia sumptos procul superant. "The benefits far exceed the costs."
- Myself
-
May 18th, 2002, 04:20 PM
#6
Monday Morning Lunatic
Umm...you said you were using VC++6? It should have both of those, because my copy of VC++5 has them.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 18th, 2002, 04:25 PM
#7
Thread Starter
Lively Member
Uh-uh (but I do have VC++6 Std), here is what the thing said:
d:\justin\c++\filetest\a.cpp(5) : fatal error C1083: Cannot open include file: 'algorithm.h': No such file or directory
So can you still help me?
- Justin Patrick Butler
Comme je trouve. "As I find."
- Butler family quote
Beneficia sumptos procul superant. "The benefits far exceed the costs."
- Myself
-
May 18th, 2002, 04:28 PM
#8
Monday Morning Lunatic
It's <algorithm>, not <algorithm.h>.
All the standard headers have no ".h" extension.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 18th, 2002, 04:35 PM
#9
Thread Starter
Lively Member
Yeah, I tried it out, it didn't have that error. But it still did not display what I was trying to get it to display. All it'd display was "Read: ".
What I'm trying to do, again, is to read a binary file, take a specific byte out of a specific address (like address 45, it will always and only take out the 45th character, byte, whatever out of it from the very beginning of the file). For the sake of testing it I want it to print out that specific byte.
Also I want to learn how to save to a specific byte, character, whatever. But I will save that till after I figure this one out.
- Justin Patrick Butler
Comme je trouve. "As I find."
- Butler family quote
Beneficia sumptos procul superant. "The benefits far exceed the costs."
- Myself
-
May 18th, 2002, 04:39 PM
#10
Monday Morning Lunatic
Ok, read in a single byte:
Code:
char buf = 0;
input.read(buf, 1);
cout << "Read: 0x" << hex << (int)buf << endl;
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 18th, 2002, 05:14 PM
#11
Thread Starter
Lively Member
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
void main(void) {
ifstream input("d:\filetest.fil");
char buf[1];
input.open("d:\filetest.fil");
input.read(buf, 4);
input.close();
cout << hex << (int) buf << endl;
}
Uh-uh, still having problems. This is the code ^ I implimented. The compiler doesn't complain of any errors, but it is still not displaying what I'm trying to get it to. (I tried using a single char, not an array, but it complained of a type mismatch {char to char *} with input.read, so I used an array.)
- Justin Patrick Butler
Comme je trouve. "As I find."
- Butler family quote
Beneficia sumptos procul superant. "The benefits far exceed the costs."
- Myself
-
May 18th, 2002, 05:27 PM
#12
Monday Morning Lunatic
Whoops sorry that was my fault. It should have been &buf (since read reads into a buffer that you supply a pointer to).
With your code, you'd need (int) buf[0].
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 18th, 2002, 05:40 PM
#13
Thread Starter
Lively Member
Uh-oh, it turns out that I had been using the wrong filename. It seems now that it didn't even care that I was. I corrected it, but still got it wrong. Sorry to keep bothering you like this, but I seem always to get it wrong. Here is the code:
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
void main(void) {
ifstream input("d:\testfile.fil");
char buf;
input.open("d:\testfile.fil");
input.read(&buf, 4);
input.close();
cout << hex << (int) buf << endl;
}
The output should be 76, but I keep getting ffffffcc, both after and even before I corrected the filename.
Sorry to keep bothering you like this, but can you continue to help me?
- Justin Patrick Butler
Comme je trouve. "As I find."
- Butler family quote
Beneficia sumptos procul superant. "The benefits far exceed the costs."
- Myself
-
May 18th, 2002, 05:46 PM
#14
Monday Morning Lunatic
Code:
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
void main(void) {
ifstream input("d:\\testfile.fil");
char buf;
// input.open("d:\testfile.fil");
input.read(&buf, 1);
input.close();
cout << hex << (int) buf << endl;
}
...just a couple of changes (in italics).
1: Use "\\t" in the filename since "\t" is a tab character.
2: It's already opened by the constructor, so no open is necessary.
3: Only read one byte since that's all the space you've given it.
NB: That "hex" manipulator on the stream means to print the hex value of what's in it, much as you'd see in a hex editor when you opened the file that way.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
May 18th, 2002, 05:53 PM
#15
Thread Starter
Lively Member
Thank you! Thank you so much Parksie! It finally works now ! I will try to figure out myself (as I usually try to do, and for this one should be easier since I know the basic premise) how to save to a specific address. (I was able to using input.seekg.)
Thank you!
- Justin Patrick Butler
Comme je trouve. "As I find."
- Butler family quote
Beneficia sumptos procul superant. "The benefits far exceed the costs."
- Myself
-
May 18th, 2002, 05:56 PM
#16
Monday Morning Lunatic
No probs. And remember, MSDN is your friend 
All you need to do is select the function/class name and hit F1
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|