Thread: reading and writing problem

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    54

    reading and writing problem

    I'm having a problem with my program i'm writing that splices raw Starcraft map files.
    chk.c (main file, the one thats not working right)
    Code:
    #include <stdio.h>
    #include "ConsoleMenu.c"
    typedef struct chkSection {
      char name[5];
      long length;
      void* data;
    } chkSection;
    int main (int argc, char** argv) {
      //Declarations
      FILE* chkFile;
      FILE* outputFile;
      long numSections = 0;
      long sectionLength, fileSize;
      short currSection, selectedSection;
      char** sectionMenu;
      char outputFileName[MAX_PATH];
      chkSection* mapSections;
      //Begin program
      system("cls"); //FIX THIS
      chkFile = fopen(argv[1],"rb");
      if (!chkFile) {
        printf("OmgErrored!!");
        exit(1);
      }
      fileSize = filesize(chkFile);
      while (ftell(chkFile) < fileSize) {
        fseek(chkFile,4,SEEK_CUR);
        fread(&sectionLength,4,1,chkFile);
        fseek(chkFile,sectionLength,SEEK_CUR);
        numSections++;
      }
      rewind(chkFile);
      sectionMenu = (char**)malloc(numSections*sizeof(void*));
      mapSections = (chkSection*)malloc(numSections*sizeof(chkSection));
      printf("%d sections: debug %d\n",numSections,sizeof(chkSection));
      for (currSection=0;currSection<numSections;currSection++) {
        fread(&(mapSections[currSection].name),4,1,chkFile);
        mapSections[currSection].name[4] = '\x00';
        fread(&(mapSections[currSection].length),4,1,chkFile);
        mapSections[currSection].data = malloc(mapSections[currSection].length);
        fread(mapSections[currSection].data,mapSections[currSection].length,1,chkFile);
        sectionMenu[currSection] = mapSections[currSection].name;
      }
      selectedSection = runLongMenu(1,2,6,20,numSections,sectionMenu);
      system("cls");
      printf("Save to? ");
      outputFile = fopen(gets(outputFileName),"wb");
      if (!outputFile) {
        printf("OmgErrored!!");
        exit(1);
      }
      if (fwrite((char*)mapSections[currSection].data,mapSections[currSection].length,1,outputFile)) {
        printf("sucess!"); // ^ ^ ^ ^ problem here
      } else {printf("Errored error: %d",GetLastError());}
      getchar();
      fclose(outputFile);
      fclose(chkFile);
    }
    int filesize(FILE* pFile) {
      int filesize;
      int origpos;
      origpos = fseek(pFile,0,SEEK_END);
      filesize = ftell (pFile);
      fseek(pFile,origpos,SEEK_SET);
      return filesize;
    }
    ConsoleMenu.c (general purpose, thats why not all the functions are used)
    Code:
    #include <stdio.h>
    #include <windows.h>
    #define false 0
    #define true 1
    #define bool char
    typedef struct MenuItem {
      char* name;
      char x;
      char y;
    } MenuItem;
    //these functions by Pie_Sniper
    void setColor( int color ) {
      HANDLE hOut = GetStdHandle( STD_OUTPUT_HANDLE );
      SetConsoleTextAttribute( hOut, color );
    }
    void setCursorPos( int x, int y ) {
      HANDLE hOut = GetStdHandle( STD_OUTPUT_HANDLE );
      COORD coord;
      coord.X = x;
      coord.Y = y;
      SetConsoleCursorPosition( hOut, coord );
    }
    bool keyDown( char *Key ) {
       INPUT_RECORD Event;
       DWORD NumberOfEvents, EventsRead, EventCounter;
       HANDLE hIn = GetStdHandle( STD_INPUT_HANDLE );
       GetNumberOfConsoleInputEvents( hIn, &NumberOfEvents );
       if( NumberOfEvents == 0 ) {
         return false;
       }
       for( EventCounter = 0; EventCounter < NumberOfEvents; EventCounter++ )
       {
         PeekConsoleInput( hIn, &Event, 1, &EventsRead );
         if( Event.EventType == KEY_EVENT && Event.Event.KeyEvent.bKeyDown )
         {
           ReadConsoleInput( hIn, &Event, 1, &EventsRead );
           *Key = Event.Event.KeyEvent.wVirtualKeyCode;
           if( !FlushConsoleInputBuffer( hIn ) )
           {
             exit( 0 );
           }
           return true;
         }
         else {
           ReadConsoleInput( hIn, &Event, 1, &EventsRead );
         }
       }
       return false;
    }
    //End functions by Pie_sniper
    bool drawMenu(int numItems,MenuItem* menuItems,int selectedItem) {
      int currMenuNum;
      for (currMenuNum=0;currMenuNum<numItems;currMenuNum++) {
        setCursorPos(menuItems[currMenuNum].x,menuItems[currMenuNum].y);
        if (currMenuNum == selectedItem) {
          setColor(0x87);
        } else {setColor(0x07);}
        printf(menuItems[currMenuNum].name);
      }
    }
    int runMenu(int numItems, MenuItem menuItems[]) {
      char key;
      int selectedItem = 0;
      drawMenu(numItems,menuItems,selectedItem);
      while (1) {
        if (keyDown(&key)) {
          if (key == 38 && selectedItem != 0) {
            selectedItem--;
          } else if (key == 40 && selectedItem != numItems-1) {
            selectedItem++;
          } else if (key == 13) {
            setColor(0x07);
            return selectedItem;
          }
          drawMenu(numItems, menuItems,selectedItem);
        }
      }
    }
    void drawBox(char x,char y,char w,char h) {
      int i;
      setCursorPos(x,y);
      printf("\xC9");
      for (i=0;i<w;i++) {
        printf("\xCD");
      }
      printf("\xBB");
      for (i=1;i<=h;i++) {
        setCursorPos(x,y+i);
        printf("\xBA");
        setCursorPos(x+w+1,y+i);
        printf("\xBA");
      }
      setCursorPos(x,y+h+1);
      printf("\xC8");
      for (i=0;i<w;i++) {
        printf("\xCD");
      }
      printf("\xBC");
    }
    void drawLongMenu (char x, char y, char width, char height, int numItems,
                       char* menuItems[], int selectedItem, int topDispMenuItem) {
      // Add some clearing stuff here, and redraw box?
      int currMenuNum;
      for (currMenuNum=topDispMenuItem;currMenuNum<(topDispMenuItem+height);currMenuNum++) {
        setCursorPos(x,y+currMenuNum - topDispMenuItem);
        if (currMenuNum == selectedItem) {
          setColor(0x87);
        } else {setColor(0x07);}
        printf(menuItems[currMenuNum]);
      }
    }
    int runLongMenu(char x, char y, char width, char height, int numItems, char* menuItems[]) {
      char key;
      int selectedItem = 0;
      int topDispMenuItem = 0;
      drawLongMenu(x,y,width,height,numItems,menuItems,selectedItem,topDispMenuItem);
      drawBox(x-1,y-1,width,height);
      while (1) {
    
        if (keyDown(&key)) {
          if (key == 38 && selectedItem != 0) {
            if (topDispMenuItem == selectedItem) {
              topDispMenuItem--;
            }
            selectedItem--;
          } else if (key == 40 && selectedItem != numItems-1) {
            selectedItem++;
            if (topDispMenuItem+height == selectedItem) {
              topDispMenuItem++;
            }
          } else if (key == 13) {
            setColor(0x07);
            return selectedItem;
          }
          drawLongMenu(x,y,width,height,numItems,menuItems,selectedItem,topDispMenuItem);
        }
      }
    }
    The problem is, when it tries to parse the file it's supposed to parse (save target as), fwrite gives me error 1784 (invalid user buffer). I dont know why.
    Last edited by Doodle77; 10-16-2006 at 04:27 PM.

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
      for (currSection=0;currSection<numSections;currSection++) {
        fread(&(mapSections[currSection].name),4,1,chkFile);
        mapSections[currSection].name[4] = '\x00';
        fread(&(mapSections[currSection].length),4,1,chkFile);
        mapSections[currSection].data = malloc(mapSections[currSection].length);
        fread(mapSections[currSection].data,mapSections[currSection].length,1,chkFile);
        sectionMenu[currSection] = mapSections[currSection].name;
      }
      selectedSection = runLongMenu(1,2,6,20,numSections,sectionMenu);
      system("cls");
      printf("Save to? ");
      outputFile = fopen(gets(outputFileName),"wb");
      if (!outputFile) {
        printf("OmgErrored!!");
        exit(1);
      }
      if (fwrite((char*)mapSections[currSection].data,mapSections[currSection].length,1,outputFile)) {
        printf("sucess!"); // ^ ^ ^ ^ problem here
      } else {printf("Errored error: %d",GetLastError());}
    It would appear as currSection is not reset and would go past the bounds of your mapSections array.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    54
    Ha! i feel dumb! i was supposed to be using selectedSection :P thx, it works now
    [/editmonkey]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-05-2009, 03:14 AM
  2. problem with reading and writing
    By yahn in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2006, 04:38 PM
  3. Replies: 6
    Last Post: 05-12-2005, 03:39 AM
  4. file writing and reading
    By Micko in forum C Programming
    Replies: 8
    Last Post: 01-13-2004, 11:18 AM
  5. reading file problem
    By samsam1 in forum Windows Programming
    Replies: 4
    Last Post: 01-15-2003, 06:03 PM