Jump to content
  • Advertisement
Sign in to follow this  
ecr0n

Reading a character from a file...

This topic is 5021 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement


#include <fstream>
using namespace std;

{
fstream input( "myfile.txt", ios::in );
char tile;
while( tilesInFile( ) ) {
input >> tile;
useTile( tile );
}
}



Also, google for fstream.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

#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;
}


Share this post


Link to post
Share on other sites
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>.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!