944,057 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3259
  • C++ RSS
Jun 4th, 2007
0

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

Expand Post »
**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.

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 7
Unverified User
Matt Tacular is offline Offline
187 posts
since Jun 2006
Jun 4th, 2007
0

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

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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jun 4th, 2007
0

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

Matt, don't use deprecated headers and system("pause");. They call for bad programming.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006
Jun 4th, 2007
0

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

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...
Reputation Points: 10
Solved Threads: 7
Unverified User
Matt Tacular is offline Offline
187 posts
since Jun 2006
Jun 4th, 2007
0

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

You might have to add the line
C++ Syntax (Toggle Plain Text)
  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.
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Jun 4th, 2007
0

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

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++"
Reputation Points: 10
Solved Threads: 7
Unverified User
Matt Tacular is offline Offline
187 posts
since Jun 2006
Jun 4th, 2007
0

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

> 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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Help
Next Thread in C++ Forum Timeline: Help with C++ code using structs and arrays





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC