Skip to main content
gamedev.net
🔒 Locked

Basic File IO

Started by Hammonjj Jun 15, 2007 at 10:07 PM 1 replies 650+ views
Original Post
Hammonjj
Hammonjj
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; 
}
James HammondUniversity of ColoradoDepartment of Computer Science
ldeej
ldeej
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.
Hammonjj
Hammonjj
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
James HammondUniversity of ColoradoDepartment of Computer Science

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.