JohnOne Posted January 25, 2012 Share Posted January 25, 2012 (edited) I am trying to read a file and output/return it all at once.I was expecting to do this with this code...ifstream myReadFile; myReadFile.open(sFile); char output[40960]; if (myReadFile.is_open()) { while (!myReadFile.eof()) { myReadFile >> output; } } myReadFile.close(); cout<<output;But (probably unsurprising to others) it does not do as I expected.It outputs (returns in my dll) just the last line of the file, and furthermoreignores all spaces.I'd appreciate it if someone would be so kind as to steer me towarda solution. Edited January 26, 2012 by JohnOne AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
jaberwacky Posted January 25, 2012 Share Posted January 25, 2012 I think you're paving over output in the loop. Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
JohnOne Posted January 25, 2012 Author Share Posted January 25, 2012 oops, of course I was, cheers. Any idea about why spaces are stripped? AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
jaberwacky Posted January 25, 2012 Share Posted January 25, 2012 I think you'd use output += myReadFile.Get() Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
JohnOne Posted January 26, 2012 Author Share Posted January 26, 2012 (edited) Still cannot get my head around this, I have tried 2 approaches, both of which hang.ifstream myReadFile; myReadFile.open(sFile); char output[40960]; char tmp[1024]; if (myReadFile.is_open()) { while (!myReadFile.eof()) { myReadFile.getline(tmp,1024); // hangs //myReadFile >> tmp; // works but strips white space //myReadFile >> noskipws >> tmp; // hangs strcat(output,tmp); } } myReadFile.close(); cout<<output;EDIT:Scrap that, as you suggested .get() works, .getline() does not.Thanks.One question, when using get(), is this reading one char at a time? Edited January 26, 2012 by JohnOne AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
jaberwacky Posted January 26, 2012 Share Posted January 26, 2012 I'll have to fire up codeblocks, been a while since I got my hands dirty. Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
JohnOne Posted January 26, 2012 Author Share Posted January 26, 2012 I don't think it is, as if I make char tmp[1024] any less I do not get the results I require, and if I make it [2], it crashes. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
jaberwacky Posted January 26, 2012 Share Posted January 26, 2012 (edited) This works for me:expandcollapse popup#include <iostream> #include <fstream> #include <String> using namespace std; int main() { ifstream myReadFile; string sFile = "C:UsersMatthewDesktoptest.txt"; myReadFile.open(sFile.c_str(), ios::in); if (myReadFile.is_open()) { string output; while (myReadFile.good()) { output += myReadFile.get(); } myReadFile.close(); cout << output; } return 0; } Edited January 26, 2012 by LaCastiglione Helpful Posts and Websites: AutoIt3 Variables and Function Parameters MHz | AutoIt Wiki | Using the GUIToolTip UDF BrewManNH | Can't find what you're looking for on the Forum? Link to comment Share on other sites More sharing options...
JohnOne Posted January 26, 2012 Author Share Posted January 26, 2012 Cheers for alternative. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now