fstream problem.

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2008
Posts: 19
Reputation: imput1234 is an unknown quantity at this point 
Solved Threads: 0
imput1234 imput1234 is offline Offline
Newbie Poster

fstream problem.

 
0
  #1
Nov 25th, 2008
Hey,

I'm trying to only read the int's and the symbol - (negative) from the file.

right now my code is where m,d, and y are dates.
Everything works fine. Except I do not read the - symbol.

Any one know how to solve this?

	
fin.open(ifile.c_str());
fin >> m1 >> c >> d1 >> c >> y1;
fin >> m2 >> c >> d2 >> c >> y2;
fin >> m3 >> c >> d3 >> c >> y3;
fin.close();


For example date -10/-1/1999

will be read as 10/1/1999

for my assignment I must read the negative symbol.

Thanks
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: fstream problem.

 
0
  #2
Nov 25th, 2008
What are the data types of the variables?

With the mdy variables as ints and the c as a char, it seems to work for me - I get the negative values.

A full code sample illustrating your problem will help us help you.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 19
Reputation: imput1234 is an unknown quantity at this point 
Solved Threads: 0
imput1234 imput1234 is offline Offline
Newbie Poster

Re: fstream problem.

 
0
  #3
Nov 25th, 2008
Nevermind, weird it works now. I havent;' made any changes.

But I do have another problem.

I'm trying to make the program so that, if the user does not enter a valid file name, it will output that and clsoe the program.

This is what I have so far, and no matter what the user enters, the console will go in to a failstate. Anyone know why

Thanks.

	ifstream fin;

	cout << "Enter filename: ";
	getline(cin, ifile);	

    
	if ( !ifile.size() == 0 )
	{	
		cout << "No file found." << endl;
		system ("PAUSE");
		return 1;
	}

	fin.open(ifile.c_str());
Last edited by imput1234; Nov 25th, 2008 at 2:08 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: fstream problem.

 
0
  #4
Nov 25th, 2008
That should work. Please post the exact data file contents you're trying to read.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 19
Reputation: imput1234 is an unknown quantity at this point 
Solved Threads: 0
imput1234 imput1234 is offline Offline
Newbie Poster

Re: fstream problem.

 
0
  #5
Nov 25th, 2008
edited original post.

ifile is a string.
Last edited by imput1234; Nov 25th, 2008 at 2:09 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 19
Reputation: imput1234 is an unknown quantity at this point 
Solved Threads: 0
imput1234 imput1234 is offline Offline
Newbie Poster

Re: fstream problem.

 
0
  #6
Nov 25th, 2008
I've also tried

 
	if ( !ifile.empty() )
	{	
		cout << "No file found." << endl;
		system ("PAUSE");
		return 1;
	}
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: fstream problem.

 
0
  #7
Nov 25th, 2008
And, the test you're doing is only testing the filename the user entered, it does not test if there's actually a file or if it has any content.

(this should have come after my next reply - they got out of order somehow)
Last edited by vmanes; Nov 25th, 2008 at 2:23 am.
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 1,679
Reputation: vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold vmanes is a splendid one to behold 
Solved Threads: 193
vmanes's Avatar
vmanes vmanes is offline Offline
Posting Virtuoso

Re: fstream problem.

 
0
  #8
Nov 25th, 2008
You're asking a question that's not exactly what I think you mean.

if ( (! ifile.size( ) ) == 0 ) means, logically negate the value the size function returns, giving you a boolean true or false value, then compare that to zero, which is considered false.

If you're trying to find out if the user actually entered some string as the file name, try
  1. if( ifile.size( ) == 0 )
  2. {
  3. //error handler
  4. }
  5. else
  6. {
  7. fin.open( ifile.c_str( ) );
  8. }
Everyone's gotta believe in something. I believe I'll have another drink.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 338
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 66
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: fstream problem.

 
0
  #9
Nov 25th, 2008
If failed to open..
  1. if (fin.fail())
  2. cout<<"file does not exist";
Last edited by cikara21; Nov 25th, 2008 at 2:39 am.
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 19
Reputation: imput1234 is an unknown quantity at this point 
Solved Threads: 0
imput1234 imput1234 is offline Offline
Newbie Poster

Re: fstream problem.

 
0
  #10
Nov 25th, 2008
I've tried those. They don't work.
This is my problem.
Lets say the user enters hello.txt
but hello.txt does not exist.
How can I make the program say "Does not exist"
That is what I have been trying to do.
all the previous methods don't do that, they just make the dates really long numbers.

Hope that makes more sense.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC