   Reading from file
  I am just wondering, how would you read from a file. Whether its numbers or letter. I use fstream and read character by character and use stringstream to convertchar to ints, and I also use getline to read from file. Is there a proper way,or better way. Maybe some code would be nice. ThanksEDIT: say file is .txt format[Edited by - tnutty on May 23, 2009 12:27:44 PM]
  The "best way" depends on the file format in question.   ;  Just a clue: there are other functions in fstream, like the operators. Look it up on: CPlusPlus Reference.Emiel1   ;  Yes I know. Would expert even use fstream or would they use sat cstdlib forI/O. Either way, how would you normally extract data from file?   ;Quote:Original post by tnuttyYes I know. Would expert even use fstream or would they use sat cstdlib forI/O. Either way, how would you normally extract data from file?I would definately use fstream. How to extract data? As rip-off has said, it depends on the file format.If you want to extract a well-known binary structure, you would create a binary fstream and do something like:[...]int x, z;double y;f >> x;f >> y;f >> z;[...](I guess you can just do f >> x >> y >> z; but am not sure)If you want to read a text file line by line, you can use readline(). If you have a hard control over what you do, perhaps you would want to read char by char. It depends.So we can't really give you a generic answer. If you are doing something concrete tell us about it and we'll se what we can do.   ;  I was reading heights from a .txt file into an array. The way I was doing itwas is reading it char by char, and the using sstream to convert the char to ints. I feel like there are better way to do this. And because of this I waswondering how one would properly read from a file if he wanted to read values from a .txt file.   ;Quote:Original post by tnuttyI was reading heights from a .txt file into an array. The way I was doing itwas is reading it char by char, and the using sstream to convert the char to ints. I feel like there are better way to do this. And because of this I waswondering how one would properly read from a file if he wanted to read values from a .txt file.Could you post an example of the type of file you're wanting to read? Maybe you could post some of your current code for reading the file as well.   ;  well here is a code. Haven't worked with sstream much so the only way I figured out to read new char is by clearing the sstream every time its read, so a new char would be converted to int. void ImportHeightFromFile(float HeightArray[][32]){int i =0;int j=0;char a;ifstream iFile("HeightFile.txt");  stringstream st;while(!iFile.eof() && i<32){iFile >> a;st << a;st >> HeightArray[j];st.clear(ios::goodbit,0); //clear stream.if(j<12) j++;else{j =0;i++;}}iFile.close();};  There are a lot of improvements that can be made here, but again, I think it would help if you were to post an example file (or at least part of one) so that we can see what the input looks like. That'll make it easier for us to provide useful feedback.   ;  Say the array was 5x5 so an example file will be : 0000000110002200011000000