Reading a file and replacing the current line - c++

I am trying to read a file using ifstream. And while reading (line-by-line), it encrypts each line and has to replace the original line with the new encrypted line. I have so far been able to read from the file but replacing the current line ( current line = line last read ) is what I can't figure out how to do. My question it: How do I replace a line in a file which I last read, using getline(...)?

The problem is you can't delete a specific data in a file; you have to write it from the beginning.
But with f_seek and r+ you may insert some data whatever you want.

Related

read from second line from a binary file C++

I have a file containing A heading followed by the encrypted data. I need to ignore the 1st line (heading ) and read the rest of the file and then decrypt. I am doing this in C++. How do i go about it. I have tried getLine, but doesnt seem to work

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.

Read entire file into a Stata macro variable?

I would like to open a file "my_query.sql" and read the entire text of that file into some macro variable x.
Clearly, I should start with something like:
file open myfile using my_query.sql
But my problem is that file read myfile x isn't quite right as that just reads the first line...
My initial ideas:
Perhaps there is a way to open it in binary and read the whole thing in with a single command?
Or do I have to do some hacked up, read the file line by line and concatenate the strings together?
My preferred solution is the "hacked up, read the file line by line and concatenate" solution.
I can also understand why the solution may seem hacked up, especially for somebody coming from a programming language. For example, this approach might even seem silly next to something like a BufferedReader in Java, but I digress...
You only get the first line of the file when you execute file read myfile x because, according to the documentation at help file:
"The file is positioned at the top (tof), so the first file read reads at the beginning of the file."
This is actually a convenience if you are writing to a file with file write because you won't have to embed newline characters in the string you wish to write - each call to file write will write a new line.
Now, there is a very simple loop construct that allows us to read line by line and store the contents into the macro.
So, if I had a .sql file at /path/to/my/file/ titled SqlScript.sql with the following contents:
SELECT *
FROM MyTable
WHERE Condition
Then the solution becomes something along the lines of:
clear *
file open myfile using "/path/to/my/file/SqlScript.sql", read
file read myfile line
local x "`line'"
while r(eof) == 0 {
file read myfile line
local x "`x'" " " "`line'"
}
file close myfile
di "`x'"
and the result:
SELECT * FROM MyTable WHERE Condition
Here, I used r(eof) to condition my while loop. This is an end of file marker which evaluates to 1 when file read reaches the end of the file.
Here's something that may help you open the file in binary and read it into a local macro.
The good news is, this appears to read the entire text file into the macro in one read.
clear *
file open myfile using "SqlScript.sql", read binary
file read myfile %100s line
local x "`line'"
file close myfile
di "`line'"
The bad news it, it (as written) reads 100 characters - it doesn't know where to stop. I think that if you know what signifies end-of-text-file on your operating system, you could search for that character and substring everything up to it. But dealing this this is beyond me at the moment. And you'll want to replace the newlines with spaces.
If this can be made to work for you I'd like to see the solution.

read file txt by column in C++

I have file.txt with this structure (in picture):
and I want to read this file and i'dont know if there is a methode to read file by column because when i get for exemple 25 from my file i need to know that it is the VEHICLE NUMBER.
I think use to lines to do this!! any idea please?
There is no function in C++ to read a file by column. You have to write your own function to do that.
A text file is stored on disk line-by-line, so you have to read the whole file line-by-line and on each line parse out the column data you are interested in.

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).