Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 01-08-2003, 04:55 PM   #1 (permalink)
yawningdog
Registered User
 
Join Date: Jan 2003
Posts: 2
yawningdog is on a distinguished road
file io problem

Consider the following code.

Code:
// memberio - opening Files for read and write
#include <fstream.h>
int main()
{
	char Name[80];
	char info[255];		//for user input
	int days = 1;
	cout << "Member's name: ";
	cin >> Name;

	ofstream fout(Name);	// open for writing
	fout << Name << "\n";
	fout << days << "\n";
	cout << "Enter comments: ";
	cin.ignore(1,'\n');		// eat the newline after the filename
	cin.getline(info,255);	// get the user's input
	fout << info << "\n";		// and write it to the file
	fout.close();				// close the file, ready for reopen

	ifstream fin(Name);		// reopen for reading
	cout << "Here's the contents of the file:\n";
	char ch;
	while (fin.get(ch))
		cout << ch;

	cout << "\n***End of file contents.***\n";

	fin.close();				// always pays to be tidy
return 0;
}

So far everything works like I need it to. output of the code looks like...

yawningdog
1
swell guy
***End of file contents***

What I want to do is retrieve the int days, increment it by 1 (++) and overwrite to the file. How do I do this?
I can probably parse it out with cin.peek, but there's got to be a better way.
yawningdog is offline   Reply With Quote
Old 01-08-2003, 10:04 PM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,695
redhead is on a distinguished road
Just as, when you read the file and print it to stdout, you know the second line is the days, read that, parse it as int, increment and write the incremented..

The altering of the filecontense do require you to use a temporary file, ie:

Code:
int count=1;
char my_temp_file[8];
char line[256];
strcpy(my_temp_file, "XXXXXX");
if(!mktemp(my_temp_file))
   {
        cout << "Error in creating temporary file.\n";
        return -1;
   }
ofstream fout(my_temp_file);
ifstream fin(Name);
while (fin.getline(line, 255))
     {
         if(2 == count++)
           {
              // it's the days, alter that.. 
            }
       fout << line;
      }
fout.close();
fin.close();
if(!rename(my_temp_file, Name))
     // everything went well
else
    // failed to overwrite old file..
rename() comes from stdio.h and mktemp() comes from stdlib.h, in C++ I guess these would be stdlib and stdio.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 01-09-2003, 04:04 PM   #3 (permalink)
yawningdog
Registered User
 
Join Date: Jan 2003
Posts: 2
yawningdog is on a distinguished road
Oh yeah, I'm "using namespace std;" if that matters. So it's really <fstream> and not <fstream.h>

Everything seems pretty clear until the while loop.

Code:
while (fin.getline(line, 255))
     {
         if(2 == count++)
           {
              // it's the days, alter that.. 
            }
       fout << line;
      }
Is this what parses to the second line of the file? As far as I can tell, what this does is open the Name file, look for an int which is one more than count and write it to a temp file.

In the if statement, does the "2" represent the second line of the file? I also don't quite get why the comment is in brackets.

I'm grateful for the help, and thanks for being patient.
yawningdog is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 04:07 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting