Simple files.

E

enki

I am trying to figure out basic reading and writing of files, I realy
havn't seen good examples. I wrote a simple program to write a file.
I would like to know where to get a good lesson on using files.

#include <cstdlib>
#include <iostream>
#include <stdio.h>


using namespace std;

int main(int argc, char *argv[], char *temp[])
{
int lp = 0 ;

for(lp = 0 ; lp != argc; lp ++){
cout<<argv[lp]<<"\n";
}

FILE *ptr = fopen("data.txt", "w");
for(lp = 0 ; lp != argc; lp ++){
//what goes here to write to <<argv[lp]<<"\n";
}

fclose(ptr);

system("PAUSE");
return EXIT_SUCCESS;
}
 
P

Phil Staite

enki said:
I am trying to figure out basic reading and writing of files, I realy
havn't seen good examples. I wrote a simple program to write a file.
I would like to know where to get a good lesson on using files.

#include <cstdlib>
#include <iostream>
#include <stdio.h>


using namespace std;

int main(int argc, char *argv[], char *temp[])
{
int lp = 0 ;

for(lp = 0 ; lp != argc; lp ++){
cout<<argv[lp]<<"\n";
}

FILE *ptr = fopen("data.txt", "w");
for(lp = 0 ; lp != argc; lp ++){
//what goes here to write to <<argv[lp]<<"\n";
}

fclose(ptr);

system("PAUSE");
return EXIT_SUCCESS;
}

How about something like this instead? (using C++ iostreams)

#include<fstream>

....

std::eek:fstream ofile("data.txt");
for(int index = 0 ; index < argc ; ++index)
ofile << "argv[" << index << "] is: " << argv[index] << std::endl;

....
 
R

Rolf Magnus

enki said:
I am trying to figure out basic reading and writing of files, I realy
havn't seen good examples. I wrote a simple program to write a file.
I would like to know where to get a good lesson on using files.

#include <cstdlib>
#include <iostream>
#include <stdio.h>


using namespace std;

int main(int argc, char *argv[], char *temp[])

In a portable program, main either takes two arguments or none.
{
int lp = 0 ;

for(lp = 0 ; lp != argc; lp ++){
cout<<argv[lp]<<"\n";
}

FILE *ptr = fopen("data.txt", "w");
for(lp = 0 ; lp != argc; lp ++){
//what goes here to write to <<argv[lp]<<"\n";

fputs(ptr, argv[lp]);
}

fclose(ptr);

system("PAUSE");
return EXIT_SUCCESS;
}

But I suggest using a C++ fstream instead.
 
E

enki

Ok, I was trying to learn files and my material is not that good. As I
thought about it I was going to ask which is better stdio.h or
iostream.

I will use this example and see how it goes.
 
E

enki

OK now how to read it, this is what I have:

#include <iostream>
#include <string>
#include <fstream>

#include "myvec.cpp"

using namespace std;

int main(){

V<string>box;

ifstream FILE("data.txt");
if (! FILE.is_open())
{ cout << "Error opening file"; exit (1); }

while(!FILE){
//what goes here?
}
box.showall();

system("pause");
}
 
D

Default User

enki said:
Ok, I was trying to learn files and my material is not that good. As I
thought about it I was going to ask which is better stdio.h or
iostream.


Please quote a relevant portion of the previous message when replying.
To do so from Google, click "show options" and use the Reply in the
expanded header.



Brian
 
R

Rolf Magnus

enki said:
Yes me again with fstream is there a close for the file? I

Yes, but usually, you don't need it. When the stream object goes out of
scope, it is automatically closed. You can explicitly close it with its
(surprise!) close() member function.
 
M

Michael Etscheid

enki said:
#include "myvec.cpp"

You cannot include a cpp-file. It must be an h-file. And what is myvec?
I write two examples with a standard vector:

----------8<----------8<----------
// first example

#include <fstream>
#include <vector>
#include <string>
using namespace std;

int main()
{
ifstream in("filename.txt");
vector<string> vec;

while(in)
{
string buffer;
getline(buffer);
vec.push_back(buffer);
}
}
----------8<----------8<----------

And that's the way I normally do it.

----------8<----------8<----------
// second example

#include <fstream>
#include <iterator>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;

int main()
{
ifstream in("filename.txt");
istream_iterator<string> iin(in), eof;
vector<string> vec;
copy(iin, eof, back_inserter(vec);
}
----------8<----------8<----------
 
E

enki

enki said:
#include "myvec.cpp"

You cannot include a cpp-file. It must be an h-file. And what is myvec?
I write two examples with a standard vector:

Ich will etwas fragen...

I use the GC++ compiler from bloodshed and when I create a project with
a cpp and hpp I put a hpp in my code but the only way I can get the
module to run is to call the cpp part. I call the cpp and that file
calls the header.

myvec is my personal vector and I like using it although I wouldn't
recommend ot for general use.
 
K

Karl Heinz Buchegger

Michael said:
You cannot include a cpp-file. It must be an h-file.

Says who?

I can #include any file I want. What the compiler makes
out of it is a different question. But I can #include it.


There seems to be a misconception on your side, what #include
does.
All that #include does is replace the line
#include "myvec.cpp"
with the content of that file.
Nothing more, nothing less. You could do the very
same thing with your ordinary text editor, if you want
to. In fact, one can imagine the preprocessor as beeing
nothing more then a glorified text editor, which is
operated by keywords in the processed text itself.
 
K

Karl Heinz Buchegger

enki said:
You cannot include a cpp-file. It must be an h-file. And what is myvec?
I write two examples with a standard vector:

Ich will etwas fragen...

I use the GC++ compiler from bloodshed and when I create a project with
a cpp and hpp I put a hpp in my code but the only way I can get the
module to run is to call the cpp part.

You don't 'call' a cpp part. You call functions.
What you do is, you *include* files. And this is nothing more
then when you just entered the text from that file instead of
the #include with your text editor.

What you need to do is:
* compile one cpp file
* compile the second cpp file
* now link those files to form the final executable.

Creating an executable using the C++ language usually is a 2
step process

* compile all the individal cpp files, which results in their
object modules. One object module for each cpp file
* now link all those object modules to form the final executable.

You can imagine this whole process as:
step one prepares all the individual parts
step two glues them together

How to do this specifically, depends on your development environment.
 
K

Kurt Stutsman

Karl said:
Says who?

I can #include any file I want. What the compiler makes
out of it is a different question. But I can #include it.
It should be noted that some compilers do complain if the file ends in ".c" or
".cpp" for whatever reason. I've seen it somewhere but don't remember where.
 
E

enki

I can #include any file I want. What the compiler makes
out of it is a different question. But I can #include it.

This is just what works.
 
G

Greg Comeau

It should be noted that some compilers do complain if the file
ends in ".c" or ".cpp" for whatever reason. I've seen it somewhere
but don't remember where.

That could be a reasonable option in some situation, but just not
a required one.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,055
Members
47,659
Latest member
salragu

Latest Threads

Top