File Opening Throws a Illegal Op.

This is a discussion on File Opening Throws a Illegal Op. within the C++ Programming forums, part of the General Programming Boards category; Hello, I haven't programmed in a while and I am trying to make a simple flashcard type program to drill ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    162

    Question File Opening Throws a Illegal Op.

    Hello,
    I haven't programmed in a while and I am trying to make a simple flashcard type program to drill myself on a foreign language.

    However for the open file function, to open the flashcard text files, it keeps throwing an Illegal Op. message in Kernel32.dll. Here is the function, which I know is where the problem is at least occuring, if not the only place with trouble.

    Specifically I have narrowed the fault down to the last fseek() function, if that helps.
    Code:
     
    void OpenCards(HWND hwnd, char *filename)
    {
                   FILE *file = NULL;
    			   fpos_t length = 0;
    			   char *buff = NULL;
    
    			   file = fopen(filename,"rt");
    					if(!file)
    					{
    						MessageBox(hwnd,"Couldn't load file!","Error",MB_OK | MB_ICONERROR);
    						return;
    					}
    				fseek(file,0,SEEK_END);
    				fgetpos(file,&length);
    
    				buff = new char[(int)length + 1];
    					if(!buff)
    					{
    						MessageBox(hwnd,"Couldn't load OurFile.txt","Error",MB_OK | MB_ICONERROR);
          						return;
    					}
    			    fseek(file,0,SEEK_SET); //this is where the fault is
                    fread(buff,(int)length,1,file);
    				buff[(int)length] = NULL;
    				fclose(file);
        ProcessCards(hwnd, buff);
    }
    Last edited by Crossbow; 08-10-2004 at 04:23 PM.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,652
    If fopen() is blow'n chow then "filename" must be a bad pointer.

    Or maybe it's all those TAB characters in your source code

    gg

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    try these changes...

    Code:
    if((file = fopen(filename,"rt")) == NULL)
    {
    	MessageBox(hwnd,"Couldn't load file!","Error",MB_OK | MB_ICONERROR);
    						return;
    }
    fseek(file,0,SEEK_END);
    length = ftell(filename);
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  4. #4
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    Mode in fopen should be "r", not "rt":
    Code:
    file = fopen(filename, "r")

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,652
    There is nothing wrong with "t" as long as it isn't the first mode character seen and you're using the MS C standard lib fopen().

    You should be using ftell() instead of fgetpos(). The fpos_t type is only defined for use by fgetpos() and fsetpos(). For instance, a standard lib implementation is free to make fpos_t a structure. In this case, it's an __int64 so your posted code won't have problems until the size of the file exceeds the largest value for a signed int.

    You should also be:
    - delete[]'ing that memory when your done with it!!
    - calling fclose() before return statements
    - checking return values of fseek(), fgetpos()/ftell(), and fread()
    - learning how to use <fstream>

    gg

  6. #6
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    Quote Originally Posted by Codeplug
    There is nothing wrong with "t" as long as it isn't the first mode character seen and you're using the MS C standard lib fopen().
    Hmm, thanks for the info, "t" isn't in the C standard, and I didn't know that MS C lib contains it.

  7. #7
    Registered User
    Join Date
    Nov 2001
    Posts
    162
    Thanks everybody, I got it to load correctly now, I guess the fgetpos( was doing something screwy, but I really have no idea. Anyway, thanks again.

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Is this program is in C++ (the thread is in the C++ forum)? If so, why not use a std::ifstream?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 09:56 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21