Hello, I would like to ask for help Programmer:
(1). How to read and write to a specific file?
(2). How to write a string into the Memory?
(3). How to load a string from a specific Memory?
(4). Thank you.
Printable View
Hello, I would like to ask for help Programmer:
(1). How to read and write to a specific file?
(2). How to write a string into the Memory?
(3). How to load a string from a specific Memory?
(4). Thank you.
1) There are 3 possible ways:
a) the C way:
File storage: pointer to struct FILE
FILE* file;
Open files: fopen(char* filename, char* openType)
file = fopen("foo.txt", "rt");
This opens the file foo.txt for reading (r, alternatives are w for write and a for append) in text mode (t, automatically converts \r\n pairs in file into \n in memory and vice versa, alternative is b for binary mode). The openType must always contain one character from each one of the categories.
Close files: fclose(FILE* file);
Read/Write:
fscanf and fprintf (like scanf and printf, but to a file):
fprintf(file, "A number: %d", i);
fgets/fputs to write/read simple strings.
fread/fwrite to write/read binary data:
int i = 5;
FILE* file = fopen("foo.bin", "wb");
fwrite(&i, sizeof(int), 1, file);
fclose(file);
b) the C++ way:
File storage: objects of the ifstream (input) and ofstream (output) classes.
Open files: usually through the constructor, but there are special functions too.
Close files: destructor or function
Write/Read: stream operators << and >> and functions
c) the WinAPI way:
File storage: standard windows HANDLE
Open files: CreateFile
Close files: CloseHandle
Read/Write: ReadFile/WriteFile (only binary mode)
2 and 3) what do you mean? Which memory?
4) NP