Sign in to follow this  
MichaelJackson

(C++ VC6) File I/O aHEEHEEE

Recommended Posts

MichaelJackson    106
shamona >.< After being comfortable with file i/o management *aHEE* in a console/DOS environment, I tried to take it up a notch and work it through a dynamic DLL *gimme the damn children* - but it crashs no matter what, were the file interfaces ment for a console environment only? I've tried using fcntl.h,iostream, mmio AND FileOpen functions *HOOEEEEEEE* It just crashs! (Where as it works in Console) It is very frustrating, when I thought I was comfortable - all I want to do is read from a BINARY file (None of the text file bs) using a struct. I have used the MSDN Thank you children!

Share this post


Link to post
Share on other sites
MichaelJackson    106
Aheehee Michael Jackson is love


#include <FCNTL.h>
#include <io.h>
#include <STDIO.H>
#include <sys\stat.h>

struct EGFileStructure
{
unsigned short CharLen;
int *Char;
};

void Whatever(void)
{
EGFileStructure file;
int fd=open("data.dat",_O_WRONLY | _O_CREAT | _O_BINARY, _S_IREAD |
_S_IWRITE );
file.Char = new int[32];

file.CharLen = sizeof(file.Char) / sizeof(int);

for(int i=0;i<file.CharLen;++i)
file.Char[i] = i;

write(fd,&file.CharLen,sizeof(unsigned short));
write(fd,file.Char,file.CharLen * sizeof(int));

close(fd);
free(file.Char);
}


int main(int argc, char* argv[])
{
Whatever();
return 0;
}


Nobody can seem to help me man! I'm hooked on this stuff and no one wants to give me a hand! Its inacurate to, the sizeof feeling man!

This code works in a console environment, however when I work it through a Win32 APP (OR Dynamic DLL) it crashs at _open.

Michael Jackson never mis-uses things... this is getting personal.

*moon walks out*

[Edited by - MichaelJackson on March 20, 2008 3:53:09 AM]

Share this post


Link to post
Share on other sites
daviangel    604
Quote:
Original post by MichaelJackson

Nobody can seem to help me man! I'm hooked on this stuff and no one wants to give me a hand! Its inacurate to, the sizeof feeling man!


Michael Jackson never mis-uses things... this is getting personal.

*moon walks out*

I don't think inserting *HOOEEEEEEE* or *aHEE* every other line is gonna get you any help or mentioning the fact that you are still using VC 6!

Share this post


Link to post
Share on other sites
Evil Steve    2017
Step 1: Take VC6 CD and set it on fire
Step 2: Throw burnt CD away
Step 3: Download Visual C++ 2008 Express Edition
Step 4: Install
Step 5: Compile your app using VC2008 and see what errors you get.

VC6 is not a C++ compiler. It came out before the first C++ standard, it's iostreams library is total crap, it's STL implementation has bugs in it, the compiler doesn't obay simple scoping rules (for loop scoping is screwed), the compielr frequently crashes when trying to compile templates, the compiler often crashes when compiling normal (non-template) code. The compiler can generate broken output.

Seriously, get rid of VC6 and move to a compiler that's less than a decade old.

EDIT:
Quote:
Original post by MichaelJackson
Aheeeheee. I'm too poor to upgrade to a newer computer system, nor have the capacity to download the express editions simply because their too big :)
You can request them on CD too. Really, if you're doing C++ programming, you need a suitable C++ compiler. VC6 is not a suitable C++ compiler.

Share this post


Link to post
Share on other sites
MichaelJackson    106
OK! In all seriousness here -

I have tried running 2005! FOR ALL THE LIFE OF ME - It will not run at the point where it is USEABLE. The IDE alone is VERY VERY VERY VERY slow - I have two horrible computers, one was purchased in 1999 other one was 2002. The fastest is an 800 MHz computer with 128 MB ram with onboard graphics (yay me)

VC6 is an environment that does not SLOW down at all for myself, I am able to use it effeciently. If I had a new computer I'd GLADLY jump into 2008. But for the life of me - All I originally asked was how to do File I/O; not being told to upgrade to future versions of the VS environment.

Thank you all - really. But seriously - all I want is to be able to peform file i/o operations within a dynamic library. Are there an alternates to do this then?

It makes sense with what you said Steve.

Share this post


Link to post
Share on other sites
ToohrVyk    1595
Quote:
Original post by MichaelJackson
Thank you all - really. But seriously - all I want is to be able to peform file i/o operations within a dynamic library. Are there an alternates to do this then?


There should be no issue with using iostream in a dynamic library, in C++. If you encounter an issue, please provide a complete minimal example to demonstrate it.

Share this post


Link to post
Share on other sites
DigitalBeing    122
How about dev-cpp it's small (13.mb) and uses gcc(yes an old one) but still i think it's better than vc6.

http://www.bloodshed.net/devcpp.html

Of course you could just put linux in the system and have everything you want

Share this post


Link to post
Share on other sites
Spoonbender    1258
Quote:
Original post by MichaelJackson
I have tried running 2005! FOR ALL THE LIFE OF ME - It will not run at the point where it is USEABLE. The IDE alone is VERY VERY VERY VERY slow - I have two horrible computers, one was purchased in 1999 other one was 2002. The fastest is an 800 MHz computer with 128 MB ram with onboard graphics (yay me)

VC6 is an environment that does not SLOW down at all for myself, I am able to use it effeciently. If I had a new computer I'd GLADLY jump into 2008. But for the life of me - All I originally asked was how to do File I/O; not being told to upgrade to future versions of the VS environment.

Yes, but what you are asking is "how can I do file I/O in an imaginary broken language that no one else uses or knows".
We can tell you how to do file I/O in C++, but since you don't have a C++ compiler, that's pointless. It may or may not work, because the think that VC6 compiles has a certain similarity to C++, but is far from the same thing.
So quite simply, if you want to ask for advice, use a language people can help you with. Such as C++. Which requires you to use a C++ compiler.

With that said, VC2005 *is* a slow IDE. I've got two suggestions:
1: Try Code::Blocks
2: Use the VC2005 compiler from a *different* IDE.

(And no, don't use dev-cpp. Please. It's hardly better than vc6)

Share this post


Link to post
Share on other sites
fmatak    536
Here is my 2 cents..

1. he is trying to do basic FILE I/O, and his code is essentially C code, with cpp extension. So, it should even work on VC 4-5.

2. instead of using open/close, i would prefer fopen/fclose/fwrite.

3. It seems there is a misassumption in the code, which causes the problem.

file.Char = new int[32];
file.CharLen = sizeof(file.Char) / sizeof(int);

----- in the above line, sizeof(file.Char) will return 4, which is equal to sizeof( int ), which in turn makes file.CharLen = 1. You should just put 32 there.

for(int i=0;i<file.CharLen;++i)
file.Char[i] = i;

Since charlen is 1, it will only set first element.

write(fd,&file.CharLen,sizeof(unsigned short));

this will write 1 to the file.

write(fd,file.Char,file.CharLen * sizeof(int));

This will write the first integer to the file.

changing the above mentioned part to 32 should fix your problems.

(NOTE: I did not test the code, so I may be wrong... )

Share this post


Link to post
Share on other sites
Evil Steve    2017
Yeah, I'd personally use the C-style IO, like fmatak (I just don't like the iostreams stuff for some reason, and I'm used to fread, etc).
I wouldn't use the low level file IO functions (open / read / write / etc), fopen, fread, fwrite etc are slightly simpler to use, and they're buffered so they'll be more efficient.

There's no reason your code shouldn't work in a DLL when it works as a console app; it's more worrying that it doesn't work in a Win32 app though, since to all intents and purposes a console app and a Win32 app are the same thing.

When you say it "crashes", what exact error do you get? Is it an access violation? If so, what are the full details? Finally, are you runnign the app through the IDE, or by double clicking the exe in explorer? Running it through the IDE (With F5) is preferable, since it'll let you debug the app more easily if it crashes.

Share this post


Link to post
Share on other sites
MichaelJackson    106
Thats just it - I only know it crashs with using _open in a Win32 app. There is no specific error, it looks like a compiler error - I have tried other peoples codes with file access. One managed to write a file in a win32 environment but failed to write any data to it.

I've given up and decided to download just VC 2008 (If the IDE is too laggy my friend suggested to use Notepad++ or whatever its called), 89 Megs isn't THAT much of a wait - I suppose I'll keep the computer when I'm heading out.

Thank you all and sorry about this. I'll try compiling under VC 2008! Thank you very much.

Share this post


Link to post
Share on other sites
Antheus    2409
I'd say it crashes for a large variety of reasons, compiler being the least of them:
file.Char = new int[32];
....
free(file.Char);

It's either new/delete or malloc/free.

file.CharLen = sizeof(file.Char) / sizeof(int);

This has constant value of 1 on 32 bit machines.

int fd=open("data.dat",_O_WRONLY | _O_CREAT | _O_BINARY, _S_IREAD | 
_S_IWRITE );
Shouldn't that be fopen()? Unless you're using some library, open doesn't exist.

    write(fd,&file.CharLen,sizeof(unsigned short));
write(fd,file.Char,file.CharLen * sizeof(int));

Same for these. And calling _SOMETHING is usually not recommended.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this