Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

File manager routines

Posted on 1997-07-12
7
Medium Priority
?
245 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

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

Accepted Solution

by:
Paul B earned 1600 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Loops Section Overview
Suggested Courses
Course of the Month11 days, 17 hours left to enroll

916 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