reading a text file into a string

From: zork (zork_at_nospam.com)
Date: 07/15/04


Date: Fri, 16 Jul 2004 03:27:57 +1000

hi, i would like to read a whole text file into a string. I thought of using
an unbounded_string for this:

----------
c, char: character;
text : unbounded_string;
...
-- read in text file
while not end_of_file ( File ) loop
    Get ( File, c );
    append ( text, c );
end loop;
...
put ( to_string ( text ) ); -- display content of unbounded_string

-- process text
for i in 1 .. length ( text ) loop
  char := element ( text, i );
  ...
end loop;
-----------

... is this the general way of going about it? or is there a more prefered
method of reading in a whole text file (into whatever format) for
processing?

Thanks again!

cheers,
zork



Relevant Pages

  • Re: why I can not write to the file after initialize the MFC in a service program
    ... you don't use char, an obsolete data type ... Why do you need an intermedate buffer to write literal strings anyway? ... For example, if AfxWinInit fails, you copy a 45-character string into a ... So you are going to try to initialize MFC EACH TIME THROUGH THE LOOP? ...
    (microsoft.public.vc.mfc)
  • Re: why I can not write to the file after initialize the MFC in a service program
    ... you don't use char, an obsolete data type ... Why do you need an intermedate buffer to write literal strings anyway? ... For example, if AfxWinInit fails, you copy a 45-character string into a ... So you are going to try to initialize MFC EACH TIME THROUGH THE LOOP? ...
    (microsoft.public.vc.mfc)
  • Re: K&R2, exercise 5.4
    ... int strend(char*, char*); ... Now ps points to the first character of a string which is one character ... * don't want to FAKE the array call ... outer for loop checks for each element of 1st array. ...
    (comp.lang.c)
  • Re: Parsing between a character and sysmbol
    ... I did say was that your statement of avoiding arrays inside loops whenever ... creating new String objects. ... This is what I got inside its main loop (I'm not ... the "-" char into an array of chars. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Optimization question
    ... PROCEDURE ForOptimize*(s: ARRAY OF CHAR); ... Calling this with a 75-character string yielded "31" for both loops. ... the sentinel null character. ... With C's "for," you define how you continue the loop, which can be (and ...
    (comp.lang.oberon)