file-io

How do I set file permissions when opening a file in C++?

ε祈祈猫儿з 提交于 2020-01-15 07:27:26
问题 In C++, I want to open a file and set its permission, but I failed. Below is my program: string filename="test.cnf"; ofstream ofile; ofile.open(filename.c_str(),O_RDONLY); ofile.close() But I get the following error: error: invalid conversion from 'int' to 'std::_Ios_Openmode' error: initializing argument 2 of 'void std::basic_ofstream<_CharT, _Traits>::open(const char*, std::_Ios_Openmode) [with _CharT = char, _Traits = std::char_traits<char>]' How to set the permission of the file to such

writing unicode to binary file in python

非 Y 不嫁゛ 提交于 2020-01-15 07:25:07
问题 I'm wondering how to write unicode (utf-8) to a binary file. Here's the background: I've got a 40 byte header (10 ints), and a table with a variable number of triple-int structs. Writing these was cake. Now, I want to add a bunch of strings to the end of the file. Writing regular ASCII based strings is easy: value = ('ab') s = struct.Struct('2s') packed_data = s.pack(value) I learned how to do this from the Interpret strings as packed binary data. But is there a way to do this for unicode

Are open(file, “wt” or “rt”) different objects?

耗尽温柔 提交于 2020-01-15 05:29:07
问题 When you do: file = open("my file","wt") and file = open("my file" , "rt") These both create file objects that we use file methods on. But are they creating different file objects? And if they are creating different file objects would it be fair to say that the "wt" one is mutable, while the "rt" one is immutable? 回答1: No, that would not be fair to say. You are creating instances of the same standard file type, which proxies file manipulation calls to the operating system. The mode defines

Creating file and intermediate directories if does not exist

依然范特西╮ 提交于 2020-01-15 04:45:08
问题 I want to create a file if and only if that file does not exist. As an example file location is referring to "C:\user\Desktop\dir1\dir2\filename.txt" if (!file.exists()) { try { file.createNewFile(); } catch(IOException ioe) { ioe.printStackTrace(); return; } } Unfortunately the above code is failing, as the dir1 and dir2 does not exist. For my case Sometime the dir1 and dir2 may exist and sometime they may not exist. I do not want to overwrite the contents of these intermediate directories

What is the fastest and safest way to append records to disk file in highly loaded .ashx http handler?

僤鯓⒐⒋嵵緔 提交于 2020-01-14 19:34:27
问题 What is the best option for writing (appending) records to file in highly parallel web environment in .net4 IIS7? I use ashx http handler to receive small portions of data that should be written to file quickly. First I used: using (var stream = new FileStream(fileName, FileMode.Append, FileAccess.Write, FileShare.ReadWrite, 8192)) { stream.Write(buffer, 0, buffer.Length); } But I noticed that some records were broken or incomplete, probably because of FileShare.ReadWrite. Next I tried to

.NET Write a file with File Version Information attached

百般思念 提交于 2020-01-14 19:19:07
问题 Is there anyway to say... Include a version number when creating a text file? Basically, my process is writing a text file that I need to check if there's a newer version available. My plan was to use FileVersionInfo to determine the current version and the version on the PC. However, I can't figure out how to write the file to the PC with a version attached to the file. Any ideas? 回答1: Typical options here include; hashing the contents and comparing that relying onthe audit dates storing

How to check if file is in use in c#? [duplicate]

巧了我就是萌 提交于 2020-01-14 13:43:10
问题 This question already has answers here : Is there a way to check if a file is in use? (18 answers) Closed 3 years ago . How can I check if the excel file on which I am working (manipulating its data, deleting it or overwriting it) is in use by another program? And how to release it from the same? Please guide me using C#. 回答1: Attempt to open in with flags "for writing". If it fails, the file is "taken" by some other process. FileStream fileStream = null; try { fileStream = new FileStream(@"c

JTextArea's append () method doesn't seem to work

喜欢而已 提交于 2020-01-14 13:09:38
问题 We were assigned to create a simple compiler as a homework that will take set of instructions (containing variables, conditions, jumps, etc.) and evaluate them. That's already done, but I thought I'd make my program little bit more… “shiny”, and add the ability to load instructions from a text file, just for the sake of user comfort; however, it seems that the JTextArea 's append () method doesn't seem to really like me, as it does exactly nothing. Here's the relevant code: BufferedReader

JTextArea's append () method doesn't seem to work

女生的网名这么多〃 提交于 2020-01-14 13:09:12
问题 We were assigned to create a simple compiler as a homework that will take set of instructions (containing variables, conditions, jumps, etc.) and evaluate them. That's already done, but I thought I'd make my program little bit more… “shiny”, and add the ability to load instructions from a text file, just for the sake of user comfort; however, it seems that the JTextArea 's append () method doesn't seem to really like me, as it does exactly nothing. Here's the relevant code: BufferedReader

File.lastModified() painfully slow!

烈酒焚心 提交于 2020-01-14 10:22:49
问题 I'm doing a recursive copy of files and like xcopy /D I only want to copy newer files destination files (I cannot use xcopy directly since I need to alter some files in the copy process). In java I use lastModified() to check if the destination file is older than the source file and it's very slow. Can I speed up the process (maybe using JNI??)? Are there any other copy scripts that can do the job better (copy new files + regexp change some text files)? Copying files anyways is not an option