p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   C++ Programming (http://p2p.wrox.com/forumdisplay.php?f=89)
-   -   How to Read,Write, & Save (File Processing) (http://p2p.wrox.com/showthread.php?t=23094)

ck December 27th, 2004 12:38 AM

How to Read,Write, & Save (File Processing)
 i'm a beginner in C/C++, so do any1 know how to read, write, & save...about File Processing in C, & C++ ????
hear from u soon...

best regards,

Silent_Coder March 23rd, 2005 07:40 PM

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 */
fout("file.whatever"); /* open for output */
fout << "line written to file"";
return 0;

icopec April 18th, 2005 07:28 AM

/************************************************** ************
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
************************************************** ************/
#include <iostream>
#include <string>
bool create_file(const std::string& filename){
   file f;

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

     std::string s;
     std::cin >> s;
     if("end" == s)
    return true;
void display_file(const std::string& filename){
     file f;


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


     return 0; //success
// file.h
class file{
     FILE* fp; //default private:
     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(){
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
************************************************** ************/

All times are GMT -4. The time now is 07:18 PM.

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