| | |
fstream problem.
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2008
Posts: 19
Reputation:
Solved Threads: 0
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?
For example date -10/-1/1999
will be read as 10/1/1999
for my assignment I must read the negative symbol.
Thanks
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
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.
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Oct 2008
Posts: 19
Reputation:
Solved Threads: 0
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.
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.
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
•
•
Join Date: Oct 2008
Posts: 19
Reputation:
Solved Threads: 0
I've also tried
if ( !ifile.empty() )
{
cout << "No file found." << endl;
system ("PAUSE");
return 1;
} 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)
(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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
You're asking a question that's not exactly what I think you mean.
If you're trying to find out if the user actually entered some string as the file name, try
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
C++ Syntax (Toggle Plain Text)
if( ifile.size( ) == 0 ) { //error handler } else { fin.open( ifile.c_str( ) ); }
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.
~~~~~~~~~~~~~~~~~~
Looking for an exciting graduate degree? Robotics and Intelligent Autonomous Systems (RIAS) at SDSM&T See the program brochure here.
If failed to open..
c++ Syntax (Toggle Plain Text)
if (fin.fail()) cout<<"file does not exist";
Last edited by cikara21; Nov 25th, 2008 at 2:39 am.
•
•
Join Date: Oct 2008
Posts: 19
Reputation:
Solved Threads: 0
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.
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.
![]() |
Similar Threads
- fstream Tutorial (C++)
- read to end of line problem (C)
- fstream.h + winsock2.h doesn't work? (C++)
- need help with programing with fstream to find area of triangle (C++)
- File processing problem (C++)
- Stack Queue Fstream (C++)
- Mode 13 graphics,Problem with io streams and new operator (C++)
- ** Need Help ** in a small C++ problem (C++)
Other Threads in the C++ Forum
- Previous Thread: Need help with Tic Tac Toe
- Next Thread: SDL Performance
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






