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
8 Replies - 961 Views - Last Post: 06 February 2011 - 06:49 PM
#1
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
Replies To: how to add a new line with text and the end of a file if the file alre
#3
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/
http://www.cplusplus...y/cstdio/fopen/
C++ use ios::app mode to open()
http://www.cplusplus...tutorial/files/
#4
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
[]
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
#5
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
#6
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);
#7
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
#8
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
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.
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? thanksgot 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.
#9
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
elninox91, on 06 February 2011 - 05:18 PM, said:
the
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.
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? thanksgot 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
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote




|