Read from a text file

May I have a good website I could visit for example on how to read from an unformatted Text File. Please and thanks.
1
2
char ch;
while(in.good()) in.get(ch); //get characters 1 by 1.... 


there you go...

When reading from an unformatted file (every file contains bytes, you can't limit your question to just text files: all of them are "text" files), you generally have to check the "format" to make sure that there is legible information to read. For example, if each line is (supposed to be) null-terminated, you could check to see if there are any '\0' characters in the file (meaning there is data). there are many different ways to read information. I'm sure there's somthing "standard" (like encoding, etc...) but generally, C++ lets you read files any way you want to (reverse, forwards, up/down, side side, round in round, you name it, but you will have to emplement it first ;))

If you're asking for somthing specific, then you should say it.
Last edited on
ok. How would I input information from a text file into my program?
I just told you...
I wouldn't recommend doing the way that IWishIKnew mentioned. You are better off doing
1
2
3
4
while(in >> input)
{
    //do something with what ever was just read in
}


@Stephanie could you be a little more specific on what you are trying to do? If you have an formatted text file that almost seems like reading in arbitrary information. If you could show in an example file you are trying to read it would help us to help you. Reading in the actual information is really easy.
@giblit thanks. suppose I was to take a text file from out of my documents to input into my c++ program, how would I go about doing that? you understand?
Well it all depends on how the information is stored in the file. To open the actual file though you just want to give the path when opening.

So for you it is probably something like:
1
2
3
4
std::ifstream in("c:/users/stephanie/my documents/file.txt");

//or
std::ifstream in("c:\\users\\stephanie\\my documents\\file.txt");


Though if you are not on windows it will have a different location.

If you could show an example text file and what exactly you are trying to do it would help greatly.
Topic archived. No new replies allowed.