Doesn't open for file input successfully.... why?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2006
Posts: 187
Reputation: Matt Tacular is an unknown quantity at this point 
Solved Threads: 7
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Unverified User

Doesn't open for file input successfully.... why?

 
0
  #1
Jun 4th, 2007
**I know the code looks big, but it's a very small isolated section at the bottom (starting at line 164) that's giving me the problem.**

When this program is run, when I try to open the file I just appended some text to, it doesn't open... can anyone spot why? Thanks....

-Matt

p.s. Yes this is a program I made after following a tutorial, incase you're wondering.

  1. //#include <iostream.h> <--No need for this because fstream includes it
  2. #include <iomanip.h>
  3. #include <fstream.h>
  4. int main()
  5. {
  6. char a, b, c, d;
  7.  
  8. char ch;
  9.  
  10. //four character arrays
  11. char stringOne[256];
  12. char stringTwo[256];
  13. char stringThree[256];
  14. char stringFour[] = "This is a string of characters!";
  15.  
  16. cout << "Enter 3 letters: ";
  17.  
  18. cin.get(a).get(b).get(c).get(d);
  19.  
  20. cout << "A: " << a << endl
  21. << "B: " << b << endl
  22. << "C: " << c << endl
  23. << "D: " << d << endl; //for the terminating character thing
  24.  
  25. cout << "Enter string one: ";
  26. cin.get(stringOne,256);
  27. cout << "stringOne: " << stringOne << endl << endl;
  28.  
  29. cout << "Enter string two: ";
  30. cin >> stringTwo;
  31. cout << "stringTwo: " << stringTwo << endl << endl;
  32.  
  33. /* If the user enters something with a space and then more after the space up in string two, then the remainder after
  34.   the white space in string two will be left to linger in the input buffer and then will be immediatly put into string
  35.   three below, you won't even get a chance to enter anything in string three */
  36.  
  37. cout << "Enter string three: ";
  38. cin.getline(stringThree,256);
  39. cout << "stringThree: " << stringThree << endl << endl;
  40.  
  41. cout << "Try again..." << endl << "Enter string two: ";
  42. cin >> stringTwo;
  43. cout << "stringTwo: " << stringTwo << endl << endl;
  44.  
  45. cin.ignore(255,'\n');
  46.  
  47. cout << "And now you actually get to enter string three: ";
  48. cin.getline(stringThree,256);
  49. cout << "stringThree: " << stringThree << endl << endl;
  50.  
  51. /* The following section of code demonstrates the put() function */
  52.  
  53. cout.put('H').put('e').put('l').put('l').put('o').put('\n');
  54.  
  55. /* peek() and putback() */
  56.  
  57. cout << "Enter a phrase: ";
  58. while ( cin.get(ch) )
  59. {
  60. if (ch == 'e')
  61. {
  62. cin.putback('E');
  63. }
  64. else
  65. {
  66. cout << ch;
  67. }
  68. while (cin.peek() == ' ')
  69. {
  70. cin.ignore(1,' ');
  71. }
  72. if (ch == '\n')
  73. {
  74. break;
  75. }
  76. }
  77.  
  78. /* The write() function */
  79.  
  80. int fullLength = strlen(stringFour);
  81. int tooShort = fullLength -4;
  82. int tooLong = fullLength + 6;
  83. cout << "using write(), with a parameter that's correct:" << endl;
  84. cout.write(stringFour,fullLength) << endl << endl;
  85. cout << "using write(), with a parameter that's too short:" << endl;
  86. cout.write(stringFour,tooShort) << endl << endl;
  87. cout << "using write(), with a parameter that's too long:" << endl;
  88. cout.write(stringFour,tooLong) << endl << endl;
  89. /* The width() function */
  90.  
  91. cout << "Width function:";
  92. cout.width(25);
  93. cout << "The paramter was 25" << endl << endl;
  94.  
  95. cout << "Width function again:";
  96. cout.width(25);
  97. cout << "Paramter also 25\n";
  98. cout << "Width only goes to the NEXT output." << endl << endl;
  99.  
  100. cout << "Width function AGAIN: ";
  101. cout.width(4);
  102. cout << "Paramter was 4, way too short, but that's the same as just enough." << endl << endl;
  103.  
  104. /* The setf() function */
  105.  
  106. const int number = 234;
  107.  
  108. cout << "The number is: " << number << endl;
  109. cout << "That number, " << number << ", in hex is " << hex << number << endl;
  110.  
  111. cout << "The hex number, with it's base shown: ";
  112. cout.setf(ios::showbase);
  113. cout << hex << number << endl;
  114.  
  115. cout << "The number, formated with 10 width: ";
  116. cout.width(10);
  117. cout << hex << number << endl;
  118.  
  119. cout << "Number with alignment 'left': ";
  120. cout.width(10);
  121. cout.setf(ios::left);
  122. cout << hex << number << endl;
  123.  
  124. cout << "Number with alignment 'internal': ";
  125. cout.width(10);
  126. cout.setf(ios::internal);
  127. cout << hex << number << endl;
  128.  
  129. cout << "Using concatenated setw() to set width to 10:"
  130. << setw(10) << hex << number << endl << endl;
  131. /* File reading and writing */
  132.  
  133. char fileName[80];
  134. char buffer[255]; //for user input
  135.  
  136. cout << "Enter filename: ";
  137. cin >> fileName;
  138.  
  139. ofstream fout(fileName); // open for writing
  140. fout << "This was written to file in the background..." << endl;
  141. cout << "Enter text for file writing: ";
  142. cin.ignore(1,'\n'); // eat the newline after the file name
  143. cin.getline(buffer,255); // get the user's input
  144. fout << buffer << "\n"; // and write it to the file
  145. fout.close(); // close the file, ready for reopen
  146. ifstream fin(fileName);
  147. cout << "Here is what's currently in the file: " << endl;
  148.  
  149. char ch2;
  150.  
  151. while(fin.get(ch2))
  152. {
  153. cout << ch2;
  154. }
  155. cout << " -=-=-=-=-= End of file contents =-=-=-=-=- " << endl << endl;
  156.  
  157. fin.close(); //just being neat and tidy
  158.  
  159. /* file writing in append mode */
  160.  
  161. cout << "Opening file in append mode: " << endl;
  162. fout.open(fileName,ios::app); //reasign existing fout object
  163.  
  164. if(!fout) //if unable to open file for output.... same as if(fout.fail())
  165. {
  166. cout << "Was unable to open file for output..." << endl;
  167. }
  168.  
  169. cout << "Enter more text for the previous file: " << endl;
  170. cin.ignore(1,'\n');
  171. cin.getline(buffer,255);
  172. fout << buffer << "\n";
  173. fout.close();
  174. fin.open(fileName); //reasign existing fin object
  175. if(!fin) //if unable to open for file input
  176. {
  177. cout << "Unable to open for file input..." << endl;
  178. }
  179.  
  180. cout << "Here's the contents of the file: " << endl;
  181. while(fin.get(ch2))
  182. {
  183. cout << ch2;
  184. }
  185. cout << " -=-=-=-=-= End of file contents =-=-=-=-=- " << endl << endl;
  186. fin.close();
  187.  
  188. /* The fill() function */
  189.  
  190. cout << "Using fill() to add astericks wherever whitespace is added from width(): " << endl;
  191. cout << "Start --> ";
  192. cout.width(25);
  193. cout.fill('*');
  194. cout << " <-- End" << endl << endl;
  195.  
  196. system("PAUSE");
  197. return 0;
  198. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,567
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 706
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Doesn't open for file input successfully.... why?

 
0
  #2
Jun 4th, 2007
Your problem is that opening and closing files doesn't change the stream state. When you read fin to end-of-file, closing the stream doesn't change the eofbit. You need to clear the stream state as well as close the file if you plan on using fin to read from a file again:
  1. fin.clear();
  2. fin.close();
You can also avoid mysterious errors like this by using is_open instead of the boolean conversion to check the stream after opening it:
  1. if(!fin.is_open()) //if unable to open for file input
  2. {
  3. cout << "Unable to open for file input..." << endl;
  4. }
This guarantees that you're testing if the file is open, not if it's in a good state altogether.
Last edited by Narue; Jun 4th, 2007 at 11:51 am.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Doesn't open for file input successfully.... why?

 
0
  #3
Jun 4th, 2007
Matt, don't use deprecated headers and system("pause");. They call for bad programming.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 187
Reputation: Matt Tacular is an unknown quantity at this point 
Solved Threads: 7
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Unverified User

Re: Doesn't open for file input successfully.... why?

 
0
  #4
Jun 4th, 2007
Thanks a lot, that helped and my program works now, although I don't understand what I should be using instead of the iostream.h i tried just iostream and it said cout was undefined...
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,947
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 914
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Doesn't open for file input successfully.... why?

 
0
  #5
Jun 4th, 2007
You might have to add the line
  1. using namespace std;
or add a std:: before every cout and cin (etc).

What are you using for a compiler? Sounds like the old Borland thingy!
Last edited by vegaseat; Jun 4th, 2007 at 1:44 pm.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 187
Reputation: Matt Tacular is an unknown quantity at this point 
Solved Threads: 7
Matt Tacular's Avatar
Matt Tacular Matt Tacular is offline Offline
Unverified User

Re: Doesn't open for file input successfully.... why?

 
0
  #6
Jun 4th, 2007
I tried that and it still said the same thing. But is any header with the .h a deprecated one? or just the iostream one? Because i tried it with the fstream one just now. And I'm using the latest version of "Bloodshed Dev-C++"
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Doesn't open for file input successfully.... why?

 
0
  #7
Jun 4th, 2007
> i tried just iostream and it said cout was undefined...
Well you also need to learn about namespaces as well.

A crude "fix" is to convert
#include <iostream.h>

to
#include <iostream>
using namespace std;


Better use would be only list the things which interest you.
#include <iostream>
using std::cout;
using std::cin;


And even better, avoid "using" altogether and be totally explicit in what type of cout you mean.
#include <iostream>
// then later in the code proper
std::cout << "Enter 3 letters: ";

The more precise you can be, the fewer problems you'll have when it comes to using code which has several namespaces.

> But is any header with the .h a deprecated one? or just the iostream one?
All the ones declared by ANSI have lost the .h suffix, and now make use of the "std" namespace.
The latest dev-c++ should be fine.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC