|
-
Nov 17th, 2001, 11:32 AM
#1
Thread Starter
Fanatic Member
Read and write
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.

prog_tom
JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
http://physics.sviesoft.com/forum
-
Nov 17th, 2001, 02:32 PM
#2
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
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|