469,290 Members | 1,913 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,290 developers. It's quick & easy.

reading file into string

36
Hi ,
i have file of 32kb , i want to read the whole file into string ,
i tried this by doing the below code, but i dint got the whole content of the file in the string , i guess the variable is not able to hold the all data, is there any way where i can achive the same?

Expand|Select|Wrap|Line Numbers
  1. open(FH, "$file");
  2.     my @output = <FH>;
  3.     close FH;
  4. foreach $logmsg (@output)
  5.     {
  6.         $sdpString = "$sdpString" ."$logmsg";
  7.  
  8.     }
  9.  
Aug 20 '08 #1
3 8915
KevinADC
4,059 Expert 2GB
Expand|Select|Wrap|Line Numbers
  1. open(FH, $file) or die "$!";
  2. my $sdpString = do{local $/; <FH>;};
  3. close FH;
  4. print $sdpString;
  5.  
Aug 20 '08 #2
I typically do like so:
Expand|Select|Wrap|Line Numbers
  1.       open(FH, $file) or die "$!";
  2.       my $sdpString = join('', <FH>);
  3.       close FH;
  4.       print $sdpString;
  5.  
I do want to say, though, that if it's in any way feasible for you to do whatever operation you're doing on one line of the file at a time, you ought to try doing it that way. The structure of Perl encourages you to do things one-line-at-a-time because it's a good idea.
Aug 20 '08 #3
KevinADC
4,059 Expert 2GB
I typically do like so:
Expand|Select|Wrap|Line Numbers
  1.       open(FH, $file) or die "$!";
  2.       my $sdpString = join('', <FH>);
  3.       close FH;
  4.       print $sdpString;
  5.  
I do want to say, though, that if it's in any way feasible for you to do whatever operation you're doing on one line of the file at a time, you ought to try doing it that way. The structure of Perl encourages you to do things one-line-at-a-time because it's a good idea.
Do it the way I show above, much more effcient than using join(), but if its a small file it won't matter much.
Aug 20 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

19 posts views Thread by Lionel B | last post: by
6 posts views Thread by Dave Reid | last post: by
111 posts views Thread by Tonio Cartonio | last post: by
2 posts views Thread by jimmy | last post: by
3 posts views Thread by miss time | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.