473,657 Members | 2,896 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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,1 00}
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 4903
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,1 00}
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,1 00}
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.c om> 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*******@yaho o.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,1 00}
|
|
| 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::si ze_type ArrayIndex( 0 );
std::string::si ze_type Column( Col );
std::string::si ze_type FieldPosition( 0 );

std::string Line, Buffer;
while( std::getline( InFile, Line ) )
{
std::stringstre am 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.tx t", 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*******@yaho o.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,1 00}
|
|
| 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::si ze_type ArrayIndex( 0 );
std::string::si ze_type Column( Col );
std::string::si ze_type FieldPosition( 0 );

std::string Line, Buffer;
while( std::getline( InFile, Line ) )
{
std::stringstre am 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.tx t", 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*******@yaho o.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,1 00}
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*******@yaho o.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,1 00}

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

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

Similar topics

0
2410
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 narrowed it down to the Scrubber.py file.. If this seems like a quick step me through, I would be very appreciative, could get you something on your Amazon wish-list (that is me on my knees begging).. From just my basic understanding, it...
4
11627
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 SourceTableName As String Attributes As Long IndexSQL As String End Type
8
2158
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
1727
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 ASPX interaction? How would I best build such pages. Many people of course don't want a page that they can't modify at all without programmer intervention. I think ASPX does this. Just curious to hear some comments on the subject from more...
3
13383
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
8791
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 = something; but I get a message saying "Cannot modify members of w because it is a foreach iteration variable" What is the point of iteration if you can't modify the objects?
1
7436
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 that I want to modify. But all of the code examples that I have seen assume that I want to assign the targeted elements to variables, modify each element, and then print only the modified elements out to a file. For example, this code is found at...
8
7972
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 -- in this case, a Boolean): Dim dic As New Dictionary(Of Int32, Boolean) dic(123) = True I want to set all of the existing entries in the dictionary to False. The
6
4968
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 able to
0
8384
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8820
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8718
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8601
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5630
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1937
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1601
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.