ways to read a text file in golfscript and print out its content - golfscript

'#{File.read("file")}' puts
Does not work. Is it possible to read in the content of a text file in GolfScript?

The #{...} expansion only occurs with double-quoted strings:
"#{File.read('file')}" puts
works fine.
However, there are some catches.
If you want 'file' to be a parameter, you have to delay the expansion.
The result is cached the first time, so if you want to read the same file more than once (e.g. to check for changes) you have to ensure that the expanded value changes. The easiest way I know to do this is to expand "#{File.read('file')#1}", "#{File.read('file')#2}", etc.

Related

ReportEvent: Logged messages run all lines together

I'm noticing that when logging a multi-line message using ReportEvent, it drops all line ends and runs the text together. For example, my MC file may have:
MessageId=
Severity=Informational
SymbolicName=MSG_TEST_MSG
Language=English
Some text
Another line of text.
Last line of text.
.
The message in Event Viewer shows all three lines run together.
If I put \r\n sequences in the text in insertion strings, those line ends do show up correctly in the logged message.
Also, if I use FormatMessageW to generate the text string of the above message, the line ends are correctly included in the text. They seem to be removed only when posting to Event Viewer.
I have not seen ANY reference to the fact that line ends are being dropped anywhere. Any idea? Is this just the way it is?
Thanks.
You have to use %n to force a "hard line break" inside the message.
Source:
%n
Generates a hard line break when it occurs at the end of a line. This
can be used with FormatMessage to ensure that the message fits a
certain width.
"Why does Format­Message say that %0 terminates the message without a trailing newline? Is it secretly adding newlines?" might also be interesting in this context.

index a text file (lines with different size) in c++

I have to extract information from a text file.
In the text file there is a list of strings.
This is an example of a string: AAA101;2015-01-01 00:00:00;0.784
The value after the last ; is a non integer value, which changes from line to line, so every line has different lenght of characters.
I want to map all of these lines into a structured vector as I can access to a specific line anytime I need without scan the whole file again.
I did some research and I found some threads about a command called, which permit me to reach a specific line of a text file but I read it only works if any line has the same characters lenght of the others.
I was thinking about converting all the lines in the file in a proper format in order to be able to map that file as I want but I hope there is a better and quick way
You can try TStringList*. It creates a list of AnsiStrings. Then each AnsiString can be accessed via ->operator [](numberOfTheLine).

Storing the current line being analysed by flex

In my parser generated by flex, I would like to be able to store each line in the file, so that when reporting errors, I can show the user the line that the error occurred on.
I could of course do this using a vector and read in all lines from the file before/after lexing, but this would just add to the time needed to parse a file.
What I thought I could instead do, is to store the line whenever a new-line character is matched, and insert the current line into a vector. So my questions is, is there a variable/macro that flex that stores the current line inside? (Something like yyline perhaps)
Note: I am also using bison
By itself, lex/flex does not do what you ask. As noted, you want this for reporting error messages. (I do something like this in vi like emacs).
With lex/flex, the only way to store the entire line is to record each token from the current line into your own line-buffer. That can be complicated, especially if your lexer has to handle multi-line content (such as comments or strings).
The yytext variable only shows you the most recently parsed token (and yylength, the corresponding length). If your lexer does a simple ECHO, that is a token just like the ones you pay attention to.
Reading the file in advance as noted is one way to simplify the problem. In vi like emacs, the lexers read via a function from the in-memory buffer rather than from an input stream. It bypasses the normal stream-handling logic by redefining the YY_INPUT macro, e.g.,
#define YY_INPUT(buf,result,max_size) result = flt_input(buf,max_size)
Likewise, ECHO is redefined (since the editor reads the results back rather than letting them go to the standard output):
#define ECHO flt_echo(yytext, yyleng)
and it traps errors detected by the lexer with another redefinition:
#define YY_FATAL_ERROR(msg) flt_failed(msg);
However you do this, the yylineno value reported for a given token will be at the end of parsing a given token.
While it is nice to report the entire line in context in an error message, it is also useful to track the line and column number of each token -- various editors can deal with lines like this
filename:line:col:message
If you build up your line-buffer by tracking tokens, it might be relatively simple to track the column on which each token begins as well.

Replace a single value in JSON file with JSONcpp

Is there any way to change a single value in an already existing JSON file with JSONcpp?
I want to preserve the existing formatting (comments, spaces, line breaks) in the file.
Example: find the value with key "test_boolean" in C:/test.json and make its value "false", leaving everything else as it is.
I've tried using Json::StyledStreamWriter, both with the root of the value I want to change, and the value itself. In the first case, the entire file was rewritten, with the new value, but no formatting or comments were preserved. In the second case, only "false" was written in the entire file.
According to my tests/research, it is impossible to modify a single value in a JSON maintaining the current formatting, using JSONcpp.

Incorporating text files in applications?

Is there anyway I can incorporate a pretty large text file (about 700KBs) into the program itself, so I don't have to ship the text files together in the application directory ? This is the first time I'm trying to do something like this, and I have no idea where to start from.
Help is greatly appreciated (:
Depending on the platform that you are on, you will more than likely be able to embed the file in a resource container of some kind.
If you are programming on the Windows platform, then you might want to look into resource files. You can find a basic intro here:
http://msdn.microsoft.com/en-us/library/y3sk7e6b.aspx
With more detailed information here:
http://msdn.microsoft.com/en-us/library/zabda143.aspx
Have a look at the xxd command and its -include option. You will get a buffer and a length variable in a C formatted file.
If you can figure out how to use a resource file, that would be the preferred method.
It wouldn't be hard to turn a text file into a file that can be compiled directly by your compiler. This might only work for small files - your compiler might have a limit on the size of a single string. If so, a tiny syntax change would make it an array of smaller strings that would work just fine.
You need to convert your file by adding a line at the top, enclosing each line within quotes, putting a newline character at the end of each line, escaping any quotes or backslashes in the text, and adding a semicolon at the end. You can write a program to do this, or it can easily be done in most editors.
This is my example document:
"Four score and seven years ago,"
can be found in the file c:\quotes\GettysburgAddress.txt
Convert it to:
static const char Text[] =
"This is my example document:\n"
"\"Four score and seven years ago,\"\n"
"can be found in the file c:\\quotes\\GettysburgAddress.txt\n"
;
This produces a variable Text which contains a single string with the entire contents of your file. It works because consecutive strings with nothing but whitespace between get concatenated into a single string.