Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old December 26th, 2004, 11:38 PM
ck ck is offline
Authorized User
 
Join Date: Dec 2004
Location: , , .
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to Read,Write, & Save (File Processing)

hi,
 i'm a beginner in C/C++, so do any1 know how to read, write, & save...about File Processing in C, & C++ ????
 thanks
hear from u soon...

best regards,


__________________
ck.
Reply With Quote
  #2 (permalink)  
Old March 23rd, 2005, 06:40 PM
Registered User
 
Join Date: Mar 2005
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

to open a file you would do this.

#include <iostream>
#include <fstream>

int main(){

ifstream fin;
ofstream fout;
fin("file.whatever"): /* to open for input */
fin.close();
fout("file.whatever"); /* open for output */
fout << "line written to file"";
fout.close();
return 0;
}



Reply With Quote
  #3 (permalink)  
Old April 18th, 2005, 07:28 AM
Authorized User
 
Join Date: Mar 2005
Location: Zagreb, , Croatia.
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

/************************************************** ************
Try to learn from this, these are my 2nd excersizes, from
the subject "Object oriented programming"!
The goal was to implement "file.cpp" and "file.h" files for
the "main.cpp" program to work properly
************************************************** ************/
//main.cpp
#include <iostream>
#include <string>
bool create_file(const std::string& filename){
   file f;

   if(!f.open(filename, false))
      return false;

   while(true){
     std::string s;
     std::cin >> s;
     if("end" == s)
        break;
     if(!f.write_line(s))
        break;
     }
    f.close();
    return true;
}
void display_file(const std::string& filename){
     file f;

     if(!f.open(filename))
         return;

     std::string s;
     while(f.read_line(s))
         std::cout << s;
     f.close();
}
int main(){
     std::string filename;
     std::cin >> filename;
     filename += ".txt";

     if(create_file(filename))
          display_file(filename);

     return 0; //success
}
// file.h
class file{
     FILE* fp; //default private:
public:
     bool open(const std::string& name, bool read=true);
     void close();
     bool read_line(std::string&) const;
     bool write_line(const std::string&);
};
// file.cpp
bool file::open(const std::string& name, bool read){
     const char* mode = read ? "rt" : "wt";
     return (fp = fopen(name.c_str(), mode)) != NULL;
}
void file::close(){
     fclose(fp);
}
bool file::write_line(const std::string& line){
     bool ret = fputs(line.c_str(), fp) != EOF;
     if(ret) fputs("\n", fp);
     return ret;
}
bool file::read_line(std::string& line) const {
     char s[256];
     bool ret = fgets(s, 256, fp) != NULL;
     if(ret) line = s;
     return ret;
}
/************************************************** ************
I hope there are no mistakes in code, sorry if you find them,
because I didn't have time to run it through compiler!

greetings: icopec
from: Croatia
************************************************** ************/
Reply With Quote
Reply


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 Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Save file as Read-Only Neal Excel VBA 2 May 17th, 2007 03:35 AM
Text file importing & data processing jarpola Excel VBA 0 January 16th, 2007 09:27 AM
read & write file C# minhpx General .NET 1 February 21st, 2005 03:14 PM
How to Read,Write, & Save (File Processing) ck Visual C++ 1 January 23rd, 2005 01:16 PM
Logging File Access Read & Write ntbluez VBScript 1 November 12th, 2003 11:43 PM



All times are GMT -4. The time now is 07:41 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.