473,408 Members | 1,763 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Can't get Sams example to work (re-opening file)

Hello, I hope it's OK to post beginner's questions to this group? If not,
please direct me to the right one.

I'm having trouble with the following program, which is taken from SAMS
Teach Yourself C++ in 21 Days. The idea is that an existing text file is
displayed to the user, who is prompted for text to append to the file. The
file with its new contents should then be displayed to the screen.

Everything works except the fin.open(fileName) statement, instead the error
branch is taken. Hence the contents of the file are not re-displayed to the
screen (true even if I remove the return(1) as you'd expect).

If I remove the block of code that displays the original contents then I
don't get a problem (I've marked this block with //START and //END).
Anyway, here's the code. I've removed the bit that does the append, since I
get the same results with or without it:

//Sams 17.17
#include <fstream>
#include <iostream>
using namespace std ;

main()
{
char fileName[50] ;
char buffer[255] ;
cout << "Please re-enter the file name : " ;
cin >> fileName ;
ifstream fin(fileName) ;
//START
if (fin) //test for existence
{
cout << "Current file contents : \n" ;
char ch ;
while (fin.get(ch))
cout << ch ;
cout << "\n* End of file * \n" ;
}
//END
fin.close() ;

fin.open(fileName) ; //This fails!
if (fin.fail()) //Same as if(!fin)
{
cout << "\nUnable to open " << fileName << " for reading.\n"
;
return(1) ;
}
cout << "\nHere's the contents of the file : \n" ;
char ch ;
while (fin.get(ch))
cout << ch ;
cout << "\n* End of file *\n" ;
fin.close() ;
return(0) ;
}

I'm using the g++ version 3.2 compiler under Linux 2.4-18. Thanks for
pointing out any silly mistakes I've made!

Arfur
Jul 22 '05 #1
2 1525
You may want to have a look at:
http://gcc.gnu.org/ml/gcc-bugs/2002-07/msg00814.html

The example code looks very similar too.
I'm using the g++ version 3.2 compiler under Linux 2.4-18. Thanks for
pointing out any silly mistakes I've made!

Arfur


Jul 22 '05 #2
Steven Green wrote:
You may want to have a look at:
http://gcc.gnu.org/ml/gcc-bugs/2002-07/msg00814.html

The example code looks very similar too.


Thanks for the link, I'll get a copy of gcc 2.95.

Arfur

Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: chuck amadi | last post by:
By the way list is there a better way than using the readlines() to > > >parse the mail data into a file , because Im using > > >email.message_from_file it returns > > >all the data i.e reads one...
6
by: ABC | last post by:
I write the code as: <asp:LinkButton ID="LinkButton2" Text='<%# Eval("Title") %>' PostBackUrl='file://<%# Eval("path") %>' runat="server"></asp:LinkButton> But It don't work to download or open...
0
by: sapo | last post by:
This is my code: def list(self): tree = self.principal.get_widget("list") self.list = gtk.ListStore(bool,str) self.options = renderer1 = gtk.CellRendererToggle()...
1
by: abureach | last post by:
Hi, I cant able to direcly open the file in IE(force download) the follwing code is not working in IE. Please help me my coding is if(ini_get('zlib.output_compression'))
3
by: flavourofbru | last post by:
Hi all, I am reading a text file and then computing some values. Then I am closing that file. Later on if i reopened that file in the same program I am unable to read that file. I found that the...
4
daniel aristidou
by: daniel aristidou | last post by:
Hi i just downloaded and installed the newest version of java. However i can seem to find or open the files it installed can anyone help me? Or give me a link to download again, maybe the...
4
by: rzismanx | last post by:
Hello I'm trying to reopen a certain file but only if it's updated: The problem is when i try to open a file (after it was changed and saved by me)for reading second time, i get nothing, like the...
3
by: =?Utf-8?B?Um9nZXIgTWFydGlu?= | last post by:
Note: My apologies for repeating this post from last week, but my nospam alias and profile account were incorrect. I think I have fixed this, so hopefully this post will trigger MS into a response...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.