How to write on a specific line in a file? - c++

I want to write some data on an already existing file. It is a file that contains about 8-10 lines of header(# comments) and then thousands of lines of data values. What i want is to keep the header same but add the updated data values to the file. It is quite possible that after the update I have less number of lines of data values.
So basically i want to erase everything after the last # comment in the header and then start writing the new values from there onwards. Is that possible?
Here is an example:
Original File
#Program
#Date
#Hello
0 23 23 54
1 12 4 2
2 253 786 9887
3 3 23 54
4 1 4 4
5 23 6 81
Updated File
#Program
#Date
#Hello
0 2 23 54
2 253 786 9887
5 23 6 81
The code i am editing is using fopen to read the file and fprintf to write to it. I would prefer if the answers are along these lines so that i don't have to change those two.

The simplest way I came up with is open the Original File, read and copy the header in to memory such as a string header. Then overwrite the whole file by writing the header, then the new data

Write a function that reads the headers from the file and store them into a class/variable/struct.
Write a function that writes the headers to the file
Write a function that writes the desired values to the file
Execute all three functions in that order. The fact that it is the same file that you overwrite is irrelevant, just be sure to close it before writing back to it

Related

Formatting in file using Fortran

do i=1,10
write(21,19) (dai(i,j),j=1,10)
end do
19 format(10f12.10)
This is part of my code where I input in file, here 21 is unit of my file. I want to print 10x10 matrix in file such that only 10 digits are there after decimal. The output of this is formatted matrix but with elements having no space between them. Also if I remove format line from code the file output is usual 10x10 matrix(unformatted). What is wrong here?
If you want spaces in the formatted output, you have to include them in the format. Either explicitly using the appropriate descriptors or by increasing the fields for your numbers.
E.g.,
19 format(10(f12.10,1x))
or
19 format(10f25.10)

How do I store numbers in two arrays with specific order?

I want to open a text file and read it in its entirety while storing its contents into arrays using c++. I have my example text file below. I would like to store the first number into an array and the rest of them into a 2nd array line by line. For example 9 to be stored in first array and 22 22 at second array,then 1 to be stored at first array 2 3 4 at second array etc...I am not sure how to accomplish this in c++, any help is greatly appreciated!
9 22 22
1 2 3 4
1 5
2 3 6 9
For example when I print the first array i want to show: 9 1 1 2 (first column)
and when I print the second array I would like to show: 22 22 2 3 4 etc...
Here's one approach:
Create a loop goes on for as long as you can successfully read a line from the file. You'll need a std::string and std::getline for that.
In the loop:
Put the line you read in an std::istringstream to simplify the extraction.
Declare a temporary variable to use for extracting the numbers.
Try to extract a number from the istringstream.
If you successfully extracted a number:
Put the extracted number in arr1.
Create another loop where you extract all the rest of the numbers one by one from the istringstream and put them in arr2.

How to update in file [duplicate]

This question already has answers here:
Replace a line in text file
(3 answers)
Closed 3 years ago.
I have a file in which i have some details:
11 apple 13
15 banana 14
16 grapes 19
Now i will search for 14 and i got to know that it's in 2nd line. So here i have two options. I can do
11 apple 15
15 banana 50 //Modify that value
16 grapes 19
Or
11 apple 15
16 grapes 19
delete that line for in file.
I can do 2nd easily by creating a new file and copying the content of the original file except for that line.
But i found that unproductive. If i have 1 million such lines and deletion is frequent operation i can't do this every time.
Any idea of how to do 1st operation (replacing that particular value) and better way to do second one?
Load (read) whole file into memory.
Peform all replacements and deletions in memory using for example memove.
Save (write) final memory buffer to a file.

How to read a certain line from a file and read from that point below, in c++?

I have a txt file, with some numbers. Let's say I have these lines.
124
559
774
12
145
698
So there are 6 lines, let's say, I would like to read the file from 12 and below, is there a function in c++ that returns the cursor of the file? I don't know prior what are the values, I was just trying to explain what I would like to do.
So for example if I would like to read the values 12, 145, 698, is it possible, and ignoring the prior values, not deleting.
You can use getline : http://www.cplusplus.com/reference/string/string/getline/
And call it in a loop to skip the first N lines.

What is a good way to test a file to see if its a zip file?

I am looking as a new file format specification and the specification says the file can be either xml based or a zip file containing an xml file and other files.
The file extension is the same in both cases. What ways could I test the file to decide if it needs decompressing or just reading?
The zip file format is defined by PKWARE. You can find their file specification here.
Near the top you will find the header specification:
A. Local file header:
local file header signature 4 bytes (0x04034b50)
version needed to extract 2 bytes
general purpose bit flag 2 bytes
compression method 2 bytes
last mod file time 2 bytes
last mod file date 2 bytes
crc-32 4 bytes
compressed size 4 bytes
uncompressed size 4 bytes
file name length 2 bytes
extra field length 2 bytes
file name (variable size)
extra field (variable size)
From this you can see that the first 4 bytes of the header should be the file signature which should be the hex value 0x04034b50. Byte order in the file is the other way round - PKWARE specify that "All values are stored in little-endian byte order unless otherwise specified.", so if you use a hex editor to view the file you will see 50 4b 03 04 as the first 4 bytes.
You can use this to check if your file is a zip file. If you open the file in notepad, you will notice that the first two bytes (50 and 4b) are the ASCII characters PK.
You could look at the magic number of the file. The ones for ZIP archives are listed on the ZIP format wikipedia page: PK\003\004 or PK\005\006.
Check the first few bytes of the file for the magic number. Zip files begin with PK (50 4B). As XML files cannot start with these characters and still be valid, you can be fairly sure as to the file type.
You can use file to see if it's a text file(xml) or an executable(zip).
Scroll down to see an example.
Not a good solution though, but just thinking out load... how about:
try
{
LoadXmlFile(theFile);//Exception if not an xml file
}
catch(Exception ex)
{
LoadZipFile(theFile)
}
You could check the file to see if it contains a valid XML header. If it doesn't, try decompressing it.
See Click here for XML specification.
File magic numbers
To clarify, it starts with 50 4b 03 04.
See http://www.pkware.com/documents/casestudies/APPNOTE.TXT (From Simon P Stevens)
You could try unzipping it - an XML file is exceedingly unlikely to be a valid zip file, or could check the magic numbers, as others have said.
it depends on what you are using but the zip library might have a function that test wether a file or not is a zip file
something like is_zip, test_file_zip or whatever ...
or create you're own function by using the magic number given above.