Problems writing some chars to files with fprintf

This is a discussion on Problems writing some chars to files with fprintf within the C Programming forums, part of the General Programming Boards category; I'm trying to write to files with fprintf, here are some examples: Code: FILE *fp; fp = fopen("teste.txt", "w") fprintf(fp, ...

  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 06: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 12:11 PM.

  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, 11:00 PM
  2. reading and writing to files in windows
    By Dohojar in forum Windows Programming
    Replies: 8
    Last Post: 06-26-2003, 03: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, 12:33 PM
  5. Problems with resource files
    By Unregistered in forum C++ Programming
    Replies: 18
    Last Post: 08-31-2001, 09:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21