avinashse
avinashse

Reputation: 1460

'File is corrupted' when opening docx file when created in C

I am using "UNIX" (on my virtual machine) and generating ".docx" file there using "C", after getting the file into Windows, when I am opening the file it's saying "the file is corrupted, can't be opened" and then its not opening. I am using MS-Word 2010.

Here is the piece of code I am using:-

Write_to_file(){
 FILE *fp;
 if((fp=fopen("hello.docx","w"))==(FILE*)NULL){
  printf("Error opening file");
  return 0;
 }
 fprintf(fp,"Hello World");
 fclose(fp);
}

Upvotes: 1

Views: 631

Answers (2)

lulyon
lulyon

Reputation: 7265

Just with an extension name (doc docx) does not make the file a MS word file. Your code is only writing a text file. You can dectect this by file command under Linux.

Please reference this http://msdn.microsoft.com/en-us/library/cc313105(v=office.12).aspx, and write the REAL MS document file.

Upvotes: 5

user123
user123

Reputation: 9071

A doc file is not a simple text file. You'd want to use the txt format:

fopen("hello.txt", "w");

To actually read/write a doc file, you'd need to use a library designed specifically to read them and write them.

The spec for MS-DOC files is pretty lengthy, so I wouldn't implement my own reader/writer if I were you.

Upvotes: 4

Related Questions