How to modify a file using C++ file objects

V

Venkat

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
 
V

Venkat

Nirmalya Ghosh Chowdhury said:
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
 
C

Chris \( Val \)

| 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::eek:fstream 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
 
O

osmium

Venkat said:
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.
 
K

Karl Heinz Buchegger

Venkat said:
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.
 
J

jeffc

Venkat said:
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.
 
C

Chris Theis

dwrayment said:
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
 
D

dwrayment

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.
 
O

osmium

Chris ( Val ) writes:

[Scan way... down]
| 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::eek:fstream 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
 
K

Karl Heinz Buchegger

dwrayment said:
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).


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.
 
C

Chris \( Val \)

| 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
 

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
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top