문제

int main()
{
    FILE *fs,*ft;
    char ch;
    fs=fopen("main.c","r");
    if(fs==NULL)
    {
        printf("can not open source file\n");
        getch();
        exit(1);
    }
    ft=fopen("demo.docx","w");
    if(ft==NULL)
    {
        printf("can not open target file\n");
        getch();
        exit(1);
    }
    while(1)
    {
        ch=fgetc(fs);
        if(ch==EOF)
        {
            break;
        }
        fputc(ch,ft);
    }
    fclose(fs);
    fclose(ft);
    return 0;
}

Using the above code, I tried to copy the contents of main.c to demo.docx.
The program worked fine, but when I externally opened demo.docx, it crashed.
Is there a problem from my side, or we can not write to a docx file using c?

도움이 되었습니까?

해결책

The Word document isn't written like a normal text file, but rather written in a very specific format which you would have to follow. This can of course be done in C but I strongly would recommend against it and instead try to find some library to use or use a different language and framework. Both C#/.Net and Java have good options for working with Office documents (for Java there's Apache POI for example).

If you really want to waste your time you can find the specs for the Office files here: http://msdn.microsoft.com/en-us/library/cc313105%28v=office.12%29.aspx

다른 팁

MS Word *.docx files are not normal text files and you need a specialized library to access such files such as http://libopc.codeplex.com/.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top