#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    60
    Rep Power
    11

    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
    //**********************
    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. #2
  3. I'm Baaaaaaack!
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Jul 2003
    Location
    Maryland
    Posts
    5,538
    Rep Power
    244
    First, please use "code" tags. Second, unless you turn off buffering, flush and rewind, or use memory mapped files you will always run into problems. The best way to handle it to me would be to simply hold all the data in memory until you have finished your work on it, then write it to a file.

    My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
    Free code: http://sol-biotech.com/code/.
    Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
    Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.
    LinkedIn Profile: http://www.linkedin.com/in/keithoxenrider

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw
  4. #3
  5. ASP.Net MVP
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Aug 2003
    Location
    WI
    Posts
    4,378
    Rep Power
    1510
    look into seekg and seekp functions.

    Hmm, also looks like you never output anything to file. You named your stream scan, but then use output for your stream operators. It should be either scan << "3 " << "4 "; or fstream output; output.open(...);

    Also, you are making this way too hard on yourself. There are much simpler ways to do several things in there. You should be able to declare a and b as integers or floats instead of strings and then you don't have to mess with atof for anything, and the stream operators know how to handle numbers as well. I would give you code, but this brings us to my final point, which is:

    What does this program do? It has no point that I can tell, and it looks suspiciously like a homework assignment. I don't think it is, because an assingment would at least have a point. It looks more like you are just writing your own "proof of concept" code. Either way, however, you will benefit more from writing the code on your own.
    Primary Forum: .Net Development
    Holy cow, I'm now an ASP.Net MVP!

    [Moving to ASP.Net] | [.Net Dos and Don't for VB6 Programmers]

    http://twitter.com/jcoehoorn
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    60
    Rep Power
    11
    Well,
    Thank you both for your suggestions. Actually, I am doing a very large project, not for school, and this is just one small part that I am trying to achieve. I always try to break down what I am trying to do to a very simple form, where I can still understand what needs to be done. Again thank you both for your help.
    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.

IMN logo majestic logo threadwatch logo seochat tools logo