Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Oct 26, 2004, 04:38 PM   #1
Earendil
macrumors 68000
 
Earendil's Avatar
 
Join Date: Oct 2003
Location: Washington
C++ Q. "in_stream.open("infile.dat");" In OSX?

So I'm in a beginner C++ class here at Whitworth college, and of course all the dev is on Windows computers. I've realized though that it is just about all cross platform code, and so have been using my powerbook and Xcode to do a lot of my homework. Until now...

we are bring in file information from a .dat files, which doesn't work for me in OSX. I few questions...

1. Is there a program that will save a .dat file? txt isn't an equivalent I found out.

Does there exist very SIMPLE and beginner level set of code that I can use in place of - in_stream.open("infile.dat");
where "in_stream" is the stream variable name?

Basically I now can't test my code on my computer, because I can't get .dat files, or any files, into the program. If the answer is elongated and involves changing too much of my code than it becomes pointless, because the point is to test my code, not to port it.

But, for the sake of learning, I'd enjoy knowing

Thanks guys!
Tyler

ps
I don't plan on asking many programming questions, but perhaps someone could suggest a Mac friendly, newbie friendly, C++ coding environment for cross platform questions? I know that Macrumors isn't exactly website for dev, there for neither are the forums to a degree...
__________________
Current: MacMini 2ghz / iPod Touch 2g / iPhone 4g
Retired: Alum Powerbook 1.25ghz / 4gb iPod Mini

"ooo! They have the internet on computers now!" - Homer J. Simpson

Last edited by Earendil; Oct 26, 2004 at 07:40 PM.
Earendil is offline   0 Reply With Quote
Old Oct 26, 2004, 08:53 PM   #2
iBert
macrumors regular
 
Join Date: Jul 2004
i don't think their should be any type of problem. If you are reading a file, whatever the extension is, what are you doing with the data in it? are you reading the data for a specific reason. when you import data to a code, one problem that can arise is if the file is in binary. Then you'd have to add a couple of things to read the file.

Perhaps you can be a little more specific with your problem.

Hope this can help any.
__________________
--MBP 2.4 GHz 4GB 500GB 7200 RPM Snow Leopard
iBert is offline   0 Reply With Quote
Old Oct 27, 2004, 11:04 AM   #3
bastardx
macrumors member
 
Join Date: May 2003
Location: Eugene, OR
Well, you may just not be opening the file correctly, if it is in binary mode and such, you have to treat things differently.
http://www.codersource.net/cpp_file_io.html
http://www.codersource.net/cpp_file_io_binary.html
Perhaps these can help you a bit, I would probably also check the permissions on the file to see if you are able read/write the file in question.
bastardx is offline   0 Reply With Quote
Old Oct 27, 2004, 12:08 PM   #4
jeremy.king
macrumors 603
 
jeremy.king's Avatar
 
Join Date: Jul 2002
Location: Fox Lake, IL
Which classes are you trying to use to open the file? Perhaps you could post your code?
jeremy.king is offline   0 Reply With Quote
Old Oct 27, 2004, 02:57 PM   #5
Earendil
Thread Starter
macrumors 68000
 
Earendil's Avatar
 
Join Date: Oct 2003
Location: Washington
I would have posted my code before, except that I hadn't been to the lab to debug it, so there were many many errors none of which are related to the issue.

The following code compiles fine under visual studio on a windows machine:

#include <fstream> //includes the library for ifstream and ofstream
#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
int s, l, next; // declares variables
ifstream in_stream; // declares a stream

in_stream.open("infile.dat"); // matches a stream with a file. Opens the file
if (in_stream.fail()) // fail safe if statement that will exit the program upon the failing of opening the file
{
cout << "Input file opening failed.\n";
exit(1);
}

in_stream >> next;
s = next;
l = next;
do
{
if( s < next)
s = next;
if( l > next)
l = next;
} while (in_stream >> next); // reads information from the file and assigns it to variables

in_stream.close(); // closes the file

cout << "The largest number in the file is " << l << ", and the smallest is " << s << "\n\n";
cout << "G'day!\n";

return 0;
}



Thanks for looking guys,
Tyler Z.
__________________
Current: MacMini 2ghz / iPod Touch 2g / iPhone 4g
Retired: Alum Powerbook 1.25ghz / 4gb iPod Mini

"ooo! They have the internet on computers now!" - Homer J. Simpson
Earendil is offline   0 Reply With Quote
Old Oct 27, 2004, 05:20 PM   #6
iBert
macrumors regular
 
Join Date: Jul 2004
Code:
if( s < next)
s = next;
if( l > next)
l = next
Are you sure this is working? I copied and pasted the code using xcode. It compiled. But, the infile.dat I'm using has one line with all the numbers separated by whitespace. Is this how the file is going to be? Another when executed, it gave me the answers backwards. Meaning smallest is the highest and highest is the smallest.
this is the file I created. just one line. Maybe since it doesn't have an end of file their could be a problem. but anyways, the program gave me smallest=8787 and highest=0. Check that if again.
Code:
2 34 8787 10 0 234 93 5
I guess the if should go the other way around.
Code:
if(next<s)
 s=next;
if(next>l)
 l=next;
this gives me, largest=8787 and smallest=0

at least for this case.
EDIT:
Code:
2 34
8787
10 0 234
93
5
Should also work.
__________________
--MBP 2.4 GHz 4GB 500GB 7200 RPM Snow Leopard

Last edited by iBert; Oct 27, 2004 at 05:22 PM.
iBert is offline   0 Reply With Quote
Old Nov 2, 2004, 05:38 PM   #7
Converted2Truth
macrumors 6502a
 
Converted2Truth's Avatar
 
Join Date: Feb 2004
Location: Hell@HighAltitude
Send a message via MSN to Converted2Truth
Also, you're .dat file MUST be located in the build folder of your project. I make this mistake all the time.
__________________
Powermac G5 Dual 1.8 (pci-x)/3.5GB/4x250GB RAID 0/ATI X800XT/Revo7.1
Join macrumors - Folding Team and help change the future.
Converted2Truth is offline   0 Reply With Quote
Old May 28, 2005, 10:34 AM   #8
mmmdreg
macrumors 65816
 
mmmdreg's Avatar
 
Join Date: Apr 2002
Location: Sydney, Australia
Send a message via AIM to mmmdreg Send a message via MSN to mmmdreg
I'm doing my own thing and the ifstream fails.
//
ifstream fooinput;
fooinput.open("path.to.foo.file");
bool failed;
failed = fooinput.fail();
cout << failed << endl;
//
That seems to fail every time. The file is a text file. I tried it with the file in the project directory, with normal path and absolute path, checked permissions, blah blah. I'm running Tiger. Could there be an issue?
mmmdreg is offline   0 Reply With Quote
Old May 28, 2005, 08:48 PM   #9
GeeYouEye
macrumors 68000
 
GeeYouEye's Avatar
 
Join Date: Dec 2001
Location: State of Denial
Send a message via AIM to GeeYouEye Send a message via Yahoo to GeeYouEye
Also consider that if you do any writing to the a .dat file in Windows, you won't be able to read it in properly without converting the numbers from little-endian to big-endian. If they're just stored as text though (as opposed to actual numbers), or you're not doing any writing, don't worry about it.
__________________
I bring order to chaos. You are in chaos Windows, you are the contradiction, a bug wishing to be an OS.
Visit Softyards Software
NEW DEFINITION OF GEEK
Like politics, free speech, computers, entertainment, and more? Join us at Wordforge.net
GeeYouEye is offline   0 Reply With Quote
Old May 30, 2005, 06:18 PM   #10
rinseout
macrumors regular
 
Join Date: Jan 2004
There is nothing wrong with Tiger.

I ran this program:
Code:
#include <iostream>
#include <fstream>

using namespace std;

int main() {
  int i(0);
  ifstream in_file;
  static const int BUF_SIZE( 80 );
  char buf[BUF_SIZE];

  in_file.open("input.dat");

  cerr << "DEBUG:  in_file.bad() = " << (in_file.bad() ? "true" : "false") << std::endl
       << "DEBUG:  in_file.fail() = " << (in_file.bad() ? "true" : "false") << std::endl;

  while(true) {
    in_file.getline( buf, BUF_SIZE  );
    if(in_file.eof() == true) break;
    else if(*buf != char(0)) cout << "Line " << (++i) << ": " << buf << std::endl;
  }

  cout << "End of file reached after " << i << " non-empty lines." << std::endl;
  in_file.close();
  return 0;
}
with this file, called input.dat:
Code:
This line has text.  The next line has numbers....
10  2348  3.14159    203.5
This is the last line.  I will terminate it with an EOL to be safe...
This was the outcome:
Code:
vanguard:~/src/ifstream rinseout$ ./test 
DEBUG:  in_file.bad() = false
DEBUG:  in_file.fail() = false
Line 1: This line has text.  The next line has numbers....
Line 2: 10  2348  3.14159    203.5
Line 3: This is the last line.  I will terminate it with an EOL to be safe...
End of file reached after 3 non-empty lines.
vanguard:~/src/ifstream rinseout$
Hope this helps... perhaps it's an issue with your delimiter. The EOL character(s) is different between DOS and OS X (or at least it used to be?). Since your files are text you can disregard the worrying about endianness.
rinseout is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
iPhone: Anyone use "SwipeNav" or "OpenTabBG" (Open tab in background)? TH55 Jailbreaks and iOS Hacks 3 Apr 21, 2014 04:22 PM
iOS7's new "Managed Open" security control trivial to bypass Omniver iOS 7 0 Sep 19, 2013 01:01 PM
Suddenly most of apps won't open "... will not open because of a problem" Doju Mac Basics and Help 1 Feb 15, 2013 03:17 PM
Name of the "file open" / "save as" dialog box app? ancilla OS X 10.8 Mountain Lion 0 Sep 29, 2012 04:59 PM
"Open in background" should be the first option when you click and hold a Safari link TH55 iPhone 18 Jun 9, 2012 05:41 AM

Forum Jump

All times are GMT -5. The time now is 04:08 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC