5 Replies - 831 Views - Last Post: 30 December 2008 - 08:33 PM Rate Topic: -----

#1 boshra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-December 08

Files in C++

Post icon  Posted 29 December 2008 - 10:13 PM

#include <iostream>
using namespace std;
#include <stdio.h>
int main()
{
	FILE *fp;
	fp=fopen("C:\Documents and Settings\Boshra\Desktop","rt");
	//fscanf(fp,"%d %d",&a,&b);
	char c;
	if(fp==NULL)
	{
		exit(1);
	}
	while(!feof(fp))
	{
		cout<<fgetc(fp)<<endl;
		cout<<fp<<endl;
		fp++;
	}
	fclose(fp);
	return 0;
}


I don't know how to work with files in C++ and I just need to khow. :crazy: Would you please help me? If possible, send me a sample code using files in order for me to know the syntax. And also I want to know, for example, how to read a data from a file, do a process on that data and then save it.
thanks for helping me
Boshra

Is This A Good Question/Topic? 0
  • +

Replies To: Files in C++

#2 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5182
  • View blog
  • Posts: 26,890
  • Joined: 10-May 07

Re: Files in C++

Posted 29 December 2008 - 10:18 PM

Can you define work with files, as I'm not sure what you are trying to do with these files.
Was This Post Helpful? 0
  • +
  • -

#3 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 278
  • View blog
  • Posts: 1,783
  • Joined: 20-September 08

Re: Files in C++

Posted 29 December 2008 - 11:26 PM

This may help get you started ...

Shalom,
David
http://developers-he...index.php/topic,46.0.html

http://www.cplusplus...rial/files.html

// demo C++ file io operations
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;

int main () 
{
    // write 10 lines to a file ...
    ofstream fout("somelines.txt");
    for(int i=0; i<10; ++i)
        fout << "Number " << i+1 <<" is in file.\n"; 
    fout.close();
    
    vector <string> v;
    string s;
    // read file into a vector of strings ...
    ifstream fin("somelines.txt");
    while(getline(fin, s))
        v.push_back(s);
    fin.close();
    
    //show v and remove "Number " and "is " from each string
    fout.open("somelines.txt");
    for(unsigned i=0; i<v.size(); ++i)
    {
        cout << v[i] << endl;
        unsigned pos = v[i].find("is");
        fout <<  v[i].erase(pos,3).substr(7) << endl;
    }
    fout.close();

    fin.clear(); // reset flags ... (eof)
    // read processed file and show ...
    fin.open("somelines.txt");
    while(getline(fin, s))
        cout << s << endl;
    fin.close();
    
    cout << "\nPress 'Enter' to continue ... " << flush;
    cin.get();
    return 0;
}

Was This Post Helpful? 0
  • +
  • -

#4 boshra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 04-December 08

Re: Files in C++

Posted 30 December 2008 - 11:38 AM

View PostDavid W, on 29 Dec, 2008 - 10:26 PM, said:

This may help get you started ...

Shalom,
David
http://developers-he...index.php/topic,46.0.html

http://www.cplusplus...rial/files.html

// demo C++ file io operations
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
using namespace std;

int main () 
{
    // write 10 lines to a file ...
    ofstream fout("somelines.txt");
    for(int i=0; i<10; ++i)
        fout << "Number " << i+1 <<" is in file.\n"; 
    fout.close();
    
    vector <string> v;
    string s;
    // read file into a vector of strings ...
    ifstream fin("somelines.txt");
    while(getline(fin, s))
        v.push_back(s);
    fin.close();
    
    //show v and remove "Number " and "is " from each string
    fout.open("somelines.txt");
    for(unsigned i=0; i<v.size(); ++i)
    {
        cout << v[i] << endl;
        unsigned pos = v[i].find("is");
        fout <<  v[i].erase(pos,3).substr(7) << endl;
    }
    fout.close();

    fin.clear(); // reset flags ... (eof)
    // read processed file and show ...
    fin.open("somelines.txt");
    while(getline(fin, s))
        cout << s << endl;
    fin.close();
    
    cout << "\nPress 'Enter' to continue ... " << flush;
    cin.get();
    return 0;
}

thank you very much for helping me
Was This Post Helpful? 0
  • +
  • -

#5 Hyper  Icon User is offline

  • Banned

Reputation: 108
  • View blog
  • Posts: 2,129
  • Joined: 15-October 08

Re: Files in C++

Posted 30 December 2008 - 01:48 PM

View Postboshra, on 30 Dec, 2008 - 10:38 AM, said:

thank you very much for helping me


Don't forget to click the little green button that sayss "This was a helpful post!" (for him, not me!)
Was This Post Helpful? 0
  • +
  • -

#6 UG Cyber  Icon User is offline

  • D.I.C Addict

Reputation: 32
  • View blog
  • Posts: 546
  • Joined: 24-July 08

Re: Files in C++

Posted 30 December 2008 - 08:33 PM

View PostHyper, on 30 Dec, 2008 - 12:48 PM, said:

View Postboshra, on 30 Dec, 2008 - 10:38 AM, said:

thank you very much for helping me


Don't forget to click the little green button that sayss "This was a helpful post!" (for him, not me!)


lmao

Basicly once you define your handle
ifstream fin; //to read
ofstream fout; // to write


and open the file
fin.open("InputFile.txt");
fout.open("OutputFile.txt");



you have nothing else to worrie about. Just treat it like "cin" and "cout" It works the same way.
//some of the functions / syntax avalible
char fin.get(char ch);
char* fin.getline(char* Buffer, int SizeOfBuffer, char DelimChar);
fin >> Buffer;

fout.write(Buffer, sizeof(Buffer));
fout << Buffer;

//Like i said, SOME of the avalible functions



But don't forget to close the file when your done
fin.close();
fout.close();
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1