How to create file in c with ansi encoding [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
How can I create file in C (not C++ or C#) with ANSI encoding?
When I create txt file by this code:
FILE* file_ptr;
file_ptr = fopen("new filler.txt", "r");
It creates file with UTF-8 encoding. How can I fix that automatically?

If you open the file in binary mode, there is no encoding at all and you are free to control every byte that goes into the file.
FILE * const f = fopen("myansi.txt", "wb");
fputs("\033+", f);
fclose(f);
The above puts the ANSI.SYS sequence for clearing the screen into the file.

Related

Keep file open throughout program execution vs open when needed and close [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I'm making a game, with a settings page, in C. I store these settings in a settings file. I have 2 options.
int WriteSettings(void) {
FILE *File;
return (memcmp(&Settings, &DefaultSettings, sizeof(struct SettingsStruct))? GetGameDir() && (File = fopen(SettingsPath, "w")) && fwrite(&Settings, sizeof(struct SettingsStruct), 1, File) == 1 && !fclose(File) : !unlink(SettingsPath))? 0 : AlertError("Unexpected error occurred while saving settings");
}
This code opens the settings file when needed, writes, and closes immediately or will delete the settings file if the data is identical to default. Another option is to keep the file open throughout game execution, and fwrite() and fflush() whenever the settings change and thus are saved. Which option is optimal?
nevilad:
Settings are typically changed by the user very rare, thus opening, writing and closing immediately would be better.

Learning the syntax of files in C [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I can't understand the syntax of writing in files in C.
I was requested in class to open a file named Numbers.txt and then print from 1 to 10 and each new line. I dont understand the syntax of it.
We use fopen,fwrite,fclose.
need help with understanding how to work with it.
You use a FILE to assign a file to something you can use. fopen opens a file depending on if the file exists or not.
FILE *pFile = fopen("Filename.foo","r");
This opens the file that is located in your project with the same filename. The 2nd parameter is the mode of the file — how you want to use it. Here is a table of modes. You must always check the return value to ensure it is not NULL, indicating that the program was unable to open the file as requested.
You always want to close the file when you're done editing it by using fclose.
fclose(pFile);
fwrite is one way of writing to a file; fprintf and family are another. Basically think about it like you opened a text editor and typed something in the file. You need to create a buffer (a place to store the numbers), then write the numbers into the file. Here is the reference for fwrite. I can't do all of your homework.

.txt Saving and naming the file [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Thats the deal, I am trying to make and offline Email Manager, where you can write and store emails in different folders.
Written in .txt files that contain the basic information of an email (to:, from:, body :, etc). I am having issues on the following process.
-Each email has an ID that helps on searching and administration. I want to give the name of .txt files the ID related to the email that contains. So .... How can I create a .txt file that has the name of the ID and the ID is given by the user.
Thank you.
You have to create a buffer to build your filename.
#define BUFFERSIZE 256 /* max size of a path */
FILE * fp ;
char buffer[BUFFERSIZE] ;
snprintf(buffer, BUFFERSIZE, "/path/to/file/%s.txt", your_id_string);
fp = fopen(buffer, "w+") ;

What file extensions can be opened with c [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
what types of file extensions can be opened with c
I know we can use .dat and .txt but what more can be used?
What types can be modified?
can I actually edit HTML, C++.
I'd like to open system 32.
An extension simply a convenient and conventional embellishment to a file name.
Any file can be opened with c (subject to permissioning).
You might want to choose between "text" and "binary" formats accordingly. But we're into specifics at this point, and such choices are down to you.
You can open any file extension in C. Only if you have permissions. Also, Bathsbeba answer is correct, just check out this link to see what are the main types of extensions.
To open a file with any extension, do this:
fopen ("file.extension", "your mode");
You can open it in binary and text formats. If you do not know how to open in binary, this is an example:
fopen ("file.extension", "wb");
To open in write binary mode.

What is mode of file pointer? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
What's mode pointer is what mode, 'insert' or overwrite?
Because I'm new to file pointer in C, so maybe my question is quite stupid. I'm sorry if anyone feels that.
And what I could do if I want to insert some character into existen file but don't lose any character in this file.
No file systems I know of support insert mode.
The C semantics for writing to file streams is either:
truncation for "w". The current file contents is lost upon successful fopen().
overwriting file contents for "r+". Only byte written explicitly to the file will overwrite existing contents, the rest is unchanged. Writing beyond the end of the file will extend its size, just like append mode.
appending to file for "a". Every write operation occurs at the end of file.
Note that "w" and "a" will also create the file is it does not exist.
In order to insert contents in the middle of an existing file, you must copy the contents to a new file, writing the new contents at the appropriate positions. You can rename the new file to the old name after closing both and removing the old one.
Depends on how you open it:
To append:
fopen("myfile.txt", "a");
To write (overwrite):
fopen("myfile.txt", "w");

Resources