Thread: Problems writing some chars to files with fprintf

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    158

    Writing to files problem, file created is octet-stream and not text

    I'm trying to write to files with fprintf, here are some examples:

    Code:
    FILE *fp;
    
    fp = fopen("teste.txt", "w")
    
    fprintf(fp, "# Author: %s\n", author);
    however, if author, for instance, has some chars like á ò ê or even €, the text file will not be saved correctly. Why?

    Easy to fix? How?
    Last edited by Nazgulled; 04-18-2006 at 05:59 PM.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    works for me using VC++ 6.0
    Code:
    #include <stdio.h>
    
    int main(int argc, char* argv[])
    {
    	FILE* fp = fopen("myfile.txt","w");
    	fprintf(fp,"%s\n","á ò ê or even €,  ");
    	fclose(fp);
    	return 0;
    }
    below also works
    Code:
    	char author[] = "á ò ê or even €,  ";
    	fprintf(fp,"%s\n",author);
    Last edited by Ancient Dragon; 04-18-2006 at 11:11 AM.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    158
    After testing this a bit more I found something...
    First, I'm compiling this on linux using gcc of course...

    Files with special chars saved in .txt extension -> work fines, they are text files

    Files with special chars saved in other extension -> file is not a text file but if I open it in a text editor I see all the text just as I see when I save it with .txt extension

    Files with or without special chars -> work fine and they are text files

    so the, problem is, when I save a file with those special chars with a different extension than .txt, however, I need it to be just text (and it is) but to save it in a different extension. that seems to be my problem.

    When I go the file properties, I see there on the mime type: application/octet-stream instead of text/plain.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    158
    no one knows why? It would be nice if I could fix this in the next 12 hours, I'll then have to present the whole project to my teachers...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. reading and writing to files
    By cgod in forum C++ Programming
    Replies: 2
    Last Post: 10-16-2004, 10:00 PM
  2. reading and writing to files in windows
    By Dohojar in forum Windows Programming
    Replies: 8
    Last Post: 06-26-2003, 02:07 AM
  3. Problems openning huge files
    By acoelho74 in forum C Programming
    Replies: 4
    Last Post: 02-11-2003, 07:02 PM
  4. trying to read all asci char's in files
    By brane_sail in forum C++ Programming
    Replies: 1
    Last Post: 09-02-2002, 11:33 AM
  5. Problems with resource files
    By Unregistered in forum C++ Programming
    Replies: 18
    Last Post: 08-31-2001, 08:45 AM