I am writing some text which is of std::string stempstr;data type into archive object ar.WriteString(vectorText[i].c_str()); and this i am doing for 10 times by keeping in the for loop where vectorText is a string (i.e. std::vector<std::string>
vectorText) and this i am doing for 10 times by keeping in the for loop. Now the problem is if i am modifying the text in the vector and again trying to save some additional text is also there along with my required text. please let me know what mistake i am doing why that text is coming while i am saving again. i tried ar.flush() also while resaving.
Related
when writing to a text file, using std::ofstream::operator<< I can either write from the beginning or the end of the file and are unable to write from the middle.
I have this text file.
##scores##
player1: 50
player2: 10
player3: 80
now, player2 just gains 40 points. and I want one able to write that in without touching the other players' scores. I could copy the whole text and then modify it inside my application but it feels a bit hacky and slow. I wondered if there are any better ways? kind of like:
std::ofstream::setpos(50);
I've looked at ostream, istream, iostream, and fstream, and I still can't quite figure out how to simply delete lines from an input or output text file just as any clown can do with a text editor.
If I've got a file that reads, for example,
box 1\n
socks\n
box 2\n
pajamas\n
box 3\n
textbooks\n\eof
etc, and I want to totally delete the second box (the 3rd and 4th line) so that the text file reads
box 1\n
socks\n
box 3\n
textbooks\n\eof
I would evidently need to create a temporary file, and place only what I want to keep in that temp file, delete the original file, then rename the temp file with the name of the original file. This is fine and dandy and I understand that it's actually how all programs handle document editing. But it's a tedious pain in the ass to code.
What I'd REALLY love to be able to do is just manipulate what exists without jumping through all these hoops every time I manipulate my text, since I have a huge amount of stuff to sort through and edit.
So this is my hypothesis that I'd like some advice with. Would it be easier to, upon opening the file, to store the entire contents of the file into a string, a vector, a dynamically allocated char array, or perhaps a stringstream, so that I can easily delete parts of it and rearrange it? I could then dump my edited text into a temp text, delete the original, and rename the tempfile with the name of the original file.
Is there any validity to this, or is there a simpler way to do it? I'm tempted to use vectors as my first guess.
[EDIT] Keep in mind that the file I'm dealing with isn't quite so nicely organized to merit the use of structs for easy manipulation of chunks of data. It could be huge paragraphs of prose, or meaningless strings of digits.
If you have many lines and lots of changes, I'm tempted to use a std::list rather than a std::vector.
If you delete or insert often, then the lines must be rearranged. Doing that with a std::vector is more expensive than with a std::list.
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.
I have two very large lists. They both were originally in excel, but the larger one is a list of emails (about 160,000) of them with other information like their name and address etc. And the smaller one is a list of just 18,000 emails.
My question is what would be the easiest way to get rid of all 18,000 rows from the first document that contain the email addresses from the second?
I was thinking regex or maybe there is another application I can use? I have tried searching online but it seems like there isn't much specific to this. I also tried notepad++ but it freezes when I try to compare these large files.
-Thank You in Advance!!
Good question. One way I would tackle this is making a C++ program [you could extrapolate the idea to the language of your choice; You never mentioned which languages you were proficient in] that read each item of the smaller file into a vector of strings. First, of course, use Excel to save the files as CSV instead of XLS or XLSX, which will comma-separate the values so you can work with them easier. For the larger list, "Save As" a copy of just email addresses, deleting the other rows for now.
Then, you could open the larger list and use a nested loop to check if you should output to an output file. Something like:
bool foundMatch=false;
for(int y=0;y<LargeListVector.size();y++) {
for(int x=0;x<SmallListVector.size();x++) {
if(SmallListVector[x]==LargeListVector[y]) foundMatch=true;
}
if(!foundMatch) OutputVector.append(LargeListVector[y]);
foundMatch=false;
}
That might be partially pseudo-code, but do you get the idea?
So I read a forum post at : Here
=MATCH(B1,$A$1:$A$3,0)>0
Column B would be the large list, with the 160,000 inputs and column A was my list of things I needed to delete of 18,000.
I used this to match everything, and in a separate column pasted this formula. It would print out either an error or TRUE. If the data was in both columns it printed out true.
Then because I suck with excel, I threw this text into Notepad++ and searched for all lines that contained TRUE (match case, because in my case some of the data had the word true in it without caps.) I marked those lines, then under search, bookmarks, I removed all lines with bookmarks. Pasted that back into excel and voila.
I would like to thank you guys for helping and pointing me in the right direction :)
So, I have this program that collects a bunch of interesting data. I want to have a library that I can use to sort this data into columns and rows (or similar), save it to a file, and then use some other program (like OpenOffice Spreadsheet, or MATLAB since I own it, or maybe some other spreadsheet/database grapher that I don't know of) to analyse and graph the data however I want. I prefer this library to be open source, but it's not really a requirement.
Ok so my mistake, you wanted a writer. Writing a CSV is simple and apparently reading them into matlab is simple too.
http://www.mathworks.com.au/help/techdoc/ref/csvread.html
A CSV has a simple structure. For each row you seperate by newline. and each column is seperated by a comma.
0,10,15,12
4,7,0,3
So all you really need to do is grab your data, seperate it by rows then write a line out with each column seperated by a comma.
If you need a code example I can edit again but this shouldn't be too difficult.