I am using MFC to write a measurement application. On the first run, I got my data written on first column and on to next row and next row.
Here's the question. On the second run, how do I write my data on the second column?
CFile DataFile(m_strPathName, CFile::modeWrite | CFile::modeCreate);
sprintf_s(File,"%d,%f,%e\r\n",i , position, buffer1);
GetLength = strlen(File);
DataFile.Write(File, GetLength);
buffer1 is the power value extracted from the measurement hardware.
Actually, I think ,you should design a format for the file. when you write, you should use a offset to determine where to write.For example ,the column length is a particular value and the same to the row value ,like this:
---column1----|----column2----|---column3----|...
---row1-------|----row2-------|----row3------|..
....
when your write a column or a row, just locate the "|" position,then write your value .
You mean write data by column, which is just next to the first column? That could not be done sequentially. Since file is a stream structure, we can't insert data to the middle of a file, too.
An alternative way is this:
Create a new file with write and append permisson.
Read one row sequentially from the original file, write it to the new file.
Write one row of the second column to the new file.
Repeat the step 2 and 3 util the original file reach the end.
Swap the file name of the original file and the new file.
Related
I'm am trying to write a script that creates an output file with all my data, but my data has different lengths. So I was thinking of writing one file, then adding a new column to it with the other data. I am open to any other suggestions.
So, for example, I would write one file with 3 columns with all my coordinates, then later add a fourth column including temperature or something. the coordinates would have a longer line length since they are measured more frequently.
this is what I've tried before
24 format(a4, 1x, 2(ES12.4, 1x),i4, 1x, f8.3,1x,ES12.4,1x, 3(i4,1x))
25 format(20x, f8.3,1x,ES12.4,1x, 3(i4,1x))
do while (.true.)
read(unit=802,fmt=2,end=122)coll2,t2,ered,tred,hb_alpha,hb_ii,hb_ij,ehh_ii,ehh_ij,rg_avg,e2e_avg
write(8,25) ttotal+t, hb_alpha,hb_ii-hb_alpha,hb_ij, colltotal+coll
end do
write(8,24) fname_digits, ttotal+t, colltotal+coll, betahb
everything is within another do-loop to read from one file to the next. the variables in the do-loop have a longer length than the variables in the second write statement.
I would expect all the data in one file, with varying line lengths.
I have been trying to figure out what the best way to do this would be, but haven't quite found an answer yet. I have a float array full of data collected from an inertial sensor and I would like to put it into the right format and output it to a CSV file. I'm using an mbed microcontroller with a local file system to store the file. It's the part about getting the format right that is confusing me at the minute.
I'd like my gyroscope/accelerometer values to be displayed in rows such as:
gx1, gy1, gz1, ax1, ay1, az1
gx2, gy2, gz2, ax2, ay2, az2
gx3, gy3, gz3, ax3, ay3, az3
I think these values first need to be converted to char before being written to the file, so I will need to do that and store them in a new array of type char. That's where I get confused, because I don't just want to copy the data into this new array all at once (thinking of using a for loop and spritf()) but I also want it to be formatted as displayed above, with the right breaks between rows.
The function that writes the content of the array to the file takes the array, its types size, the array size and the file object.
fwrite(converted_array, sizeof(char), sizeof(converted_array), FileObject);
What would be the best way to make sure that the array content is formatted like I want it to be?
Is there a way, where I can open a file that contains a large amount of data, and retrieve only one specific row or index, without getting the rest of the content as well?
Update:
Based on what others have mentioned here in the comments, I have some follow-up questions.
Can anyone give me an example of how to put a fixed width on the rows/linebreaks(whatever you want to call it), or show me a good source where I can read more about it?
So if I set this up correctly, I will be able to get a specific line from the file superfast, even if it contains several million rows?
If you want to access a file by records or rows, and the rows are not fixed length, you'll have to create a structure that you can associate (or map) file positions to row indices.
I recommend using std::vector<std::streampos>.
Read through the file.
When the file is at the beginning of a row, read the file position and append to the vector.
If you need to access a row in the file:
1) Use the vector to get the file position of the row.
2) Seek to the row using the file position.
This technique will work with fixed length and variable length rows.
I have a .CSV file that's storing data from a laser. It records the height of the laser beam every second.
The .CSV file ends up having rows for each measurement that are all in this format:
DR,04,#
where the # is the height reading.
For example, if the beam is at a height of 10, the reading would say:
DR,04,10.
I want my program in C++ to read only the height (third column of the .CSV) from each row and put it into an array. I do not want the first two columns at all. That way I end up with an array with just a bunch of height values from each measurement.
How do I do that?
You can use strtok() to separate out the three columns. And then just get the last value.
You could also just take the string and scan for the first comma, and then scan from there for the second comma. What follows is the value you are after.
You could also use sscanf() to parse out the individual values.
This really isn't a difficult problem, and there are many ways to approach it. That is why people are complaining that you probably should've tried something and then ask a question here when you get stuck on a specific question.
I'm writing a simple calendar application which saves the data in a text file. I use the iCalendar-format, so my text file ends "END:VCALENDAR".
When the user adds a new event, the application should write the associated data at the end of the text file without overwriting "END:VCALENDAR", how can I do this? What about deleting an event which is saved in the middle of the text file? Is there a need to write the whole file again using the updated data? Many thanks.
You can't dynamically "expand" the file by writing in the middle of it.
You'll need to, either:
Deserialize the whole calendar to memory, then write it back (best option)
Read into memory everything which lies past the point you want to insert the data, write you data, then write the stored file "tail"
There isn't any way of inserting into the middle of a file; the underlying OS doesn't support it. The usual technique is to copy the file into a temporary file, making whatever modifications you need to along the line, then (and only if there are no errors on the output of the copy—do verify that the output stream has not failed after the close) delete the input file and rename/move the temporary file to the original name.
There is no method supported by the C++ libraries that, unlike append, gives an option to insert at any specific position into a file; be it a text or a binary file.
There are two options for you then:
First is the one you are presuming, that is, read the whole file, update the data and write it back again.
Second is to seek in the file to the last line's first character E as in END:VCALENDAR, write your event and then append "END:VCALENDAR" to it.
And yes, you can find that first character of last line, E right after the last newline character, programmatically.
Sorry, there isn't really any other way around, as far as I know.