Thread: Writing to a File

  1. #1
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74

    Writing to a File

    Quik noob question. Suppose I want to have my program out put a file called "Scores.txt" to the desktop what would the syntax be for doing that.
    I have the ofstream out_stream;
    but what would i put here -> out_stream.open ( "Scores.txt"); to get it to the desktop? As of right now it just writes it to the project dirictory. but i need it to go to the desktop.

    thx
    ben

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Code:
    #include <windows.h>
    #include <shlobj.h>
    #include <iostream>
    #include <fstream>
    #include <cstring>
    using namespace std;
    
    int main()
    {
        char desktop_path[MAX_PATH];
    
        // Get a token for this process. 
        HANDLE hToken;
        if (!OpenProcessToken(GetCurrentProcess(), 
                              TOKEN_IMPERSONATE | TOKEN_QUERY, &hToken))
        {
            cerr << "OpenProcessToken() failed, ec = " << GetLastError() << endl;
            return 1; 
        }//if
    
        // Get the desktop folder for the user of this process
        HRESULT res = SHGetFolderPathA(NULL, CSIDL_DESKTOPDIRECTORY, 
                                       hToken, 0, desktop_path);
        
        CloseHandle(hToken);
    
        if (!SUCCEEDED(res))
        {
            cerr << "SHGetFolderPathA() failed, ec = " << GetLastError() << endl;
            return 2;
        }//if
    
        strcat(desktop_path, "\\Scorse.txt");
    
        ofstream touch(desktop_path);
        touch.close();
    
        cout << desktop_path << endl;
    
        return 0;
    }//main
    gg

  3. #3
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74
    thx codeplug. But i think thats a little advanced for me. Im just starting out and wrote a little program. Hers the part im trying to get to the desktop.
    Code:
    ofstream out_stream;
    	out_stream.setf(ios::fixed);
    	out_stream.setf(ios::showpoint);
    	out_stream.precision(2);
    	out_stream.open("Scores.txt");
    	out_stream << "Scores By: Kimberly" << endl<< endl;
    	out_stream << setw(48) << "* Service Scores *" << endl << endl;
    	out_stream << "Advisor" << setw(34) << "Score" << setw(37) << "Total Surveys" << endl;
    	out_stream << "-------" << setw(35) << "-------" << setw(36) << "-------------" << endl;
    	out_stream << "Jeremy " << setw(35) << Jeremy  << setw(31) << sJeremy <<endl;
    	out_stream << "Harry "  << setw(36) << Harry   << setw(31) << sHarry << endl;
    	out_stream << "Tina "   << setw(37) << Tina    << setw(31) << sTina << endl;
    	out_stream << "Louis"   << setw(37) << Louis   << setw(31) << sLouis << endl;
    	out_stream << "Mike "   << setw(37) << Mike    << setw(31) << sMike << endl;
    	out_stream << "Chris"   << setw(37) << Chris   << setw(31) << sChris << endl;
    	out_stream << "Antonio" << setw(35) << Antonio << setw(31) << sAntonio <<endl;
    	out_stream << "Gary "   << setw(37) << Gary    << setw(31) << sGary << endl;
    	out_stream << "Irene"   << setw(37) << Irene   << setw(31) << sIrene <<endl << endl << endl;
    	out_stream << "OSS " << setw(37) << "Rec." << setw(34) << "FRFT" << endl;
    	out_stream << "-----"  << setw(37) << "-----" << setw(34) << "-----" << endl;
    	out_stream << oss << setw(36) << rec << setw(34) << frft_score << endl << endl << endl;
    	out_stream << setw(48) << "* Sales Scores *" << endl << endl;
    	out_stream << setw(24) << "OSS" << setw(36) << "Rec." << endl;
    	out_stream << setw(24) << "-----" << setw(36) << "-----" << endl;
    	out_stream << setw(24) << sales_oss << setw(36) << sales_rec << endl << endl;
    	out_stream << setw(45) << "* VOC * " << endl;
    	out_stream << setw(44) << "-------" << endl;
    	out_stream << setw(43) << voc_score << endl << endl << endl;
    	out_stream.close();
    	cout << "Hit Any Key And Press Enter To Exit: ";
    	cin >> dummy;
    	return 0;
    }
    This is just a little console app that I wrote as im learning to program.(chapter 4) How could I get it to the desktop using this?
    thks
    ben
    Last edited by big146; 06-09-2004 at 05:55 AM.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Change
    >> out_stream.open("Scores.txt");
    to
    >> out_stream.open(desktop_path);
    and run the code that populates desktop_path before hand.

    As easy as it gets.

    gg

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Its actully really easy to do just change this
    Code:
    out_stream.open("Scores.txt");
    To This
    Code:
    out_stream.open("C:/windows/destktop/Scores.txt");
    Woop?

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    That won't work on my machine....but feel free to hardcode the full path if you only care about it running correctly on your machine.

    gg

  7. #7
    Registered User big146's Avatar
    Join Date
    Apr 2003
    Posts
    74
    Thanks codeplug and prog-bman for the help. I see what your saying about portability codeplug.But this program is just going to be on my wifes computer so it wont matter for now.but thanks for the code ill use it later when i get there.

    prog-bman that did the trick. I had the " \ " this way instead of this way " / ". once i changed it it wrote to the desktop.

    thanks to the both of you for the help I appreciate it.

    ben

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM