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.

Candy Rose & Lana Nikols in Agent Action Aftermath

So they came all the long way, dressed for the evening ball to find out where the secret meeting place of the world's most renowest villains is. They did expect the worst but not yet how to get off the building. Will they make it out, sneaking past the goons now streaming in from all around the property. And then she said something like: It's time to kick ass and chew bubble gum but I am all outta gum.

Keeping Contact! A Standalone Social RPG System

In einer magischen Welt und einer modernen Stadt: KEEPING CONTACT! - Standalone Social system for Solo Roleplaying  mittels Listen und vorgegebenen Regelsets. Einfach das Bild mit Bōkensha-kun klicken, wobei es Dich auf's Gügl Draif weiterleitet. Ah ja, anstossen nicht vergessen, haha. Cheers! :-) Just click the image to get the PDF. Come on! Bōkensha-kun is treating you with a jug of beer!  Don't leave him hanging there... he might faint doing a solo drinking challenge! KEEPING CONTACT! is a standalone roleplaying system which is usable with many other roleplaying game rulesets. Play along with a full set of lookup tables, rules and ideas. Never get bored even when the internet connection broke down or no friend indeed see's your RPG need. Have phunn and stay creative! =) Cheers, Sam. 

Candy Rose & Lana Nikols intimate moment

   Enjoy!

Einführung in das Ultimate Pose Master Werkzeugset Teil 1

 Einführung und Tabübersicht des Pose Masters. Erste Schritte im Bedienen des Posierungswerkzeugs.