Reading a character from a file...

Started by
3 comments, last by ToohrVyk 19 years, 2 months ago
How do you read one individual character from a file? I'm trying to load the file into a two-dimensional array, but i don't know how to get the character. is there a simple function? I'm using C++.
Advertisement

#include <fstream>using namespace std;{fstream input( "myfile.txt", ios::in );char tile;while( tilesInFile( ) ) {   input >> tile;   useTile( tile ); }}


Also, google for fstream.
A C alternative to ToohrVyk's post:
#include <stdio.h>FILE *fp = fopen( "somefile.txt", "r" );char c = getc( fp );// use c as you want
Evillive2

#include <fstream>

using namespace std;

ifstream input;

int main()
{

input.open("file.txt");

char array[4][4];
input >> array[0][0];

input.close();

return 0;
}


Eric Wright o0Programmer0o
evillive2: you leaked your file handle. Don't forget to close it.

OP: if using evillive2's method with a C++ compiler, you must include <cstdio> instead of <stdio.h>.

This topic is closed to new replies.

Advertisement