Solved

File manager routines

Posted on 1997-07-12
7
226 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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 Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Login Script to Copy Folders 12 21
Strange date stored 22 100
java ^ examples 8 57
fix34  challenge 9 97
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…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now