|
C / C++ |
Java |
C/C++ explanation |
Java explanation |
|
FILE *fs = fopen("outputfile.txt",
"w"); |
File fs = new
File("outputfile.txt"); |
Create file (on the
hard disk usually) ready for writing to.
Use "a" instead
of "w" for append
instead. The second line just
checks if the file could be created. |
Create file (on the
hard disk usually) ready for writing to.
The BufferedWriter isn't strictly necessary, but speeds up the process
of writing considerably. UseFileWriter(fs, true); instead to append
data instead of overwrite
it. Don't forget to allow for
an IOException throw
(i.e. by wrapping it in a
try/catch block, or putting
"throws IOException"
right next to the method
declaration). |
|
FILE *fl = fopen("input.txt",
"r"); |
File fl = new
File("input.txt"); |
Prepare a file ready
for reading. The second line
makes sure the file exists. This is now ready for
use. However, if you want
super fast memory access, then the green
part comes in handy. The first
green line creates an array ready for the
file to be transferred, and the second,
transfers it (getSize()
is a custom function - see below). Finally,
the rewind function rewinds the file position indicator back to the start of the file. |
Prepare a file ready
for reading. The BufferedReader part isn't strictly
necessary, but speeds up the reading
process significantly. This is now ready
for use. However, if you
want super fast memory access,
then the green part comes
in handy. The first green line
creates an array ready for the
file to be transferred, and the final line transfers it. |
|
int getSize(FILE*
fin) { fseek(fin,0,2); int size=ftell(fin); rewind(fin); return size; } |
fl.length() |
Useful function to retrieve the
size of a file. |
Retrieve the size of
a file (fl was declared above). |
|
FILE *vFile1 = fopen ("myfile.bin","wb"); |
... |
Declaring write and read
files ready for binary writing
to a file named "myfile.bin". |
... |
|
C / C++ |
Java |
C/C++ explanation |
Java explanation |
|
char
ch = array[100]; |
char
ch = array[100]; |
If the green section was included above, then this
will read one character from the array
(which has the file data
in!) and put it into ch. |
If the green section was included above, then this
will read one character from the array
(which has the file data
in!) and put it into ch. |
|
char
ch = fgetc(fl); |
char
ch = myreader.read(); |
If you didn't want to
create a massive array to put the file data in (i.e. you excluded the green code), then this command
will read characters directly from the file. After each fget,
the file position indicator is advanced ready to read
the next character. |
If you didn't want to
create a massive array to put the file data in (i.e. you excluded the green code), then this command
will read characters directly from the file. As it is, it will return ints,
so put "(char)"
in front of "myreader.read()" to return chars. After each read(),
the file position indicator is advanced ready to read
the next character. |
|
bool
b = fgets (mystring ,
10000 , fl); |
String
s=myreader.readLine(); |
Like above, but reads a whole line from the
file instead of just a single character. The first argument
(mystring) is the string (char array) which to write to.
The second is the maximum amount
of characters allowed before cutoff, and the third
is the file from which to read
the line from. bool b just returns whether the operation
was successful (useful for end
of file checking etc.) |
Like above, but reads a whole line from the
file instead of just a single character. If the end
of the file is reached, then readLine returns null. |
|
fputs("Hello", fs); |
mywriter.write("Hello"); |
The fputs command appends a string to the
file. Also, the fclose function (see below) makes
the file available for the OS to
use. You might want to
try: fwrite("Hello",1,
sizeof("Hello"),
fs); instead if you
have problems (i.e. if you want
to write zeros to a file). The 1 represents the size of the variable type (char = 1 byte), and sizeof(hello)
is how many elements there are in "hello". |
The write command writes a string to the
file. Finally, the close function (see below) makes
the file available for the OS to
use. |
|
fprintf(fs,"%s %f %i","Hello",1.7,2); |
... |
Similar to above, but works more in line with the printf
syntax. Nice and flexible, since floats etc. can be printed as chars too (without
going through intial conversion). |
... |
|
fclose(fs); |
mywriter.close(); |
Close file to release to the
OS, making it ready to use. |
Close file to release to the
OS, making it ready to use. |
|
fseek(fl, 100, 0); |
--- |
Move the file position indicator to the 100th character of the file. |
--- |
|
fwrite(&i, sizeof(int), 1, vFile1); |
... |
Write variable
data (like integers, arrays or strings)
to a file, ready to load back
another time. In our case, "i" is a singler integer, "mystring" is a 20 char string (i.e a 1d array of chars), and "array2d"
DESPITE POSSIBLE APPEARANCES TO THE CONTRARY, is a 2D array
of floats (6x4 elements). |
... |