8 Replies - 1374 Views - Last Post: 06 February 2011 - 06:49 PM Rate Topic: -----

#1 elninox91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 06-February 11

how to add a new line with text and the end of a file if the file alre

Posted 06 February 2011 - 02:21 PM

ok so i have a file named "inputfile.txt" which contains 4 lines with text as follows:

acount # and amount of moeny
1234 100
2222 200
4444 150
7777 600

and i need to add new account in a new line but the account number comes from the user. i just need help in how to get the a new line with txt from the user. thanks
Is This A Good Question/Topic? 0
  • +

Replies To: how to add a new line with text and the end of a file if the file alre

#2 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: how to add a new line with text and the end of a file if the file alre

Posted 06 February 2011 - 02:25 PM

This snippet might be useful.
Was This Post Helpful? 0
  • +
  • -

#3 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 291
  • View blog
  • Posts: 1,900
  • Joined: 25-October 06

Re: how to add a new line with text and the end of a file if the file alre

Posted 06 February 2011 - 02:29 PM

you can open a file for append where writing operations append data at the end of the file, e.g. C use "a" mode to fopen()
http://www.cplusplus...y/cstdio/fopen/

C++ use ios::app mode to open()
http://www.cplusplus...tutorial/files/
Was This Post Helpful? 0
  • +
  • -

#4 elninox91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 06-February 11

Re: how to add a new line with text and the end of a file if the file alre

Posted 06 February 2011 - 02:44 PM

mmm i tried both methods but i think im not getting it . im going to post my code below

[]

int new_acct(int acctnum_array[], double balance_array[], int num_accts, ofstream &outfile)
{ outfile.close();
// open input file


ofstream dbfile;
dbfile.open("myinput.txt");
int requested_account;
int index;
double new_account;

cout << endl << "Please enter the name of your new account: "; //prompt for account number
cin >> new_account;
new_account=0,
outfile << "Your New Account Balance is : "<< new_account<<endl;


dbfile.close();
return num_accts;
}
[/]

ok i want to read a file tat has 4 lines as follows
acct # and amount of money
1234 150
4444 250
5555 300
8888 950

and i want to write some new lines to the same file i am reading from, i want to add a account number and assign to it the amount of money of $1 . thanks
Was This Post Helpful? 0
  • +
  • -

#5 elninox91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 06-February 11

Re: how to add a new line with text and the end of a file if the file alre

Posted 06 February 2011 - 03:01 PM

ok so i was able to write to the file i was reading from but when i a got to write the info i wanted to, ma previous info got erased. how can i write to the file without erasing my previous data . thanks
Was This Post Helpful? 0
  • +
  • -

#6 chinchang  Icon User is offline

  • Indie Game Developer
  • member icon

Reputation: 192
  • View blog
  • Posts: 725
  • Joined: 22-December 08

Re: how to add a new line with text and the end of a file if the file alre

Posted 06 February 2011 - 03:05 PM

Open your file like this :

dbfile.open("myinput.txt",ios::app);

Was This Post Helpful? 0
  • +
  • -

#7 elninox91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 06-February 11

Re: how to add a new line with text and the end of a file if the file alre

Posted 06 February 2011 - 03:07 PM

View Postchinchang, on 06 February 2011 - 03:05 PM, said:

Open your file like this :

dbfile.open("myinput.txt",ios::app);


im going to try it right now . thanks
Was This Post Helpful? 0
  • +
  • -

#8 elninox91  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 06-February 11

Re: how to add a new line with text and the end of a file if the file alre

Posted 06 February 2011 - 03:18 PM

the
1
dbfile.open("myinput.txt",ios::app);

adds the info to the file, however it adds it like next to last letter on the last word; it doesnt add the info in a new line . what do i need to include so tat the info goes in a new line? thanks

got it !!!! thanks guys.... now i just need to erase a line from the file . for instance :

lets say i got this
1234 5000
4567 8446
7812 500
4444 200
7777 600

and lets say i want to delete a line from the middle, how would i do it? thanks.
Was This Post Helpful? 0
  • +
  • -

#9 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 281
  • View blog
  • Posts: 1,788
  • Joined: 20-September 08

Re: how to add a new line with text and the end of a file if the file alre

Posted 06 February 2011 - 06:49 PM

View Postelninox91, on 06 February 2011 - 05:18 PM, said:

the
1
dbfile.open("myinput.txt",ios::app);

adds the info to the file, however it adds it like next to last letter on the last word; it doesnt add the info in a new line . what do i need to include so tat the info goes in a new line? thanks
got it !!!! thanks guys.... now i just need to erase a line from the file . for instance :
lets say i got this
1234 5000
4567 8446y
7812 500
4444 200
7777 600
and lets say i want to delete a line from the middle, how would i do it? thanks.

You could read your data file into a vector of struct ...
Then update that vector as you wish ...
Then rewrite the data in the vector container over top the old data file.
But may be better to use list instead of vector as a container if you want to do deleted easily.

This post has been edited by David W: 06 February 2011 - 06:52 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1