output to file (extremely simple but...)

This is a discussion on output to file (extremely simple but...) within the C++ Programming forums, part of the General Programming Boards category; Hi there. I'm trying to make a program that loads a value from a file (starting from 0 ) and ...

  1. #1
    csd@auth
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    71

    output to file (extremely simple but...)

    Hi there. I'm trying to make a program that loads a value from a file (starting from 0 ) and adds a number each time.
    i.e. I have a file "data.log" that contains 0 , after the first run I add 100 so now it contains 100, then I add 50 , and it contains 150 and so on.
    Code:
    #include <fstream>
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    int main()
    {
        int MyScore = 0 , i = 0;
    	char Temp = '0', c = '0';
        fstream my_file;
        my_file.open("data.log",   ios::out | ios::in);
        
    	if( my_file) cout <<"found"<<endl;
         else cout <<"not found"<<endl;
        
    // LOADING FROM FILE
    	 my_file.get(Temp);
         
    	 cout<<setw(40)<<"Simple Program"<<endl;
    	 for(i=0;i<80;i++) cout<<'-';
    	 gputs("How much completed?:\na. 1/3\nb. 1/2\nc. 1\n ans = ");
         c = cin.get();
         switch (c)
    	 {
    	 case 'a': MyScore += 50; break;
    	 case 'b': MyScore += 75; break;
    	 case 'c': MyScore +=100; break;
    	 default :  break;
    	 }
    	 cout<<endl<<endl;
    
         MyScore += Temp - '0';
    	 my_file<<"My Score is :"<<MyScore<<endl;
         my_file.put(MyScore);
    
    	my_file.close();
        cin.get();
        return EXIT_SUCCESS;
    }
    The problem is: 4 lines before the end
    Code:
    my_file.put(MyScore);
    doesn't work as it should
    Do you have any idea why?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    put() outputs a character, and so it is turning your int score into the character with that character code. You already output the score on the line above, so what are you trying to do with put?

  3. #3
    csd@auth
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    71
    oops , I couldn't decide which function to use. Anyway, even the line
    Code:
    my_file<<"My Score is :"<<MyScore<<endl;
    doesn't work.
    I noticed that if I remove the main part
    Code:
        
    // LOADING FROM FILE
    	 my_file.get(Temp);
         
    	 cout<<setw(40)<<"Simple Program"<<endl;
    	 for(i=0;i<80;i++) cout<<'-';
    	 gputs("How much completed?:\na. 1/3\nb. 1/2\nc. 1\n ans = ");
         c = cin.get();
         switch (c)
    	 {
    	 case 'a': MyScore += 50; break;
    	 case 'b': MyScore += 75; break;
    	 case 'c': MyScore +=100; break;
    	 default :  break;
    	 }
    	 cout<<endl<<endl;
    
         MyScore += Temp - '0';
    it works fine

  4. #4
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    You shouldn't use general fstreams to do both input and output because there are wacky rules for switching between I and O and sometimes it just doesn't work for no reason. But there's reasons, and one of them is the eofbit being set and stopping all operations on the stream. Try this and see if it works:
    Code:
    my_file.clear();
    my_file<<"My Score is :"<<MyScore<<endl;
    my_file.put(MyScore);

  5. #5
    csd@auth
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    71
    I tried it and it worked! thanks a lot.

  6. #6
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    Stream I/O is the only thing I'm really good at.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    I'm pretty sure that EXIT_SUCCESS is in <cstdlib>.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. File Input and Output, simple.
    By Vber in forum C Programming
    Replies: 5
    Last Post: 11-17-2002, 01:57 PM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 03:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21