473,406 Members | 2,847 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,406 software developers and data experts.

How to modify a file using C++ file objects

Greetings to All,

I have the following file and i need to modify it with the contents of an
array

File1.txt

Name Location Points Grade
Venkat,Newyork,100,A
Jack,LA,12,C
Heather,Las Vegas,190,B
Jay,Sanjose,199,A
John,Austin,1,D

int NewPoints = {23,1,100,181,100}
Now i need to write a program using file objects of C++(ifstream, ofstream)
such that it should open the file File1.txt and should modify the values
under Points section with that present in NewPoints.
It is urgent please let me know how to go about it.
Thanks and Regards,
Venkat

Jul 22 '05 #1
12 4881
Sounds like homework.
Venkat wrote:
Greetings to All,

I have the following file and i need to modify it with the contents of an
array

File1.txt

Name Location Points Grade
Venkat,Newyork,100,A
Jack,LA,12,C
Heather,Las Vegas,190,B
Jay,Sanjose,199,A
John,Austin,1,D

int NewPoints = {23,1,100,181,100}
Now i need to write a program using file objects of C++(ifstream, ofstream)
such that it should open the file File1.txt and should modify the values
under Points section with that present in NewPoints.
It is urgent please let me know how to go about it.
Thanks and Regards,
Venkat



Jul 22 '05 #2
Venkat writes:
I have the following file and i need to modify it with the contents of an
array

File1.txt

Name Location Points Grade
Venkat,Newyork,100,A
Jack,LA,12,C
Heather,Las Vegas,190,B
Jay,Sanjose,199,A
John,Austin,1,D

int NewPoints = {23,1,100,181,100}
Now i need to write a program using file objects of C++(ifstream, ofstream) such that it should open the file File1.txt and should modify the values
under Points section with that present in NewPoints.


First of all, you will have to create a new file and copy most of the old
stuff to the new file. Then delete the old file and rename the new file.
Done.

The reason for this is that the hard drive is used to emulate thousands of
tape drives, each file is really treated as a tiny tape unit (with a
potentially huge capacity!). And as you know, you can not add 'stuff' to an
audio tape or any other kind of tape. Note that the least record in your
exercise goes from 1 to 100. (You can't remove stuff either, but the adding
argument is probably more persuasive.)

Modify the points file as the new file is being created.

I doubt if the latest whiz-bang version of the C++ language provides all you
need, but there are some old, rusty tools in <cstdio> which will do the file
renaming bit.
Jul 22 '05 #3
"Nirmalya Ghosh Chowdhury" <ng******@ngc.com> wrote in message
news:3F**************@ngc.com...
Sounds like homework.


Hi Nirmalya,

Thanks for a hilarious comment. Really i am getting laugh on your comment
ha ha ha........
I want to make my question simple so that it would be easy for others to
understand and answer it.

My application involves writing a dll which when lanched calls the
file1.txt in question it is a CSV(Comma Seperated Value) file and replaces
its contents dynammically.

I knew the procedure of reading and writing to a file using ifstream and
ofstream objects but was wondering is there any wayout to edit the file at a
particular place.

To my understanding this newsgroup is meant to share the knowledge and i
am no naive to this group, i had been actively contributing to this group
from the past 2 years.

But to be frank comments like this would refrain people from using this
group.
regards,
Venkat


Jul 22 '05 #4

"Venkat" <ve*******@yahoo.com> wrote in message news:1073477752.960388@sj-nntpcache-5...
| Greetings to All,
|
| I have the following file and i need to modify it with the contents of an
| array
|
| File1.txt
|
| Name Location Points Grade
| Venkat,Newyork,100,A
| Jack,LA,12,C
| Heather,Las Vegas,190,B
| Jay,Sanjose,199,A
| John,Austin,1,D
|
| int NewPoints = {23,1,100,181,100}
|
|
| Now i need to write a program using file objects of C++(ifstream, ofstream)
| such that it should open the file File1.txt and should modify the values
| under Points section with that present in NewPoints.
|
|
| It is urgent please let me know how to go about it.

Try this for starters:

# include <iostream>
# include <fstream>
# include <ostream>
# include <string>
# include <sstream>

template<class T>
inline bool UpdateFile( const char* Old, const char* New,
const int& Col, const T& Array )
{
std::ifstream InFile( Old );
std::ofstream OutFile( New );

if( !InFile || !OutFile )
return false;

std::string::size_type ArrayIndex( 0 );
std::string::size_type Column( Col );
std::string::size_type FieldPosition( 0 );

std::string Line, Buffer;
while( std::getline( InFile, Line ) )
{
std::stringstream SS( Line );
while( std::getline( SS, Buffer, ',' ) )
{
if( Column == FieldPosition )
OutFile << Array[ ArrayIndex ] << " ";
else
OutFile << Buffer << " ";
++FieldPosition;
}

OutFile << std::endl;

FieldPosition = 0;
++ArrayIndex;
}

return true;
}

int main()
{
int NewPoints[] = { 23, 1, 100, 181, 100 };

if( UpdateFile( "MyFile.txt", "NewFile.txt", 2, NewPoints ) )
std::cout << "Done " << std::endl;
else
std::cout << "Some error occured " << std::endl;

return 0;
}

This will not remove the original, but should be able to do
that part :-). Just use 'std::remove()' and 'std::rename()'
in <cstdio>.

Cheers.
Chris Val
Jul 22 '05 #5
Chris ( Val ) writes:

[Scan way... down]
"Venkat" <ve*******@yahoo.com> wrote in message news:1073477752.960388@sj-nntpcache-5... | Greetings to All,
|
| I have the following file and i need to modify it with the contents of an | array
|
| File1.txt
|
| Name Location Points Grade
| Venkat,Newyork,100,A
| Jack,LA,12,C
| Heather,Las Vegas,190,B
| Jay,Sanjose,199,A
| John,Austin,1,D
|
| int NewPoints = {23,1,100,181,100}
|
|
| Now i need to write a program using file objects of C++(ifstream, ofstream) | such that it should open the file File1.txt and should modify the values
| under Points section with that present in NewPoints.
|
|
| It is urgent please let me know how to go about it.

Try this for starters:

# include <iostream>
# include <fstream>
# include <ostream>
# include <string>
# include <sstream>

template<class T>
inline bool UpdateFile( const char* Old, const char* New,
const int& Col, const T& Array )
{
std::ifstream InFile( Old );
std::ofstream OutFile( New );

if( !InFile || !OutFile )
return false;

std::string::size_type ArrayIndex( 0 );
std::string::size_type Column( Col );
std::string::size_type FieldPosition( 0 );

std::string Line, Buffer;
while( std::getline( InFile, Line ) )
{
std::stringstream SS( Line );
while( std::getline( SS, Buffer, ',' ) )
{
if( Column == FieldPosition )
OutFile << Array[ ArrayIndex ] << " ";
else
OutFile << Buffer << " ";
++FieldPosition;
}

OutFile << std::endl;

FieldPosition = 0;
++ArrayIndex;
}

return true;
}

int main()
{
int NewPoints[] = { 23, 1, 100, 181, 100 };

if( UpdateFile( "MyFile.txt", "NewFile.txt", 2, NewPoints ) )
std::cout << "Done " << std::endl;
else
std::cout << "Some error occured " << std::endl;

return 0;
}

This will not remove the original, but should be able to do
that part :-). Just use 'std::remove()' and 'std::rename()'
in <cstdio>.

Cheers.
Chris Val


Couldn't you detect from the wording that that was a student assignment????
Do you think you are really helping him by posting actual code??? Did you
ever hear the parable about the man and the fish?

-- Osmium
Jul 22 '05 #6
Venkat wrote:

To my understanding this newsgroup is meant to share the knowledge and i
am no naive to this group, i had been actively contributing to this group
from the past 2 years.


That's hard to believe given that your question covers an
extremely basic (and simple) job.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #7
look up fseek in your c++ help file
"Venkat" <ve*******@yahoo.com> wrote in message
news:1073477752.960388@sj-nntpcache-5...
Greetings to All,

I have the following file and i need to modify it with the contents of an
array

File1.txt

Name Location Points Grade
Venkat,Newyork,100,A
Jack,LA,12,C
Heather,Las Vegas,190,B
Jay,Sanjose,199,A
John,Austin,1,D

int NewPoints = {23,1,100,181,100}
Now i need to write a program using file objects of C++(ifstream, ofstream) such that it should open the file File1.txt and should modify the values
under Points section with that present in NewPoints.
It is urgent please let me know how to go about it.
Thanks and Regards,
Venkat


Jul 22 '05 #8

"Venkat" <ve*******@yahoo.com> wrote in message
news:1073480340.695754@sj-nntpcache-3...

To my understanding this newsgroup is meant to share the knowledge and i
am no naive to this group, i had been actively contributing to this group
from the past 2 years.
Your name looks new to me.
But to be frank comments like this would refrain people from using this
group.


If telling people we won't do their homework for them makes them refrain
from using the group, then so be it.
Jul 22 '05 #9
dwrayment wrote:

text mode, binary mode same s**t you can open whatever mode you like and to
the same job.
One more time: Please don't top post. Put your reply underneath the text
you are replying to and snip from the reply what you don't need any longer.

Not if you are working with a text file which doesn't have a constant line
length. The OP's file seems to be of that sort.

and fseek will do the job.


fseek could do the job, but it's much more complicated then simply
writing a new file.

The reason:
look at the numbers:

(Although the poster didn't mention it explicitely, it is still safe to
assume that this is a text file, since we have seen the very same homework
assignment a number of times in the past with the very same input file).

Name Location Points Grade
Venkat,Newyork,100,A
Jack,LA,12,C
Heather,Las Vegas,190,B
Jay,Sanjose,199,A
John,Austin,1,D

int NewPoints = {23,1,100,181,100}

Replace (in your mind) the '100' in the first record with 23. You can't do
it without moving the rest of the line (and the rest of the file) also.
You will agree that fseek is't really that much usefull in doing this.
Same for the last line: replace '1' with '100'. In order to do this the line
(and the rest of the file) has to be enlarged. Again: fseek isn't much of use in this.

While it theoretically is possible to do all the modifications on the original
text file:

remember the start of a line
reading a line into memory
do the modification in memory
seeke back to the start of the line at the file
determining if the file needs to be enlarger or shrinked
if so do the enlarge or shrink operation (by reading the rest
of the file into a buffer and rewriting it to the file such that
the gap is shortened or enlarged)
write the modified line back to the file

it turns out that the enlarge or shrink operation costs that much CPU and/or
I/O time and/or memory for the buffer, that it isn't worth the attempt.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #10

"dwrayment" <dw*******@rogers.com> wrote in message
news:7Q*******************@news01.bloor.is.net.cab le.rogers.com...
look up fseek in your c++ help file


Please don´t top post. Furthermore fseek will not be of much help in this
case as the OP is not dealing with a binary file where he can specify
exactly at which location (meaning which byte) the contents is found which
should be replaced.

Regards
Chris
Jul 22 '05 #11
text mode, binary mode same s**t you can open whatever mode you like and to
the same job.

and fseek will do the job.

"dwrayment" <dw*******@rogers.com> wrote in message
news:7Q*******************@news01.bloor.is.net.cab le.rogers.com...
look up fseek in your c++ help file
"Venkat" <ve*******@yahoo.com> wrote in message
news:1073477752.960388@sj-nntpcache-5...
Greetings to All,

I have the following file and i need to modify it with the contents of an array

File1.txt

Name Location Points Grade
Venkat,Newyork,100,A
Jack,LA,12,C
Heather,Las Vegas,190,B
Jay,Sanjose,199,A
John,Austin,1,D

int NewPoints = {23,1,100,181,100}
Now i need to write a program using file objects of C++(ifstream,

ofstream)
such that it should open the file File1.txt and should modify the values
under Points section with that present in NewPoints.
It is urgent please let me know how to go about it.
Thanks and Regards,
Venkat



Jul 22 '05 #12

"osmium" <r1********@comcast.net> wrote in message
news:bt************@ID-179017.news.uni-berlin.de...
| Chris ( Val ) writes:

[snip]

| Couldn't you detect from the wording that that was a student assignment????
| Do you think you are really helping him by posting actual code??? Did you
| ever hear the parable about the man and the fish?

My apologies to the group if it was an assignment, but at the
time of posting, I didn't realise it, otherwise, I would not
have provided it.

Cheers.
Chris Val
Jul 22 '05 #13

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

Similar topics

0
by: Chris McKeever | last post by:
I am trying to modify the Mailman Python code to stop mapping MIME-types and use the extension of the attachment instead. I am pretty much clueless as to what I need to do here, but I think I have...
4
by: John | last post by:
I am using code provided by Mr. Steele that allows for my MDB to dynamically connect to remote SQL server databases. The code works fine as follows: Type TableDetails TableName As String...
8
by: Sorin Sandu | last post by:
Is it posible to modify the lenghth of an array ? I want to do something like int i = 0; string debit = new string; string credit = new string; while (myread3.Read())
12
by: SStory | last post by:
Doing pages for contract..... If I make an ASPX file that does certain things, how simple would it be for a person who know nothing about it to modify the user interface without bothering the...
3
by: Maileen | last post by:
Hi, I've asked yesterday if someone already modify data into XML file using VB.NET. In fact, my XML file is like that. .... <DB> <DB_Loc>
2
by: Bob | last post by:
Hi, I have a list of widgets. I want to iterate through the selected items collection and modify one of the widgets properties. I tried foreach(widget w in lst.SelectedItems) w.MyProperty =...
1
by: TimEl | last post by:
Hi. Using Perl, I want to modify data in an XML file and print out the entire modified file, not just the elements I modify. In CPAN I have found that XPath allows me to pinpoint the elements...
8
by: Bob Altman | last post by:
Hi all, I'm trying to do something that should be really easy, but I can't think of an obvious way to do it. I have a dictionary whose value is a "value type" (as opposed to a reference type --...
6
by: Ramesh | last post by:
Hello, I am using the ofstream class to create a text file with keys and values like: Key1=Value10 Key2=Value15 Key3=Value20 In case I need to set a new value for Key2, say value50 - I am...
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.