473,383 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Simple files.

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;
}

Jul 23 '05 #1
14 1284
enki wrote:
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::ofstream ofile("data.txt");
for(int index = 0 ; index < argc ; ++index)
ofile << "argv[" << index << "] is: " << argv[index] << std::endl;

....

Jul 23 '05 #2
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.

Jul 23 '05 #3
Yes me again with fstream is there a close for the file? I

Jul 23 '05 #4
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");
}

Jul 23 '05 #5

enki wrote:
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

Jul 23 '05 #6
enki wrote:
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.

Jul 23 '05 #7
enki schrieb:
#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<----------
Jul 23 '05 #8


enki schrieb:
#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.

Jul 23 '05 #9
Michael Etscheid wrote:

enki schrieb:
#include "myvec.cpp"


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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #10
enki wrote:

enki schrieb:
#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.


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.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #11
Karl Heinz Buchegger wrote:
Michael Etscheid wrote:
enki schrieb:
#include "myvec.cpp"


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.

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.
Jul 23 '05 #12
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.

Jul 23 '05 #13
Thanks, I have got the program to finally work.

Jul 23 '05 #14
In article <hU*******************@newssvr13.news.prodigy.com> ,
Kurt Stutsman <ks*******@NOSPAM.sbcglobal.net> wrote:
Karl Heinz Buchegger wrote:
Michael Etscheid wrote:
enki schrieb:
#include "myvec.cpp"

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.

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.
--
Greg Comeau / Comeau for the Mac? Stay tuned.
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Jul 23 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Brian J. Ackermann | last post by:
Hi everybody, I'm new to c++, and I'm working on this simple little application, which ought to compile just fine, but I'm missing something. The program has 4 file, 2 headers, and 2 cpp files....
8
by: Dan | last post by:
Using XML::Simple in perl is extreemly slow to parse big XML files (can be up to 250M, taking ~1h). How can I increase my performance / reduce my memory usage? Is SAX the way forward?
3
by: Ryno Rijnsburger | last post by:
I am busy packaging our product as a standard Setup project in VS.NET that uses a bunch of merge modules (basically, a merge module for every key infrastructure component in our system). Part...
44
by: Neil Cerutti | last post by:
In Rob Pike's style guide he urges the following: Simple rule: include files should never include include files. If instead they state (in comments or implicitly) what files they need...
10
by: serge calderara | last post by:
Dear all, I need to build a web application which will contains articles (long or short) I was wondering on what is the correct way to retrive those article on web page. In orther words, when...
4
by: bob lambert | last post by:
Help I am trying to deploy to another pc a vb.net std 2002 windows form application. I am confused. I created a project - windows form I built form, compiled and debugged. I created a...
6
by: Jim M | last post by:
I've been distributing a fairly mature, very specific MS Access application to end users in small offices of colleges for several years now. This is a part-time venture and low volume operation-...
10
true911m
by: true911m | last post by:
This is a simple walkthrough to get PyInstaller up and running. I decided to give PI a try, because it claims to be more selective about what it bundles into its executable files by default, and...
2
by: olympus_mons | last post by:
Hi, I'm just discovering the power of xsd.exe, so maybe I'm doing something wrong. schema files describing requests and responses. So there is an extra xsd file for each response and each...
0
by: Andy Dingley | last post by:
I have a problem involving lots of simple text files (Java properties files), for which I'm building Python tools to manage their contents. I'm also writing lots of Python modules and using...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.