By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,705 Members | 1,301 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,705 IT Pros & Developers. It's quick & easy.

How to modify a file using C++ file objects

P: n/a
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
Share this Question
Share on Google+
12 Replies


P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a

"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 discussion thread is closed

Replies have been disabled for this discussion.