Archived

This topic is now archived and is closed to further replies.

Malchivus

windows and files, why can't we get along?

Recommended Posts

i can''t get the functions to read file to work in visual c++ 6.0 on winXP. i treid the olde open() read() ones but those didn''t work. i tried _lread(wint openfile and lopen) and fread() and even fscanf() then casting characters into data(this was the only way i could write files btw) so now i''m trying to read the files and i can''t get them open without the program falling apart. i doubt the source would help if i posted it here because i''m copying it out of that cool c book by kerigan/ritchie. ie the sameples should work. i''m woried that this problem is realted to the NTFS or FAT32 systems and microsoft never developing bug free implementations of ANSI C file reading functions. i am currently at a university that doesn''t believe in dorm network or phone conections being useful for everyone (hey their british!) but i haven''t found a lab with diskdrives so i''m probaly going to want to copy it all to a notebook and take it back to my room. i''m using this for all file acess (levels animations, sound? dialouge, configuration) so i''m not anxious to use d3d texture loading methods, not that i know how, if i did i would. I just wanna get this done.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I doubt that the problem is in your Compiler / OS, else I am sure that someone else would have posted about this before.

I''d guess it is in your code, but without seeing anything, I can''t really help.

Share this post


Link to post
Share on other sites
#include <stdio.h>
#include <stdlib.h>

int main ()
{

FILE *file = NULL;
char data[500];

file = fopen("c:\\directory\\file.txt","rb");
fread(&data, 500, 1, file);
fclose(file);
file = NULL;

return 0;
}


notice the double ''\'' in the fopen() function !!!

- Iliak -

Share this post


Link to post
Share on other sites
quote:
Original post by iliak
char data[500];

file = fopen("c:\\directory\\file.txt","rb");
fread(&data, 500, 1, file);

- Iliak -


What you want is the address of data[0]

data is a pointer to the address of data[0]...
data == &data[0]

so what you want is either:
fread(data, 500, 1, file);
OR
fread(&data[0], 500, 1, file);



Share this post


Link to post
Share on other sites
after fopen() when i check the returnded value i get an error expresing that i have tried to allocate too much memory, the number it says is 4 less than 4gb(the limit for a 32 bit memory system) so my guess is that the function is trying to allocate an int or word and subtracting the number of bytes it needs from zero, this causes an overflow which causes this . . . but the code does look like that. is this something related to precompiled heades? i realy have no clue whats going on here

I just wanna get this done.

Share this post


Link to post
Share on other sites
quote:
Original post by Malchivus
after fopen() when i check the returnded value i get an error expresing that i have tried to allocate too much memory


excuse my french, but WTF?

fopen() returns a FILE HANDLE which you''ll use to actually read the file, with fread() (and yeah, close it with fclose() afterwards...)

post your code or learn to code.

Share this post


Link to post
Share on other sites
i'm gona go buy disketes so i can post it. all i need to show is the functions that use it right? so like:

function that calls the fileacess

fileacess i'm using

i like help, i apoligize if i'm making it to complex for anyone to help.

I just wanna get this done.

[edited by - malchivus on October 17, 2003 11:32:52 AM]

Share this post


Link to post
Share on other sites
heres the code for my functions, i have put macros around them because i was changing how they worked so often it didn''t make sense not to.

void LoadAnimation(char *Fname, CAnimation Var)
{
INFILE_TYPE InFile;

//open a file for input
OPEN_INFILE_TYPE(InFile,Fname);

if (INFILE_BAD(InFile))
{
LastErr = "Couldn''t open the passed animation file";
return false;
}

//read the number of animations
ACESS_INFILE(InFile,&(Var->m_iAnimationsInTotal),sizeof(int));

//write the number of frames
ACESS_INFILE(InFile,&(Var->m_iFramesInTotal),sizeof(int));

//there will be more but right now not even this much works
}

//i used macros because it was easier to swap back and fourth between functions with them
typedef int INFILE_TYPE;

#define CLOSE_INFILE_TYPE(FILE_VAR) close(FILE_VAR)
#define OPEN_INFILE_TYPE(FILE_VAR,STR) FILE_VAR = open(STR,O_RDWR,0)
#define INFILE_BAD(FILE_VAR) false(FILE_VAR == -1)
#define ACESS_INFILE(FILE_VAR,MEM,SIZE) read(FILE_VAR,MEM,SIZE)


typedef FILE *INFILE_TYPE;

#define CLOSE_INFILE_TYPE(FILE_VAR) fclose(FILE_VAR)
#define OPEN_INFILE_TYPE(FILE_VAR,STR) FILE_VAR = fopen(STR,"rb")
#define INFILE_BAD(FILE_VAR) (FILE_VAR == NULL)
#define ACESS_INFILE(FILE_VAR,MEM,SIZE) fread(MEM,1,SIZE,FILE_VAR)
//#define ACESS_INFILE(FILE_VAR,MEM,SIZE) SketchWinFileReadHack(FILE_VAR,MEM,SIZE)

//and just in case you want this . . . i had to wrie the files ina simmilar way
void SketchWinFileReadHack(FILE *Source, void *Data, unsigned int Size)
{
unsigned char *buff;
unsigned int pozish;

buff = (unsigned char *) Data;


pozish = 0;
while (pozish < Size)
{
buff[pozish] = fgetc(Source);
pozish ++;
}
}

I just wanna get this done.

Share this post


Link to post
Share on other sites
that thing CAnimation is actualy a pointer. the items refrenced or whatever are both ints. the message board seems to have erased all my neat tabs

I just wanna get this done.

Share this post


Link to post
Share on other sites
oh is there anyway to get rid of that "balh blah blah were microsoft. you are using a learning edition" box that hapens when i start my programs? it makes it impossible to write any dlls that don''t seem to mess up directX

I just wanna get this done.

Share this post


Link to post
Share on other sites
Try cut and pasting llyod''s code to start with. Take what he wrote and try it in a brand new project.

The macro thing you have looks complicated, though i don''t see any obvious errors in it. You are only using one of those two at a time, right? I usually go with FILE and don''t mess with any wrapping of it.


As for your program''s splash screen:
Project->Settings:
c/c++:
category(dropdown)->customize:
suppress startup banner.
I actually don''t know if this is the right thing. It''s an option that is always clicked off for me, and turning it back on didn''t change my program or MSVC. It could just be part of the student version you have.

can you access a file you know you have like so:?
//------------------
#include <stdio.h>
int main()
{
FILE *file;
file = fopen( "c:\\autoexec.bat","rt" );
if( file ) printf("It worked\n");
else printf("It didn''t work.\n");
}
//------------------

Share this post


Link to post
Share on other sites
ok i''v been a bit stupid with this . . when i ran the program i usualy copied the file i wanted to read into the /debug and then clicked ''run'' in msvc, this made the home directory the parent (not debug) so it wasn''t finding the file, i then tried the lopen stuff and at that point added error checking . . don''t know why that made so many complaints but . . finaly the line fgetc didn''t read like that until recently so when i used the fscanf i think it must have goten something weird which is why later on (erp) the program was trying to allocate 4gb of memory.

my lesson to myself: always make sure i''m running from the righ directory before i try to debug, and make sure i isolat the error

i hate how whenever i post on here its a question i should already know the answer too . . well except for the fwrite thing, but that was easy enough to work around

Share this post


Link to post
Share on other sites