file-handling

modify existing contents of file in c

不羁的心 提交于 2019-11-28 10:26:42
int main() { FILE *ft; char ch; ft=fopen("abc.txt","r+"); if(ft==NULL) { printf("can not open target file\n"); exit(1); } while(1) { ch=fgetc(ft); if(ch==EOF) { printf("done"); break; } if(ch=='i') { fputc('a',ft); } } fclose(ft); return 0; } As one can see that I want to edit abc.txt in such a way that i is replaced by a in it. The program works fine but when I open abc.txt externally, it seemed to be unedited. Any possible reason for that? Why in this case the character after i is not replace by a , as the answers suggest? Analysis There are multiple problems: fgetc() returns an int , not a

How do I clear the whole contents of a file in C?

纵饮孤独 提交于 2019-11-28 09:02:29
I have a file with some of user1's data. I want to use the same file for user2 by clearing the content of the file. My idea is that when a new user comes, data of the previous user should be clear and the same file should be ready for the new user. As @stefan said using fopen() with "w" mode will do the job for you. When you open a file with "w" flag it creates an empty file for writing. If a file with the same name already exists its contents are erased and the file is treated as an empty new file. If the file is already open you can use freopen() function from stdio.h with "w" mode as it

PermissionError: [WinError 32] The process cannot access the file because it is being used by another process

偶尔善良 提交于 2019-11-27 23:42:09
My code is for a script that looks at a folder and deletes images that are under a resolution of 1920x1080. The problem I am having is that when my code runs; import os from PIL import Image while True: img_dir = r"C:\Users\Harold\Google Drive\wallpapers" for filename in os.listdir(img_dir): filepath = os.path.join(img_dir, filename) im = Image.open(filepath) x, y = im.size totalsize = x*y if totalsize < 2073600: os.remove(filepath) I get this error message: Traceback (most recent call last): File "C:\Users\Harold\Desktop\imagefilter.py", line 12, in <module> os.remove(filepath)

Modifying a text file in a ZIP archive in Java

荒凉一梦 提交于 2019-11-27 23:32:11
My use case requires me to open a txt file, say abc.txt which is inside a zip archive which contains key-value pairs in the form key1=value1 key2=value2 .. and so on where each key-value pair is in a new line. I have to change one value corresponding to a certain key and put the text file back in a new copy of the archive. How do I do this in java? My attempt so far: ZipFile zipFile = new ZipFile("test.zip"); final ZipOutputStream zos = new ZipOutputStream(new FileOutputStream("out.zip")); for(Enumeration e = zipFile.entries(); e.hasMoreElements(); ) { ZipEntry entryIn = (ZipEntry) e

Difference between System.getProperty(“line.separator”); and “\n” ?

时光总嘲笑我的痴心妄想 提交于 2019-11-27 17:44:55
问题 While developing GUI with Java FX , I seem to get different results with System.getProperty("line.separator"); and "\n" during writing to a file or getting data from internet. What basically is the difference ? 回答1: System.getProperty("line.separator") returns the OS dependent line separator. On Windows it returns "\r\n" , on Unix "\n" . So if you want to generate a file with line endings for the current operating systems use System.getProperty("line.separator") or write using a PrintWriter .

Why am i getting this warning in “if (fd=fopen(fileName,”r“) == NULL)”?

跟風遠走 提交于 2019-11-27 15:43:08
FILE *fd; if (fd=fopen(fileName,"r") == NULL) { printf("File failed to open"); exit(1); } This is a code snippet. When I compile it with gcc, i get the following warning:- warning: assignment makes pointer from integer without a cast When I put fd=fopen(argv[2],"r") within brackets, the problem gets solved.. I am not able to understand where am i converting integer to pointer when the brackets are not put. Due to operator precedence rules the condition is interpreted as fd=(fopen(fileName,"r") == NULL) . The result of == is integer, fd is a pointer, thus the error message. Consider the

How to check if a file is empty in Bash?

那年仲夏 提交于 2019-11-27 10:42:12
I have a file called diff.txt. Want to check if it is empty. Did something like this but couldn't get it working. if [ -s diff.txt ] then touch empty.txt rm full.txt else touch full.txt rm emtpy.txt fi Misspellings are irritating, aren't they? Check your spelling of empty , but then also try this: #!/bin/bash -e if [ -s diff.txt ] then rm -f empty.txt touch full.txt else rm -f full.txt touch empty.txt fi I like shell scripting a lot, but one disadvantage of it is that the shell cannot help you when you misspell, whereas a compiler like your C++ compiler can help you. Notice incidentally that I

Why is my iOS app not showing up in other apps' “Open in” dialog?

佐手、 提交于 2019-11-27 10:32:16
I am trying to implement the registering process that allows my iOS app to show up in the "Open in" list of other applications (as described in Apple's Document Interaction Programming Topics ). I want my app to be able to handle audio from any app that will provide a standard audio file format (MP3, AIFF, WAV, etc.). As I understand it, all I should need to do is add the CFBundleDocumentTypes key, with relevant subdata, to my app's Info.plist. This is what I put in (via Xcode 4's Document Types editor): <key>CFBundleDocumentTypes</key> <array> <dict> <key>CFBundleTypeIconFiles</key> <array>

Reading a string from file c++

隐身守侯 提交于 2019-11-27 04:43:06
问题 I'm trying to make billing system for my father's restaurant just for practice. So, I'm facing the problem that I can't be able to read the complete string one time.e.g If there were Chicken burger in txt file than compiler read them but break them into two words. I'm using the following code and the file is already exist. std::string item_name; std::ifstream nameFileout; nameFileout.open("name2.txt"); while (nameFileout >> item_name) { std::cout << item_name; } nameFileout.close(); 回答1: To

How do I clear the whole contents of a file in C?

醉酒当歌 提交于 2019-11-27 02:35:07
问题 I have a file with some of user1's data. I want to use the same file for user2 by clearing the content of the file. My idea is that when a new user comes, data of the previous user should be clear and the same file should be ready for the new user. 回答1: As @stefan said using fopen() with "w" mode will do the job for you. When you open a file with "w" flag it creates an empty file for writing. If a file with the same name already exists its contents are erased and the file is treated as an