Sponsored Content
Top Forums Programming Results Of A Variable Into An Array Using C Language Post 302929352 by Corona688 on Friday 19th of December 2014 04:54:45 PM
Old 12-19-2014
Speaking more generally, C does have a way to slurp an entire file into memory at once: mmap. It's actually better than reading it into memory, a 200MB file won't waste 200MB RAM just sitting there, only the parts you're using will actually be "read". To your program, it's a giant array of bytes corresponding to the entire file.

Since you actually want the file to be organized into lines, etc, this may not be too useful for you.
This User Gave Thanks to Corona688 For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

store awk results in a variable

I try to get the month (of last save) and the filename into a variable, is this possible ? something like this : for month in `ls -la | awk '{print $6}'` do if ] then a=filename of the matching file cp $a /Sep fi thanks, Steffen (1 Reply)
Discussion started by: forever_49ers
1 Replies

2. UNIX for Dummies Questions & Answers

Setting the Results of a Command to a Variable

Hi, Hi, I run the command: hostname to get the host back from the server: db201 Now, I need to take that result and set it to a variable. Can anyone help me with this?? I need to be able to use the same script on multiple servers so I do not want to hardcode the hostname result into... (1 Reply)
Discussion started by: stky13
1 Replies

3. Shell Programming and Scripting

Results of command execution into array

Hi Can anybody tell me how can I dump the results of execution of a command into array form? For example, I want to execute: and put each part of the result in an array element: Thanks (2 Replies)
Discussion started by: alirezan
2 Replies

4. Shell Programming and Scripting

Compare Array results

Hi, In a kshell , i need to compare the results of two array . each Non-match value should copy to a new array. For example: ========== Array one contain the following values: A B C Array two contain the following values: C F A E After comparing this arrays , a new array should... (4 Replies)
Discussion started by: yoavbe
4 Replies

5. UNIX for Dummies Questions & Answers

how to - redirect query results to a variable

How can I send the results of a query to a unix variable. I basically want to run a query then do some logic on the results. Trying to redirect the result into a variable I define in the script. select count(*) as counter from table - nut to redirect the "count" returned from the query... (2 Replies)
Discussion started by: rstone
2 Replies

6. Shell Programming and Scripting

Adding results of a find to an array

I'm trying to add the paths of all the xml files in certain directories to an array. I want to use the array later in my code. Anyway, for some reason this isn't working. Any help would be appreciated. Path_Counter=0 for result in "find * -name '*.xml'"; do XmlPath="$result" echo... (2 Replies)
Discussion started by: Fly_Moe
2 Replies

7. Shell Programming and Scripting

Adding grep'd results in a variable

Here is one I am baffled with; I have not used unix for a while and now that I am back it has been fun remembering and I have enjoyed it, for the most past. this is in ksh. I need to search in a file for the line with X1 and cut columns 20-25, put them into a variable, added them (dollar... (3 Replies)
Discussion started by: CougarMutt
3 Replies

8. Shell Programming and Scripting

Storing the SQL results in array variables

Requirement 1) I need to execute 15 SQL queries in oracle through linux script. All these query results needs to be stored in array variables. Requirement 2) And these 15 queries needs to be executed in parallel. Requirement 3) Once all the queries executed then the shell script should... (3 Replies)
Discussion started by: Niranjancse
3 Replies

9. Shell Programming and Scripting

Append awk results into file or array

for a in {1..100} do awk '{ sum+=$a} END {print sum}' a=$a file1 > file2 done I know I will get only one number if following the code above, how can I get 100 sum numbers in file2? (2 Replies)
Discussion started by: wanliushao
2 Replies

10. Shell Programming and Scripting

I want to add a variable for the results from the formula of one variable and results of another var

Good morning all, This is the file name in question OD_Orders_2019-02-19.csv I am trying to create a bash script to read into files with yesterdays date on the file name while retaining the rest of the files name. I would like for $y to equal, the name of the file with a formula output with... (2 Replies)
Discussion started by: Ibrahim A
2 Replies
Paranoid::Input(3pm)					User Contributed Perl Documentation				      Paranoid::Input(3pm)

NAME
Paranoid::Input - Paranoid input functions VERSION
$Id: Input.pm,v 0.20 2011/04/13 22:01:43 acorliss Exp $ SYNOPSIS
use Paranoid::Input; FSZLIMIT = 64 * 1024; LNSZLIMIT = 2 * 1024; $rv = slurp($filename, @lines); $rv = sip($filename, @lines); $rv = sip($filename, @lines, 1); $rv = tail($filename, @lines); $rv = tail($filename, @lines, -100); $rv = tail($filename, @lines, -100, 1); $rv = closeFile($filename); addTaintRegex("telephone", qr/(d{3})s+d{3}-d{4}/); $rv = detaint($userInput, "login", $val); $rv = stringMatch($input, @strings); DESCRIPTION
The modules provide safer routines to use for input activities such as reading files and detainting user input. The sip and tail functions keep open file handles. Even so, it's specifically built to be safe for use in fork scenarios. You can being a tail or sip in a parent, fork children, and all process can independently continue sipping with no confusion between processes. This is possible because we check to see if the PID matches the PID in effect with the file was opened. If not, we reopen the file and seek to the same position so we can pick up where we left off. The slurp function isn't affected by this since it reads entire files in a single call, no filehandles are kept open between calls. All file-reading functions use and obey flock. addTaintRegex is only exported if this module is used with the :all target. SUBROUTINES
/METHODS FSZLIMIT The value returned/set by this lvalue function is the maximum file size that will be read into memory. This affects functions like slurp (documented below). Unless explicitly set this defaults to 16KB. LNSZLIMIT The valute returned/set by this lvalue function is the maximum line length supported by functions like sip (documented below). Unless explicitly set this defaults to 2KB. slurp $rv = slurp($filename, @lines); This function allows you to read a text file in its entirety into memory, the lines of which are placed into the passed array reference. This function will only read files up to FSZLIMIT in size. Flocking is used (with LOCK_SH) and the read is a blocking read. An optional third argument sets a boolean flag which, if true, determines if all lines are automatically chomped. If chomping is enabled this will strip both UNIX and DOS line separators. The return value is false if the read was unsuccessful or the file's size exceeded FSZLIMIT. In the latter case the array reference will still be populated with what was read. The reason for the failure can be retrieved from Paranoid::ERROR. sip $rv = sip($filename, @lines); $rv = sip($filename, @lines, 1); This function allows you to read a text file into memory in chunks, the lines of which are placed into the passed array reference. The chunks are read in at up to FSZLIMIT in size at a time. Like slurp file locking is used and autochomping is also supported. This function returns true if there was input read, but if any or all of the input splits into lines greater than LNSZLIMIT it will discard that input and return -1 (which is still technically boolean true). The reason why we now care about line lengths is because it's very likely that line boundaries will not fall neatly along our chunk boundaries, so we need to take trailing portions of unterminated lines and store them to be joined with the remainder from the next sip. When sip comes up to then end of the file it does not close the file, you're required to close it explicitly with closeFile. This is done intentionally to allow the process to continue to effectively tail a growing file. Unlike the tail function provided here, though, it does perform any additional checks to see if the file you're reading was truncated or replaced. An optional third argument tells sip whether or not to chomp all the read lines before returning. tail $rv = tail($filename, @lines); $rv = tail($filename, @lines, -100); $rv = tail($filename, @lines, -100, 1); The only difference between this function and sip is that tail opens the file and immediately seeks to the end. If an optional third argument is passed it will seek backwards to extract and return that number of lines (if possible). Depending on the number passed one must be prepared for enough memory to be allocated to store LNSZLIMIT * that number. This function returns true if the file is successfully open, regardless of whether any new input was there to be read. It only returns false if there was a problem opening or reading the file. Tail should be called with the third argument for the first tail of a file. Continuing to use it for subsequent calls will cause the number of lines returned to be truncated to fit within that limit. Like sip, one must explicitly close a file with closeFile. closeFile $rv = closeFile($filename); This function closes any open file descriptors that may have been opened via sip or tail for the named file. This returns the value of the close function if the file was open, otherwise it returns true. addTaintRegex addTaintRegex("telephone", qr/(d{3})s+d{3}-d{4}/); This adds a regular expression which can used by name to detaint user input via the detaint function. This will allow you to overwrite the internally provided regexes or as well as your own. detaint $rv = detaint($userInput, "login", $val); This function populates the passed reference with the detainted input from the first argument. The second argument specifies the type of data in the first argument, and is used to validate the input before detainting. The following data types are currently known: alphabetic ^([a-zA-Z]+)$ alphanumeric ^([a-zA-Z0-9])$ email ^([a-zA-Z][w.-]*@ (?:[a-zA-Z0-9][a-zA-Z0-9-]*.)* [a-zA-Z0-9]+)$ filename ^[/ w-.:,@+]+[?$ fileglob ^[/ w-.:,@+*?{}[]]+[?$ hostname ^(?:[a-zA-Z0-9][a-zA-Z0-9-]*.)* [a-zA-Z0-9]+)$ ipaddr ^(?:d+.){3}d+$ netaddr ^(?:d+.){3}d+(?:/(?:d+| (?:d+.){3}d+))?$ login ^([a-zA-Z][w.-]*)$ nometa ^([^`$!@]+)$ number ^([+-]?[0-9]+(?:.[0-9]+)?)$ If the first argument fails to match against these regular expressions the function will return 0. If the string passed is either undefined or a zero-length string it will also return 0. And finally, if you attempt to use an unknown (or unregistered) data type it will also return 0, and log an error message in Paranoid::ERROR. NOTE: This is a small alteration in previous behavior. In previous versions if an undef or zero-length string was passed, or if the data type was unknown the code would croak. That was, perhaps, a tad overzealous on my part. stringMatch $rv = stringMatch($input, @strings); This function does a multiline case insensitive regex match against the input for every string passed for matching. This does safe quoted matches (Q$stringE) for all the strings, unless the string is a perl Regexp (defined with qr//) or begins and ends with /. NOTE: this performs a study in hopes that for a large number of regexes will be performed faster. This may not always be the case. DEPENDENCIES
o Fcntl o Paranoid o Paranoid::Debug BUGS AND LIMITATIONS
If you fork a process that's already opened a file with sip or tail a new file descriptor will be opened for the child process. But what may be less obvious is that with a newly opened file descriptor you will be starting back from the beginning (or end, in the case of tail) of the file, rather than from where ever you were before the fork. AUTHOR
Arthur Corliss (corliss@digitalmages.com) LICENSE AND COPYRIGHT
This software is licensed under the same terms as Perl, itself. Please see http://dev.perl.org/licenses/ for more information. (c) 2005, Arthur Corliss (corliss@digitalmages.com) perl v5.14.2 2011-04-13 Paranoid::Input(3pm)
All times are GMT -4. The time now is 01:40 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy