Nathan Fig
Nathan Fig

Reputation: 15109

write() creating partially readable / corrupt files

I have a long string (DNA sequence with \n at regular intervals) that I'm trying to write to a file.

Writing with the code below, I have a file that cannot be fully accessed by getline in a C++ program- that is, istream seems to think the file only has about a dozen lines. Likewise, if I do cat or more on the file I only see about a dozen lines of the file. However, if I open the file with an editor (like gedit) the whole file is there, and if I resave I'm suddenly able to read the whole thing.

  output = open(fileFasta, 'w')
  lines = vSeq[1].split('\n')
  # Tried this
  output.write(vSeq[1])
  # And this
  for line in lines:
    output.write(line)
    output.flush()
  output.close()

I'm obviously not finishing the file in some important way that gedit does. Ideas?

Upvotes: 0

Views: 1317

Answers (2)

Nathan Fig
Nathan Fig

Reputation: 15109

Used ghex to discover my source file was using carriage returns (\r) instead of newline (\n) characters.

Upvotes: 2

Perhaps your C++ program don't flush the output stream enough. Or perhaps there is a null byte somewhere...

Upvotes: 0

Related Questions