Solved

File manager routines

Posted on 1997-07-12
7
237 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

622 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