read from second line from a binary file C++ - 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

Related

How to use random access files to create a program to read and display a file

I’ve started doing C++ and I’ve come across random access files. I have a good understanding of how they work but how would I use them if I want them to read and display an entire file from beginning to end? For example, I am trying to make a program that opens up a menu and gives you options on what to do with a previous file. The options would be:
1.Read and display the entire file from the beginning to the end of file
Read and display the entire file from the End to the beginning of file in reverse order
I understand that for this you would use
inf.seekg(0, ios::beg);
inf.seekg(0, ios::end);
to move to the beginning or the end of the file. I was wondering how you would use this to read and display a file from beginning to end and from end to beginning in a programs output.

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.

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

Reading a file and replacing the current line

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.

How can I read Notepad++ file in DOS or Fortran?

I received a textfile created with Notepad++ that I'm trying to read with a Fortran 95 program on both a Mac and a PC. The read line is:
read(lun,'(a)',iostat=io1) input
Since I don't know what the line lengths are I defined input to be 512 in length. With non-notepad++ files when the end of line is found the read "stops" and automatically advances to the next line of text. With the notepad++ file, it reads 512 characters, skipping over the carriage returns. When I open the file using the dos editor on the pc I see carriage return symbols (ASCII char 13) but there is no break between lines, they are all appended to one another.
I've tried searching for ichar(13) and ichar(10), backspacing to the beginning of the line and trying to force an advance to the next line; reading in with format '(a,/')', but haven't been able to get anything to work.
What you need is a pipeline type design. The basic routine is one called getline, which gets a line of data up to the carriage return. Inside the initialization, what you do is open the file as a binary file and read a buffer of say 1024 characters in. Whenever getline is called, return the next lot of characters until you get to a CR. If there aren't enough characters, move the unprocessed characters to the front and read in the remaining characters.
It is basically how compilers work - they get a stream of tokens, which, in your case is a string of characters ending with a CR, and then they process the tokens.