GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 26-Sep-2003, 08:06
calculus87's Avatar
calculus87 calculus87 is offline
Junior Member
 
Join Date: Sep 2003
Location: FLorida
Posts: 41
calculus87 is on a distinguished road

Opening files with a twist, C++


Hello,

Okay, in my program I open a file and begin to put data in the file. Now before I am finished putting the data in the file I need to use some of the data.

okay for example
data in file1:
3 4

Now for the next input I need to take the sum of the first two statements. So the next spot in the statement would be 7

3 4 7

afterwards I go on inputting data.

3 4 7 1 ...

I tried to do a function inside a function, where the outer function opens the file, and begins to input data into it.
Then the inner function is called. It opens the file and takes the
first two inputs, does the conversions, adds them, converts them back then inputs the string into the file then exits.
The outer function then continues inputing into the file.

When I run my code I am supposed to have a=3 and b =4 but they are blank.

so my question is this.
1) can it be done?
2) here is some of the code that I have tried, any suggestions on how to fix this code?

Thanks for the help
~Ronald
CPP / C++ / C Code:
//**********************
int main()
{
fstream scan;
scan.open("calcout.txt", ios::out| ios::in);
output<<"3 ";
output<<"4 ";
output<<getstrings();
output<<"1 "
return 0;
}
//**********************
string getstrings()
{
string a, b;
ifstream input2;
double s1,s2,sum=0;
string t1;
stringstream stream;

input2.open("calcout.txt");
input2>>a;
input2>>b;
cout<<a<<endl<<b<<endl;

s1=atof(a.c_str());
s2=atof(b.c_str());
sum = s1 + s2;

stream << sum;			
t1 = stream.str();
input2.close();
return t1;
}
__________________
This message is a natural product made from recycled electrons. The slight
variations in spelling and grammar enhance its individual character and
beauty and in no way are to be considered flaws or defects.
  #2  
Old 26-Sep-2003, 09:44
Garth Farley Garth Farley is offline
Invalid Email Address
 
Join Date: May 2002
Location: Ireland
Posts: 638
Garth Farley is a jewel in the roughGarth Farley is a jewel in the roughGarth Farley is a jewel in the rough
Hey,

trying to have the same file opened twice and reading & writing simultaneously doesn't work. The file system should only allow you to open a file for writing if no other program is using it.

You'll have to rethink how you store your data - memory would be the handiest for small things.

Is this a very recursive operation? Will you need to go back & forward through a text file a lot? There are functions like seekg() where you can move the file pointer back & forward, but writing & reading simultaneously is still a problem.

GF
  #3  
Old 26-Sep-2003, 14:41
calculus87's Avatar
calculus87 calculus87 is offline
Junior Member
 
Join Date: Sep 2003
Location: FLorida
Posts: 41
calculus87 is on a distinguished road
What you have said makes great sense. I will try to use the seekg function. Thank again
Sincerely,
Ronald
__________________
This message is a natural product made from recycled electrons. The slight
variations in spelling and grammar enhance its individual character and
beauty and in no way are to be considered flaws or defects.
 
 

Recent GIDBlogMeeting the local Iraqis by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
gxx linker accepts only 7 object files danielxs66 C++ Forum 1 12-Dec-2003 09:27
How to create batch files without DOS box? rhino1616 Web Design Forum 3 18-Aug-2003 10:00
any one knows about the address of the deleted files in FAT shaam C++ Forum 1 09-Aug-2003 16:18
[Linux] How to upgrade an rpm with source files? JdS Computer Software Forum - Linux 2 18-Jun-2003 09:17
publishing Drirector/Shockwave files ...? darrin Web Design Forum 1 04-Oct-2002 17:21

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 23:22.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.