473,386 Members | 1,715 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,386 software developers and data experts.

read/write file contents

Is this acceptable ? It seems too easy and too simple...

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

struct Person{
char name[25] ;
int age ;
char ssn[9] ;
} person[5] ;

int main(int argc, char *argv[])
{
std::ifstream input ;
std::ofstream output ;
std::string name ;
int age ;

input.open("file.txt", std::ios::in) ;
output.open("fileout.txt", std::ios::out) ;

if(!input){
std::cout << "File not found" << std::endl ;
exit(1) ;
}

for(int i = 0; i < 5; i++)
input >> person[i].name >> person[i].age >> person[i].ssn ;

for(int k = 0; k < 5; k++)
std::cout << person[k].name << " " << person[k].age << " " <<
person[k].ssn << std::endl ;

// write some text to a file
name = "Me" ;
age = 105 ;

output << name << " " << age << std::endl ;

return 0 ;
}
Here's the file contents exactly as in file.txt

bob
44
123-23-4532
fred
61
434-98-7654
hilbert
23
772-81-7108
peter
12
132-64-0132
jake
51
565-61-0909
I'd like to know what you people think of this .

Regards,
Me

Aug 10 '05 #1
2 1613
blip wrote:
Is this acceptable ? It seems too easy and too simple...

... snip ...
Here's the file contents exactly as in file.txt

bob
44
123-23-4532
fred
61
434-98-7654
hilbert
23
772-81-7108
peter
12
132-64-0132
jake
51
565-61-0909
I'd like to know what you people think of this .

Regards,
Me

It looks okay to me. But I didn't test it. If it works when you run it,
then I'd say it's probably good. Maybe someone sees something wrong I
don't. I only glanced for a minute.

--John Ratliff
Aug 10 '05 #2
In message <kymKe.13373$084.5880@attbi_s22>, John Ratliff
<us**@example.net> writes
blip wrote:
Is this acceptable ? It seems too easy and too simple...
... snip ...
Here's the file contents exactly as in file.txt
bob
44
123-23-4532
fred
61
434-98-7654
hilbert
23
772-81-7108
peter
12
132-64-0132
jake
51
565-61-0909
I'd like to know what you people think of this .
Regards,
Me

It looks okay to me. But I didn't test it. If it works when you run it,
then I'd say it's probably good. Maybe someone sees something wrong I
don't. I only glanced for a minute.


As an example of file reading and writing it's OK up to a point, but it
won't work if any of the strings contain spaces. operator>>(istream&,
string&) stops when it reaches whitespace.

For robust code it's probably better to define overloaded operators >>
and << for Person, which validate the data and provide some kind of
quoting convention to deal with the problem of embedded spaces etc.

(And it really ought to test for errors on all the file operations.)

As an example of good program design it has flaws in other areas, but I
don't think that was the point of the question.

--
Richard Herring
Aug 10 '05 #3

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

Similar topics

4
by: JDJones | last post by:
I'm trying to write a script that will read from a text list of songs that I burned onto my CD (Albums011.txt), then write to the database text the new text made ready for inserting into a...
2
by: Amy G | last post by:
I am looking to make this code a little "nicer"... any suggestions??? I want to do a "read+" where I would be able to first read the contents of the file... then either close the file out or write...
0
by: Joan Bos | last post by:
Hi, Is there somewhere on the Internet a description of what a .NET application config file should contain? For our application, I have to write passwords encrypted in a config file. The...
22
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; ...
1
by: cnu | last post by:
My program generates a log file for every event that happens in the program. So, I open the file and keep it open till the end. This is how I open the file for writing: <CODE> public...
4
by: ESPN Lover | last post by:
Below is two snippets of code from MSDN showing how to read a file. Is one way preferred over the other and why? Thanks. using System; using System.IO; class Test { public static void...
10
by: Tibby | last post by:
I need to read/write not only text files, but binary as well. It seems like on binary files, it doesn't right the last 10% of the file. -- Thanks --- Outgoing mail is certified Virus...
8
by: kepioo | last post by:
I currently have an xml input file containing lots of data. My objectiv is to write a script that reports in another xml file only the data I am interested in. Doing this is really easy using SAX....
3
by: =?Utf-8?B?ZGF2aWQ=?= | last post by:
I try to follow Steve's paper to build a database, and store a small text file into SQL Server database and retrieve it later. Only difference between my table and Steve's table is that I use NTEXT...
2
by: Kevin Ar18 | last post by:
I posted this on the forum, but nobody seems to know the solution: http://python-forum.org/py/viewtopic.php?t=5230 I have a zip file that is several GB in size, and one of the files inside of it...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

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.