• Advertisement
Sign in to follow this  

Basic File IO

This topic is 3875 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

I'm working on some basic file IO. I can output to files just fine, but I can't seem to input to files. I have a file called ints.doc. It has the numbers 5, 6, 7, 8 in it, except without commas and with spaces. Whenever I read in the values I get random values, like the variables aren't being filled with anything. I'm sure I'm making some small mistake, but I just can't figure it out. In case it matters, I'm on a mac using Xcode 2.0. Thanks
#include <iostream>
#include <fstream>
using namespace std;

int main () {
	ifstream inputStream;
	int n1, n2, n3, n4;
	
	inputStream.open("ints.doc");
	
	if(inputStream.fail())
	{
		cout << "Failed to open file.";
		return -1;
	}
	
	inputStream >> n1 >> n2 >> n3 >> n4;
	
	cout << n1 << " " << n2 << " " << n3 << " " << n4;
	
	inputStream.close();
	
	return 0; 
}

Share this post


Link to post
Share on other sites
Advertisement
Do you have office installed on your box?
Is this .doc document a Microsoft Office Document?

If it is then you are reading the header of the document (several file types have headers)

Try using a simple .txt file, your code works fine on Visual Studio 2005 on a windows box.

If you are trying to read an office document then basic file IO is not going to give you want you want without parsing/processing the internal format of the document.

Share this post


Link to post
Share on other sites
Thanks

After opening the file in a basic text editor, I found out that it had a monster header file that was killing my input. After removing the header, it works perfectly.

James

Share this post


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

  • Advertisement