Direkt zum Hauptbereich

C++ Recurring Issues ¦ Reading a file

Opening Files for Reading Only

Firstly, a text file is a file that you can open and read its contents visually - for example, C source files, .txt files, HTML etc - anything that looks "neat" in Notepad.

A binary file is a file that is written in machine code - usually seen as a mass of weird characters in Notepad! Examples are bitmaps, executables etc.

For these tutorials, we're going to be looking at standard text files. If you wanted to open a binary file, simply put a b at the end of the second argument of fopen.

To open a text file for reading only, pass "r" as the second argument of fopen, as demonstrated in this example:

#include 

int main() {
FILE *file; /
* declare a FILE pointer */

file = fopen("data/hello.txt", "r");
/* open a text file for reading */

if(file==NULL) {
printf("Error: can't open file.\n");

/* fclose(file); DON'T PASS A NULL POINTER TO fclose !! */
return 1;
}
else {
printf("File opened. Now closing it...\n");
fclose(file);

return 0;
}
}

Other Options When Opening Files

There are 4 other options I'd like to mention:

"a" lets you open a text file for appending - i.e. add data to the end of the current text.

"r+" will open a text file to read from or write to.

"w+" will create a text file to read from or write to.

"a+" will either create or open a text file for appending.

Add a "b" to the end if you want to use binary files instead of text files, like this:

"rb", "wb", "ab", "r+b", "w+b", "a+b".


Using fgetc To Read Characters

First, we will look at a function that allows us to read one character at a time.

fgetc requires one argument - a stream. This could either be your FILE pointer OR stdin. Writing fgetc(stdin) is the equivalent of writing getc(). We're going to be passing a FILE pointer to it.

fgetc returns the int that denotes a character from the file. For example, if the first character in the file was "A", fgetc would return 65.

After returning an int, the FILE pointer is moved to point to the next character. Should the pointer be moved beyond the last character, fgetc returns EOF - the END OF FILE constant.

We usually use EOF to determine when we've finished reading from a file.

For the next example, I created a text file called numbers.txt and placed it in the same directory as my C program. Here are its contents:

one
two
three

four
five

Here's a demonstration the use of fgetc:


#include

int main() {
char c; /* declare a char variable */
FILE *file; /* declare a FILE pointer */

file = fopen("numbers.txt", "r");
/* open a text file for reading */

if(file==NULL) {
printf("Error: can't open file.\n");
/* fclose(file); DON'T PASS A NULL POINTER TO fclose !! */
return 1;
}
else {
printf("File opened successfully. Contents:\n\n");

while(1) { /* keep looping... */
c = fgetc(file);
if(c!=EOF) {
printf("%c", c);
/* print the file one character at a time */
}

else {
break; /* ...break when EOF is reached */
}
}

printf("\n\nNow closing file...\n");

fclose(file);
return 0;
}
}

Kommentare

Die beliebtesten Posts eines Jahres

STOP RETARGETING !!! Paragon Char with ALS

  It's a bit older and made for UE4 but I bet you will get the trick. ;-) Turn the way it's done around by exporting as FBX-file to Blender and renaming the bones. Then back as FBX to Unreal Editor et voilà, no more retargeting nonsense.

DAZ Studio 3D Render Falba Feuerschweif

       

Candy Rose & Lana Nikols intimate moment

   Enjoy!

Why Do Video Game Studios Avoid Blender?

Animationen und Übergänge

Immer wieder faszinierend und anziehend: Animationen und Übergänge für 2D Sprites aus animierten 3D Modellen erstellen. Ein knackiges Thema, das frisch und ungeplant angegangen doch eine harte Nuss sein kann, wenn's nach etwas aussehen soll. Speziell die Übergänge und deren anschliessende Programmierung sind definitv mit mehr Fleissarbeit und heikler als anfangs angenommen. Ein paar der Versuche wollte ich zumindest mal veröffentlichen.  Eine Animation vom letzten Jahr für eine Spielfigur im klassischen Diablo-Stil. Das erste ist ein animated PNG. Scheint nicht ganz so zu funktionieren, wie ich dachte. Drum fix noch ein GIF draus gemacht, nur hier fehlt das Anti-Aliasing und sieht vergleichsweise körnig aus. Die Animation an sich ist schön und gut. Das Kombinieren verschiedener Animation ist dann wieder eine ganz andere Geschichte. Eine Zeitlang hatte ich noch versucht alle Basisanimationen für eine Spielfigur zusammen zu stellen, doch spätestens als ich es mit der Kamera vergeigt