text-files

What is the most elegant way to write a text/binary file with c++?

纵然是瞬间 提交于 2019-12-05 20:22:31
I found some good results on reading e.g.: Read txt with iterators and preallocation , or read into containers . So I wondered how would I write most elegant a std::string into a file? Edit: When reading I can preallocate space for the string via seek and tellg, since I know the size of the string, how could I tell the filesystem how much I want to write? Here's a tiny example on how to output a std::string , but you should really read up on fstream #include <iostream> #include <fstream> #include <string> int main(int argc, char *argv[]) { std::string s("writemeout"); std::ofstream outfile(

batch file write <> to text file

北战南征 提交于 2019-12-05 19:54:59
This is my first time doing these things. Basically I am creating a batch file html maker for church songs. This is what I am stuck with: echo <br> %var17% >>Runfiles\temporary2.txt Basically var 17 is a line of lyric and I want to add a line break in front of it. I have seperated the template maker into three parts: first half lyrics second half Here is the code: @echo off copy "I:\Build\Files\*.txt" ren *.txt temp.txt setlocal ENABLEDELAYEDEXPANSION set vidx=0 for /F "tokens=*" %%A in (temp.txt) do ( SET /A vidx=!vidx! + 1 set var!vidx!=%%A ) set var echo <br> %var17% >>Runfiles\temporary2

Explode contents of a txt file into array

微笑、不失礼 提交于 2019-12-05 16:20:25
I´m having trouble exploding contents of a .txt file (structure below): 01Name 1 02whatever contents 03whatever contents ------------------- 01Name 2 02whatever contents 03whatever contents As you can see, the "delimiter" is "-------------------". Now, the question is: how to explode this file into an array, so I can search for a specific name and display that block´s contents? I´ve tried to explode like this: header("Content-type:text/plain"); $file = fopen("cc/cc.txt", "r"); while (!feof($file)) { $lot = fgets($file); $chunk = explode("-------------------",$lot); print_r($chunk); } fclose(

How do I match a word in a text file using python?

℡╲_俬逩灬. 提交于 2019-12-05 16:03:14
I want to search and match a particular word in a text file. with open('wordlist.txt', 'r') as searchfile: for line in searchfile: if word in line: print line This code returns even the words that contain substrings of the target word. For example if the word is "there" then the search returns "there", "therefore", "thereby", etc. I want the code to return only the lines which contain "there". Period. split the line into tokens: if word in line.split(): import re file = open('wordlist.txt', 'r') for line in file.readlines(): if re.search('^there$', line, re.I): print line The re.search

Efficient way to add two lines at the beginning of a very large file

陌路散爱 提交于 2019-12-05 15:55:50
I have a group of very large (a couple of GB's each) text files. I need to add two lines at the beginning of each of these files. I tried using sed with the following command sed -i '1iFirstLine' sed -i '2iSecondLine' The problem with sed is that it loops through the entire file, even if had to add only two lines at the beginning and therefore it takes lot of time. Is there an alternate way to do this more efficiently, without reading the entire file? You should try echo "1iFirstLine" > newfile.txt echo "2iSecondLine" >> newfile.txt cat oldfile.txt >> newfile.txt mv newfile.txt oldfile.txt

vim: would like it to turn settings on only for certain file types

余生颓废 提交于 2019-12-05 13:18:05
问题 I've looked at this but it wasn't too much help. Maybe I didn't read it too well. Basically what I want is when I open a .txt file the settings: set wrap set linebreak are turned on. How might I go about doing that? Thanks in advance. Also, I'm using XP. 回答1: My answer to that question still applies: Put autocmd commands based on the file suffix in your ~/.vimrc autocmd BufRead,BufNewFile *.txt set wrap linebreak As Luc says, you might prefer to autocmd BufRead,BufNewFile *.txt setlocal wrap

How To Overwrite A File If It Already Exists?

你。 提交于 2019-12-05 12:38:37
问题 I'm making a music player. It has 2 forms; one is the main area where you play music. The second form has a CheckedListBox where you select the mp3s you want. When I click a button, it saves the selection in a .txt file so I can access them in the first form, where I'll put the strings into the paths for music player to find the files. This is the code in my second form, where I save the selected songs into .txt files. private void selectbtn_Click(object sender, EventArgs e) { if (File.Exists

convert array into .txt file [duplicate]

和自甴很熟 提交于 2019-12-05 12:26:43
This question already has answers here : Closed 9 years ago . Possible Duplicate: Convert array into csv Hi, How to convert array into .txt file? This is my array: Array ( [OrderList_RetrieveByContactResult] => Array ( [OrderDetails] => Array ( [0] => Array ( [entityId] => 156456 [orderId] => 110631 [orderName] => testing2 [statusTypeId] => 15656 [countryCode] => AU [orderType] => 2 [invoiceNumber] => 1001 [invoiceDate] => 2010-10-19T00:00:00 [userID_AssignedTo] => 245454 [shippingAmount] => 8.95 [shippingTaxRate] => 0 [shippingAttention] => tttesst [shippingInstructions] => this a test

How do I increment a value in a textfile using the regular Windows command-line?

戏子无情 提交于 2019-12-05 11:25:15
I'd like to keep a "compile-counter" for one of my projects. I figured a quick and dirty way to do this would be to keep a text file with a plain number in it, and then simply call upon a small script to increment this each time I compile. How would I go about doing this using the regular Windows command line? I don't really feel like installing some extra shell to do this but if you have any other super simple suggestions that would accomplish just this, they're naturally appreciated as well. You can try a plain old batchfile. @echo off for /f " delims==" %%i in (counter.txt) do set /A temp

Pitfalls in my code for detecting text file encoding with Python?

筅森魡賤 提交于 2019-12-05 10:58:51
I know more about bicycle repair, chainsaw use and trench safety than I do Python or text encoding; with that in mind... Python text encoding seems to be a perennial issue (my own question: Searching text files' contents with various encodings with Python? , and others I've read: 1 , 2 . I've taken a crack at writing some code to guess the encoding below. In limited testing this code seems to work for my purposes* without me having to know an excess about the first three bytes of text encoding and the situations where those data aren't informative. *My purposes are: Have a dependency-free