Solved

text edit and files

Posted on 1998-10-21
5
149 Views
Last Modified: 2013-12-26
How do I read and write strings to and from a number of text edits such as in an address book where you have a first name, last name, and so on to read and write to a file?
0
Comment
Question by:HumanData
  • 2
  • 2
5 Comments
 

Author Comment

by:HumanData
ID: 1291555
Edited text of question
0
 
LVL 10

Expert Comment

by:rbr
ID: 1291556
What is text edit? Can you pls give me more details.
0
 
LVL 3

Expert Comment

by:boonstra
ID: 1291557
The following program does what I think you want:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

int main(void)
{
      FILE *aFile;
      char name[256],lastname[256],firstname[256];

      /* open file for write */
      aFile = fopen("Test file","w");
      if (NULL==aFile) {
            printf(" cannot open file for write\n");
            return 0;
      }
      
      /* write some strings */
      strcpy(name,"LastName1, FirstName1");
      fprintf(aFile,"%s\n",name);
      
      strcpy(name,"LastName2, FirstName2");
      fprintf(aFile,"%s\n",name);
      
      fclose(aFile);
      
      /* open file for write */
      aFile = fopen("Test file","r");
      if (NULL==aFile) {
            printf(" cannot open file for read\n");
            return 0;
      }
      
      fscanf(aFile,"%255s %255s",lastname,firstname);
      printf("Read string: %s %s\n",lastname,firstname);
      
      fscanf(aFile,"%255s %255s",lastname,firstname);
      printf("Read string: %s %s\n",lastname,firstname);
      
      fclose(aFile);

      return 0;
}

If by "text edit" you mean a TEHandle, then you would get the text from (**te).hText instead of using strcpy.  The text at offset N into a TERec is at (*(**te).hText + N), where te is the TEHandle.
0
 

Author Comment

by:HumanData
ID: 1291558
Please provide answers in C.

By text edit I am meaning a field of text (TEtextBox).
0
 
LVL 3

Accepted Solution

by:
boonstra earned 50 total points
ID: 1291559
A TETextBox can be used to display information, but not for input.  For input you need to use the Dialog or Window Managers and an edit text item.  The following code shows how to read from a file and display in a TETextBox.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <Windows.h>
#include <TextEdit.h>
#include <Fonts.h>

#define kTextSize 2048

int main(void)
{
      FILE *aFile;
      char name[256],lastname[256],firstname[256];
      WindowPtr wind;
      Rect windRect,box;
      char *text,*p;
      unsigned long theTime;
      
      InitGraf(&qd);
      InitFonts();
      InitWindows();
      
      /* open file for write */
      aFile = fopen("Test file","r");
      
      text = p = NewPtrClear(kTextSize);
      
      /* read some strings */
      fscanf(aFile,"%255s %255s",lastname,firstname);
      strcpy(p,lastname);      p+=strlen(lastname);
      strcpy(p,firstname);      p+=strlen(firstname);
      *p++ = '\r';
      
      fscanf(aFile,"%255s %255s",lastname,firstname);
      strcpy(p,lastname);      p+=strlen(lastname);
      strcpy(p,firstname);      p+=strlen(firstname);
      *p++ = '\r';
      
      /* create window */
      SetRect(&windRect,60,60,360,160);
      wind = NewWindow(nil, &windRect, "\pDemo", true, 0, (WindowPtr)-1, false, 0);
      SetPort(wind);
      
      box = windRect;
      OffsetRect(&box,-windRect.left,-windRect.top);
      InsetRect(&box,10,10);
      TETextBox(text, p-text, &box, teFlushLeft);
      
      while (!Button()) ;      /* wait for click to dismiss */
      
      fclose(aFile);

      return 0;
}

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ORA-01403: no data found 43 98
WinWaitActive parameters 12 31
Prevent this page from creating additional dialogs. 3 509
Path of Workbook 3 75
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

813 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

19 Experts available now in Live!

Get 1:1 Help Now