File opening, saving, etc.:

Declaring Variables (for next section)

C / C++

Java

C/C++ explanation

Java explanation

FILE *fs = fopen("outputfile.txt", "w");
if (!fs) { printf("Can't write file"); exit(0); }

File fs = new File("outputfile.txt");
FileWriter fw = new FileWriter(fs);
BufferedWriter mywriter;
mywriter = new BufferedWriter(fw);

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"); 
if (!fl) { printf("Can't open file"); exit(0); }
char *array=(char *)malloc(getSize(fl));
fread(array, 1, getSize(fl), fl);
rewind(fl);

File fl = new File("input.txt");
FileReader fis;
fis = new FileReader(fl);
BufferedReader myreader;
myreader = new BufferedReader(fis);
byte[] array=new byte[(int)fl.length()];
fis.read(array);

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");
FILE *vFile2 = fopen ("myfile.bin","rb");

...

Declaring write and read files ready for binary writing to a file named "myfile.bin".

...

 

The commands below rely on the variables as declared above, and should go into a function (eg: main)

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);
fwrite(mystring, sizeof(char), 20, vFile1);
for(int n=0; n<6; n++)
  { fwrite(array2d[n], sizeof(float), 4, vFile1);}
fclose (vFile1);

// At this point, the file of variable data is on
// hard drive, ready for use another day.

fread (&i, sizeof(int), 1, vFile2);
fread (mystring, sizeof(char), 20, vFile2);
for(int n=0; n<6; n++)
  {fread(array2d[n], sizeof(float), 4, vFile2);}

// Now you can do stuff with
// i, mystring[] and array2d[][]

fclose(vFile2);     // Finally, close the file.

...

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).

Once we have used fwrite to write the variables to a file, we can later use fread to load them back in again at a later time (even from another run of the program).

The first parameter in fwrite (or fread) is the pointer to the data, the 2nd is the type size, the 3rd is the number of elements to write, and the 4th is the FILE pointer (declared earlier).

(Note that we haven't declared the variables (i, mystring, and array2d), or put anything inside them - that's up to you).


...