Read from File After Write, Before Closing

Read from file after write, before closing

You need to reset the file object's index to the first position, using seek():

with open("outfile1.txt", 'r+') as f:
f.write("foobar")
f.flush()

# "reset" fd to the beginning of the file
f.seek(0)
print("File contents:", f.read())

which will make the file available for reading from it.

Problems with reading back from a file after writing to it and closing it in c

you shouldn't open your files in text mode while reading/writing as binary structures.

Whereas it has no effect on Linux/Unix, on Windows this has serious consequences. And it makes your files non-shareable between Windows and Linux.

Depending on the data LF <=> CR/LF conversion can corrupt/shift the data (removing the carriage return or inserting one)

in text mode in Windows, each LF (ASCII 10) byte is replaced by CR+LF (13+10 ASCII) bytes when writing (and reverse in reading: 13+10 => 10). Those 10 bytes can happen, for instance when writing year 1802 (hex: 0x70A) as binary.

Solution: use binary mode:

if((fp = fopen("this.dat","rb")) == NULL)

and

FILE* fp = fopen("this.dat","wb");

Note: In "text" mode, specifying a block size doesn't work since the size depends on the data. That probably answers your second question: last 100th record read is corrupt because you're reading too few bytes. I'm not sure about the details but since the system adds/removes bytes when writing/reading, block size can be buggy.

Should I close file while writing/reading concurrently?

You can't write to a closed file, so if you close it after each write operation, you also have to (re)open it before each write.

This would be quite inefficient. So instead leave it open, and only close it once your app is about to terminate (this is required because File.Write() does not guarantee that when it returns the data is written to disk). Since you're writing the file from HTTP handlers, you should implement graceful server termination, and close the file after that. See Server.Shutdown() for details.

Also, if the purpose of your shared file writing is to create some kind of logger, you could take advantage of the log package, so you would not have to use a mutex. For details, see net/http set custom logger.

Cannot read file after writing to it

You're not calling functions target.close and txt.close, instead you're simply getting their pointers. Since they are functions (or methods, to be more accurate) you need () after the function's name to call it: file.close().

That's the problem; you open the file in write mode which deletes all the content of the file. You write in the file but you never close it, so the changes are never committed and the file stays empty. Next you open it in read mode and simply read the empty file.

To commit the changes manually, use file.flush(). Or simply close the file and it will be flushed automatically.

Also, calling target.truncate() is useless, since it's already done automatically when opening in write mode, as mentioned in the comments.

Edit: Also mentioned in the comments, using with statement is quite powerful and you should use it instead. You can read more of with from http://www.python.org/dev/peps/pep-0343/, but basically when used with files, it opens the file and automatically closes it once you unindent. This way you don't have to worry about closing the file, and it looks much better when you can clearly see where the file is being used, thanks to the indentation.

Quick example:

f = open("test.txt", "r")
s = f.read()
f.close()

Can be done shorter and better looking by using with statement:

with open("test.txt", "r") as f:
s = f.read()

Is it necessary to close files after reading (only) in any programming language?

In general, you should always close a file after you are done using it.

Reason number 1: There are not unlimited available File Descriptors
(or in windows, the conceptually similar HANDLES).
Every time you access a file ressource, even for reading, you are reducing the number of handles (or FD's) available to other processes.
every time you close a handle, you release it and makes it available for other processes.

Now consider the consequences of a loop that opens a file, reads it, but doesn't close it...

http://en.wikipedia.org/wiki/File_descriptor

https://msdn.microsoft.com/en-us/library/windows/desktop/aa364225%28v=vs.85%29.aspx

Reason number 2: If you are doing anything else than reading a file, there are problems with race conditions, if multiple processes or threads accesses the same file..
To avoid this, you may find file locks in place.
http://en.wikipedia.org/wiki/File_locking

if you are reading a file, and not closing it afterward, other applications, that could try to obtain a file lock are denied access.

oh - and the file can't be deleted by anyone that doesn't have rights to kill your process..

Reason number 3: There is absolutely no reason to leave a file unclosed. In any language, which is why Python helps the lazy programmers, and automatically closes a handle that drops out of scope, in case the programmer forgot.

Reading from file right after Writing in it

Even if you can open both in read & write, this write operation is buffered, which means that it may not be written to disk unless you flush the stream (or you close the file).

Of course the code below works perfectly:

#include<iostream>
#include<fstream>
using namespace std;
int main(int argc,char* argv[]){

int x,y;
ofstream fd1(argv[1]);

cin>>x;
fd1<<x;

fd1.close();

ifstream fd2(argv[1]);
if (fd2.good())
{
cout << "read OK" << endl;
}
fd2>>y;
cout<<"just read "<<y<<endl;
fd2.close();

return 0;
}

writing back into the same file after reading from the file

Use a temporary file. Python provides facilities for creating temporary files in a secure manner. Call example below with: python modify.py target_filename

 import tempfile
import sys

def modify_file(filename):

#Create temporary file read/write
t = tempfile.NamedTemporaryFile(mode="r+")

#Open input file read-only
i = open(filename, 'r')

#Copy input file to temporary file, modifying as we go
for line in i:
t.write(line.rstrip()+"\n")

i.close() #Close input file

t.seek(0) #Rewind temporary file to beginning

o = open(filename, "w") #Reopen input file writable

#Overwriting original file with temporary file contents
for line in t:
o.write(line)

t.close() #Close temporary file, will cause it to be deleted

if __name__ == "__main__":
modify_file(sys.argv[1])

References here:
http://docs.python.org/2/library/tempfile.html



Related Topics



Leave a reply



Submit