473,322 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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 9120
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

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

Similar topics

19
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much...
6
by: Dave Reid | last post by:
Hi everyone... I'm pretty much a newbie C++ user, and I've run into a problem. I'm trying to read in a large text file, and then do manipulations on it. I can read it into a large 2-dimensional...
2
by: Claire | last post by:
This works ok in a new empty project. I write an empty string to a file. Looking at the file with a hex editor, there's a single byte of value zero. I expect this, it's utf8 encoding and this is...
111
by: Tonio Cartonio | last post by:
I have to read characters from stdin and save them in a string. The problem is that I don't know how much characters will be read. Francesco -- ------------------------------------- ...
2
by: jimmy | last post by:
Hi, I have an XML string that has been returned using a WebRequest object that i now need extract some data from. Some sample data is shown below. <?xml version="1.0" encoding="UTF-8"?>...
7
by: random guy | last post by:
Hi, I'm writing a program which creates an index of text files. For each file it processes, the program records the start and end positions (as returned by tellg()) of sections of interest,...
1
by: Mannie | last post by:
Hi! In my C++ application I use a COM Object (written in C#). When I try to call one of the methods (that tries to read settings from configuration file) I get a nasty error -0x7c812a5b...
3
by: miss time | last post by:
Hi all, my java friends ^-^ I have next week quiz in reading file text ,and understand the topic very well. can any one give some question related to this topic .this help me more to...
1
Coldfire
by: Coldfire | last post by:
Hi, The strange problem i am having is, the input element of type='file' not reading file names after 20 file elements. It simple returns null on reading the 'name' of file. The code is...
1
by: bjoarn | last post by:
I have an Application C# handling file reading, building index on this file, using dll wrapped with SWIG. The dll is originaly programmed in C++. Dll reports back to the the C# programm throug...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.