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

Why Do Video Game Studios Avoid Blender?

Candy Rose & Lana Nikols intimate moment

   Enjoy!

DAZ Studio 3D Render Falba Feuerschweif

       

Unreal Engine Animation Retargeting Toolkit

  Worry not; this worked for me with DAZ3D Genesis 8 figures retargetet for Manny and Quinn in UE5.2.1 - but yet I'm not satisfied with the tingle, tangle, tinkering around. It's better than the raw stuff you get from the or lezz say got. Epic pimped their system by now but still... there is so much troubling the ways of getting all the different models from different artists with their own naming conventions in a convenient fashion over to UE5. Prior to this I posted the forget about retargeting video, where the tutor was using the Advanced Locomotion System with different kinds of actors just by renaming the bones. Found a naice DAZ Script which does this in a semi-automatic style. But, o dizzy head, I should work on it instead of writ(h)ing and rambling about it.

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