Solved

File manager routines

Posted on 1997-07-12
7
231 Views
Last Modified: 2013-12-26
I need a simple C program that creates a new file with the save as dialog, then writes a few varyables, closes the file. then opens another from the open file dialog, reads some data, and closes.
0
Comment
Question by:simap
  • 4
  • 2
7 Comments
 
LVL 10

Expert Comment

by:rbr
ID: 1291560
In which language? Several languages for the MAC (C, 4D, ...) have such functions in the library.
0
 
LVL 1

Author Comment

by:simap
ID: 1291561
C. The pascal routines in Inside macintosh where no help.
0
 
LVL 1

Author Comment

by:simap
ID: 1291562
Edited text of question
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:simap
ID: 1291563
Adjusted points to 400
0
 
LVL 1

Accepted Solution

by:
Paul B earned 400 total points
ID: 1291564
Hi,
 Here is an example of how to create, write and read back a data structure. Intead of reading one or two variables at a time you should put them in a structure and read and write the whole structure at one.

Hope this helps
Paul B
pbaxter@asssitivetech.com


typedef struct {
      long      longvalue1;
      long      longvalue2;
      short      shortvalue1;
      short      shortvalue2;
} File_DataStruct, *File_DatastructPtr;

 Str63 gDefaultName = "\pMy Data File";

File_DataStruct      gFiledata;

/* Prototypes */
void Initialize(void);
Boolean SaveData(void);
Boolean LoadData(void);


void main(void)
{

      Initialize();
      if (SaveData())
            LoadData();
}

Boolean SaveData(void)
{
      Point where = {-1, -1};
      Str255 prompt = {"\pSave varables in:"};
      StandardFileReply reply;
      long size;
      short refnum;
      OSErr err = -1;

      gFiledata.longvalue1 = 1;
      gFiledata.longvalue2 = 2;
      gFiledata.shortvalue1 = 3;
      gFiledata.shortvalue2 = 4;

      // put the dialog box
      CustomPutFile(prompt, gDefaultName, &reply, 0, where, nil, nil, nil, nil, nil);
      // user hit ok
      if (reply.sfGood) {
            BlockMoveData(reply.sfFile.name, gDefaultName, sizeof(gDefaultName));
            
            if (!reply.sfReplacing) {
                  // Create New File
                  err = FSpCreate(&reply.sfFile, 'TEST', 'XXXX', reply.sfScript);
                  if (err)
                        return false;
            }
            // Open the File
            err =  FSpOpenDF(&reply.sfFile, fsCurPerm, &refnum);
            if (err)
                  return false;

            // size of structure
            size = sizeof(gFiledata);
            // write the file
            err = FSWrite(refnum, &size, &gFiledata);
            // close the file
            FSClose(refnum);
      }
      return err == noErr;
}

Boolean LoadData(void)
{
      Point where = {-1, -1};
      Str255 prompt = {"\pSelect the data File:"};
      StandardFileReply reply;
      SFTypeList      typeList;
      long size;
      short refnum;
      OSErr err;

      gFiledata.longvalue1 = 0;
      gFiledata.longvalue2 = 0;
      gFiledata.shortvalue1 = 0;
      gFiledata.shortvalue2 = 0;
      // put the dialog box
      typeList[0] = 'XXXX';
      CustomGetFile(nil, 1, typeList,  &reply, 0, where, nil, nil, nil, nil, nil);
      // user hit ok
      if (reply.sfGood) {
            BlockMoveData(reply.sfFile.name, gDefaultName, sizeof(gDefaultName));
            
            if (!reply.sfReplacing) {
                  // Create New File
                  err = FSpCreate(&reply.sfFile, 'TEST', 'XXXX', reply.sfScript);
                  if (err)
                        return false;
            }
            // Open the File
            err =  FSpOpenDF(&reply.sfFile, fsCurPerm, &refnum);
            if (err)
                  return false;

            // size of structure
            size = sizeof(gFiledata);
            // read the file
            err = FSRead(refnum, &size, &gFiledata);
            // close the file
            FSClose(refnum);
      }
      return err == noErr;
}

//
//      Initialize everything for the program, make sure we can run
//

//MW specified argument and return type.
void Initialize(void)
{
      /* Initialize all the needed managers. */
      InitGraf(&qd.thePort);
      InitFonts();
      InitWindows();
      InitMenus();
      TEInit();
      InitDialogs(nil);
      InitCursor();

}
0
 
LVL 1

Expert Comment

by:Paul B
ID: 1291565
OOOOPs,
  In the function LoadData  get rid of:
            if (!reply.sfReplacing) {
                  // Create New File
                  err = FSpCreate(&reply.sfFile, 'TEST', 'XXXX', reply.sfScript);
                  if (err)
                        return false;
            }

I Cut and pasted too much

sorry,
   Paul

0
 
LVL 1

Author Comment

by:simap
ID: 1291566
I just wish you had been around here on sunday :P
Thanks :)
     SIMAP
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
move a line in eclipse 3 107
ShiftLeft challenge 21 86
fix34  challenge 9 143
Way to decrease size of apk file 9 86
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question