Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

File manager routines

Posted on 1997-07-12
7
Medium Priority
?
240 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
In this post we will learn different types of Android Layout and some basics of an Android App.
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 tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

715 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