Appending the header of a txt document with ColdFusion - coldfusion

Using ColdFusion I would like to append the header of the file each time that I add a row into the txt file
The format for the header is as follows:
E.g.
1234567890123456789012345678901234567890
BEEP BANK 1000009099810
The header's content is broken down into:
Element Description Pad char Pad side Length
Bank name First 16 characters of the Bank name as per file [space] right 16
name, padded with spaces at the end if the name is
less than 16 characters long. Convert to Uppercase
Record count Number of data records in the file, depicted as a 0 left 3
3 character, zero-left-padded string
Total value Value (in cents) of all amounts in the file, 0 left 10
depicted as a 10 character, zero-left-padded
string
Total Header Record Length 29
At the moment I am running a loop and inside of the loop I would like to update the Record Count and the Total Value inside of the header while still keeping the same format
What I would like to know is how do I get the header of the txt file called test.txt and split the numeric value to get the individual values namely Record Count and Total Value to work with them and add my new dynamic values to them?
Thanks in advance

This will read the file and replace the first line, and write it back to the original file.
<cffile action="read" variable="textDoc" file="#getDirectoryFromPath(getTemplatePath())#/test.txt" />
<cfset textDoc = listSetAt(textDoc,1,"new header line","#chr(10)##chr(13)#")>
<cffile action="write" file="#getDirectoryFromPath(getTemplatePath())#/test.txt" output="#textDoc#">
In this instance we are using new-line and carriage-return as the list delimiters.

You will have to parse the file. If you want to keep it human readable (as opposed to making it XML or similar) then you have to tell coldfusion how to find the characters to update.
I assume the header is always static, only the numbers recording the records change.
The best way is to include those records in the header by escape characters, for example {{125}}.
Then grab the file, and do a regexp looking for {{.?}}, update the number and replace. Then write the file.
OR just delete the whole header and rewrite with the update data everytime the file is saved. Whichever is easier in your case.

Related

ofsteam overwriting the selected part of a txt file

I am new in C++98. I am getting some fields from a large text file. I want to update only 4 out of 50 lines in the text file. Here is my code. It is getting the text from a lineEdit of a Qt4 form.
strcpy(Name,ui->lineEdit_1->setText(QString::fromStdString(Name)) );
strcpy(Class,ui->lineEdit_1->setText(QString::fromStdString(Class)));
strcpy(Grade,ui->lineEdit_1->setText(QString::fromStdString(Grade)));
std::fstream myfile;
myfile.open(mypath,std::fstream::in | std::fstream::out );
myfile<<Name<<"\t"<<"Name"<<"\n";
myfile<<Class<<"\t"<<"Class"<<"\n";
myfile<<Grade<<"\t"<<"Grade"<<"\n";
Here is sample.conf.txt:
Hello. Name
One. Class
Two. Classsec
A+. Grade
B+. Gradesec
On updating it by adding random values:
Name AA
Class BB
Grade CC
After executing the above code, it shows this updated sample.conf.txt:
AA Name
BB Class
CC Grade
A+. Grade
B+. Gradesec
It should be like this Model instead:
AA Name
BB. Class
Two. Classsec
CC. Grade
B+. Gradesec
Means it (fstream) is just:
1- overwriting truncate the top 3 lines in the file, leaving the rest of the file intact.
2- it is not selecting the position field name to overwrite its value, according to input content?
How can I overwrite by selecting the specific position by name and write its corresponding value, or write column-wise? How can I accomplish this task? Please help.
You cannot do this. When you update a file, then any text you write will replace exactly the same number of bytes as the size of the text you are writing.
It's not that case that if you write three lines of text, then they will replace the first three lines of text currently in the file (unless those two pieces of text happen to be exactly the same length).
Unless you are doing binary IO with fixed length records then trying to update files is not the way to go. Instead your program should read in the whole file into some data structure, manipulate that data structure as required, and then write out the entire data structure to a file, replacing the whole contents of the file.

How to read a specific line from a text file in c++?

C++ program that displays on the screen item codes with corresponding
item descriptions and prices. It asks the user to enter the code of the item
purchased by a customer. It looks for a match of the item code stored in items.txt.
How can I output only a specific line from a text file after the user inputs the item code?
You need to read the file line-by-line (std::getline), extract (depending on the exact format, e.g. by searching for a whitespace in the string) and compare the code and then return the corresponding line on a match.
It is not possible to access lines from a text file directly by index or content.
This is assuming that you mean the file contains lines in the form
code1 item1
code2 item2
//...
If the code is just the index of the line, then you only need to call std::getline in a loop with a loop counter for the current index of the line.
If you do this multiple times on the same file, you should probably parse the whole content first line-by-line into a std::vector<std::string> or a std::(unordered_)map<std::string, std::string> or something similar to avoid the costly repeated iteration.
Depending on the use case, maybe it would be even better to parse the data into a database first and then query the database, even if it is only e.g. sqlite or something like that.

How to modify only timecodes but not other numbers or letters in a text file?

I'm making a program to speed up and slow down parts of videos, and I want to support modifying times on subtitles to match. How can I search for only the timecodes in a text file and modify them?
This is for srt subtitle files. Timecodes are in the format of HH.MM.SS,mmm. The files contain other numbers (eg in hex colors) so I only want to search for numbers in the specific timecode format.
I already have a function to take an input time in seconds and return an output time in seconds. It should also be fairly easy to convert between 'timecode' format and time in seconds.
This is an example of the text file:
1
00:00:00,000 --> 00:00:09,138
<font color="#CCCCCC">alexxa</font><font color="#E5E5E5"> who's your favorite president</font>
2
00:00:04,759 --> 00:00:12,889
<font color="#E5E5E5">George Washington</font><font color="#CCCCCC"> has my vote Alexa</font>
The only thing left is how to take in only timecodes and then replace them with new timecodes?
Not sure where to go from here. It would also be good to avoid looping through the text file more than necessary because there will be a lot of timecodes to change.
Given it's a text format, the most efficient way to match (and replace) the format of the time-stamps in your file would be to use regular expressions: https://en.cppreference.com/w/cpp/regex
the algo would you like this: you read line by line from your source file, for
every read line where RE matches, you replace it with the new time-stamps (i.e. craft a new line) and output to a new file (or to a buffer, which later could be committed into the source file - after processing is done). Other lines (where RE does not match) you output intact, as they were read.

Skip first line in csv read on Kettle

Hello i am trying to skip the first line of a csv file when i import it to Kettle Pentaho PDI 8.1.0.
The first line has the separator declaration
sep=;
The second line has the Headers. Cause of the first line the get fields button read only two variables. The first is the sep= and the second one that does not set a name.
I tried to set that header lines are 2 ,also to escape sep= also to use the Document header lines set to 1 in order to escape the first line but the get fields button does not recognize the headers.
Is there any other idea?
Get fields will always look at the first line. You will need to enter the field list by hand.
You were on the right track, set headers to 2 and it will read the data correctly.
If you need to parse the separator declaration you will need to parse the file once to determine its structure, then use metadata injection to read a 2nd time for the data.

Reading a line of a text file from a specific position in C++

I would like to read a text file in C++ in following manner:
Ignore the entire first line as it is simply meant as an introduction.
Only read the following lines from a specific position.
That starting position for reading is a fixed one and remains the same for every line; however, the numbers after that may be of variable length. I need to save all of these numbers from line 2 to line n into an Array.
At the moment I can read a regular 2D Array with getline.
How can I work around these things?
An example for a line I want to read could be:
Person1: 25 988.3 0.0023 7
To set the file to a position, use std::ifstream::seekg().
To set the file to the beginning of a line, you must read and count the line endings. Many text files have variable length text lines.
How can I work around these things?
You can't, unless you can ensure that all of the data lines after the first line are all the same length.
If you can't ensure that, then all you can do is read through all of the preceding lines.
An alternative I have employed in the past is to generate an 'index' of line start positions in a secondary file in binary format (so that I CAN jump directly to the right place in that file), and use that to jump to the right place in the text file. Of course that means that you need to regenerate that index file every time you replace/amend the data file.