Creating NEW notepad Doc.

Okay, i know how to edit the contents of a notepad document, but how do i create a brand new one? and when i start to edit an old one, (one that was already made) how to i "cout<<" what is currently inside it? and every time i start to edit an old one, all of the lines of ttext inside it area automatically deleted. How do i stop them from being deleted automatically?

Thanks for all the help guys! :D
closed account (3pj6b7Xj)
cout << ? Wait a minute, I don't understand you. You are making a console type of program?
@mrfaofx

How do i open up a NotePad File (in a C++ console Program) and then print on the console program the contents of the NotePad file i opened up?

And, how do i create a new NotePad document through coding?
@mrfaofx

How do i open up a NotePad File (in a C++ console Program) and then print on the console program the contents of the NotePad file i opened up?

And, how do i create a new NotePad document through coding?
closed account (3pj6b7Xj)
you'll first want to create a stream for reading the data, I would use fstream, vector and string might need more who knows...i wuld use the vector to create a vector<string> myfile;
as your reading the file, you'll want to add new lines by using myfile.push_back(the data of the file) you'll want to do this until you reach the end of the file EOF.

Once you have your data loaded you can load it page by page or create an array that will display 25 lines of text based on the array index, this way you can scroll the document up and down using the index drawing only 25 lines.

You'll want to tag an extra white space on lines less than the width of your document, for example if that array i was talking about displays 80 characters across and you have a line thats lets day 50 characters long, you'll want to add a space of 30 characters so that when you scroll you don't get left over garbage from the previous line that was there.

Its pretty easy...it gets even more fun if you design your own little cursor, you can then move the cursor anywhere in the screen based on the array index or another index that represents the position your at in a text line and you can insert, add and delete text..you could make a full fledged text editor and have lots of fun with it.

Would you like to see some code? I have already done this but unfortunately I used conio.h to get access to the cursor and colors...don't use conio.h i believe there is an library out there called cputs or something that is portable and you can jazz up your program with some nice colors.
Last edited on
closed account (3pj6b7Xj)
Oh yeah to save a file, same idea. Use fstream but you'll want to do this instead, could use an iterator to cycle through the index outputing all the lines out to the file to be saved. Hard to explain.
Okay, sounds complicated since i just started but ill try it. Thanks for the help again man. :)
closed account (3pj6b7Xj)
Oh sorry if it sounded complicated, i'll write some code for you tonight, probably some pseudo code and how to use fstream to read files, you can search the net thou.
closed account (3pj6b7Xj)
Look at your other post >>> http://cplusplus.com/forum/windows/35885/ someone already has posted some code on how to use fstream, try to keep your posts to one, if you post the same topic over and over, you'll get flagged for spamming the forum and you'll probably get a friendly warning from a moderator or admin the first time, careful.
Thanks for all the help, but im wondering how to create a NEW notpad doc, and then store stuff into that. I already know how to store tho. Im just tired of making pre-made notepad docs that are blank.
Hi DeeJayParadice!

It would help your cause to use correct terminology. What I think you are saying is that you want to create and/or work with something which is termed a 'text file'. 'Notepad document' is slightly strange terminology to use. When you are starting out on any new endeavor it is a good idea to try to learn the correct terminology so that people will understand you. The reason 'Notepad Document' isn't the best choice for terminology is that Notepad.exe is a specific instance of a class of programs known as 'text editors'. There are hundreds, nay thousands, nay millions of them! What you want to learn how to use (create.manipulate) are text files. These files are fairly simple in nature and only contain simple character codes with only simple formatting information such as carriage return / line feed characters, tab characters, etc.
Okay, ill google it...Thanks freddie!
Okay, ill google it...Thanks freddie!
closed account (3pj6b7Xj)
Alright bud, I have some code for you a complete working application. However, I got carried away while making and forgot this was for you but it does work, just copy and paste into your compiler and compile as a windows app, it should work right out of the package.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Main.cpp ; 2/7/2011
// By: Rafael Perez-Santana

/* This program opens a file and allows you to scroll up and down. Side
 scrolling is also supported if the file width is larger than the window
 view. Only a vertical scroll bar is implemented; click the arrows or drag
 the thumb track to scroll text on-demmand.

 The following lines of text are from the Win32/API, I inserted them here because
 they will be bigger than the window view width, causing this application to activate
 side scrolling to demonstrate this functionality.

 ////////////////////////////////////////////////////////////////////////////
 Note that the WM_VSCROLL message carries only 16 bits of scroll box position data. Thus, applications
 that rely solely on WM_VSCROLL (and WM_HSCROLL) for scroll position data have a practical maximum
 position value of 65,535.

 However, because the SetScrollPos, SetScrollRange, GetScrollPos, and GetScrollRange functions support
 32-bit scroll bar position data, there is a way to circumvent the 16-bit barrier of the WM_HSCROLL
 and WM_VSCROLL messages. See GetScrollPos for a description of the technique and its limits.
 ////////////////////////////////////////////////////////////////////////////

 I implemented code that will draw a "»" to the line that exceeds the window view
 width, neat little feature I believe. I didn't wanted to implement a horizontal
 scroll bar. I don't know if its just me but they look ugly.

 To keep this example less complicated than it already is, I did not opt
 to implement a open file dialog box, to open the file you want, scroll
 down until you see WM_CREATE: and look for

    const char*FileName="Main.cpp"; // The file to open.

 Change "Main.cpp" to the file you want to open, for example, if you wanted
 to open a file called readme.txt, replace "Main.cpp" with "readme.txt". If
 the file is not in the current directory you can use slashes the following
 way; "c:/somefolder/documents/sales.txt" do not use "\" C++ reserves that
 for special uses such as \n \a or \r the compiler will simply get confused
 so use "/" instead.

 You can open just about any file with this thing, even garbage files. Be
 warned, it does not check to see if you have exceeded system memory when
 loading a file, so you may get a run-time error if memory gets maxed out.
 Also, some files may not side scroll correctly since I didn't invest much
 time to debug this feature.

 This is intended to serve as a learning/tutorial experience for those
 learning windows programming, how to load files, how to create scrollbars,
 etc. Enjoy!
*/

// RANDOM: I looked at my code and comments, then realized that I may be
// a perfectionist?! I wonder if thats a good thing or a bad thing. O_o

#include <windows.h>
#include <vector>
#include <string>
#include <fstream>

// Namespaces to use.
using std::vector;
using std::string;
using std::ifstream;

struct VIEWDATA
// Structure for scrolling the file data.
{
    UINT Offset_X; // Horizontal array offset.
    UINT Offset_Y; // Vertical array offset.
    UINT Width; // Number of columns to display.
    UINT Height; // Number of rows to display.
    UINT FileWidth; // Largest string found.
    UINT FileLength; // Total number of lines in file.
    COLORREF Background; // Viewport background.
    COLORREF Foreground; // Viewport foreground.
    vector<string>Text; // File data is stored here.
};

// Declare function proto-types.
LRESULT CALLBACK WinProc(HWND,UINT,WPARAM,LPARAM);
void TxtOut(HDC,int,int,LPCSTR,COLORREF);

// Program entry point.
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE,LPSTR,int)
// Note: Anyone care to explain how this WinMain function works with no
// variables specified for the 2nd,3rd & 4th parameters? I didn't need them
// for this program but how in the world does the compiler allow this?
{
    WNDCLASSEX WinClassEx; // Data structure for the window class.

    // Window structure.
    WinClassEx.hInstance=hInstance;
    WinClassEx.lpszClassName="WindowClassEx";
    WinClassEx.lpfnWndProc=WinProc; // Pointer to window procedure.
    WinClassEx.style=CS_HREDRAW|CS_VREDRAW;
    WinClassEx.hIcon=LoadIcon(NULL,IDI_APPLICATION);
    WinClassEx.hIconSm=LoadIcon(NULL,IDI_APPLICATION);
    WinClassEx.hCursor=LoadCursor(NULL,IDC_ARROW);
    WinClassEx.lpszMenuName=NULL;
    WinClassEx.cbClsExtra=0;
    WinClassEx.cbWndExtra=4; // Extra bytes for pView pointer.
    WinClassEx.hbrBackground=CreateSolidBrush(RGB(0,0,200));
    WinClassEx.cbSize=sizeof(WNDCLASSEX);

    if(!RegisterClassEx(&WinClassEx))
    {   // If there was a failure, alert the user.
        MessageBox(NULL,"Failed to register window class.","Error",MB_ICONERROR);
        return 0; // Quit program.
    }

    // Now we specify width and height of window.
    UINT iWidth=800,iHeight=600,iWinX=0,iWinY=0; // iWinX & iWinY are 0 for now.

    // Create the application window.
    HWND hWnd=CreateWindowEx
        (0, // Extended window style.
         WinClassEx.lpszClassName, // Window class name.
         NULL, // I will set during WM_CREATE.
         WS_EX_LAYERED, // Look it up, sorry.
         iWinX,iWinY, // Window position.
         iWidth,iHeight, // Window dimensions.
         HWND_DESKTOP, // Window is a child-window to desktop.
         NULL, // No menu for this one.
         WinClassEx.hInstance, // Application instance.
         NULL); // No window creation data.

    if(!hWnd)
    {   // If there was a failure, alert the user.
        MessageBox(NULL,"Failed to create window.","Error",MB_ICONERROR);
        return 0; // Quit program.
    }

    // Adjust size of the window client area, we want exact width & height.
    RECT rect;GetClientRect(hWnd,&rect); // Get our client area dimensions.
    iWidth+=(iWidth-rect.right);iHeight+=(iHeight-rect.bottom); // Add the difference.

    // Also center the window in the desktop area.
    SystemParametersInfo(SPI_GETWORKAREA,0,&rect,0); // Get desktop minus taskbar area.
    iWinX=(rect.right-iWidth)/2;iWinY=(rect.bottom-iHeight)/2; // Calculate center.

    // Update window position and size.
    MoveWindow(hWnd,iWinX,iWinY,iWidth,iHeight,NULL);
    ShowWindow(hWnd,SW_SHOWNORMAL); // Display the window.

    MSG Msg; // Save window messages here.

    // Start the message pump.
    while(GetMessage(&Msg,NULL,0,0)) // Get it.
    {
        TranslateMessage(&Msg); // Translate it.
        DispatchMessage(&Msg); // Pump it.
    }

    return 0; // End.
}


Damn, it is quite big so part 2 is on the next post.
closed account (3pj6b7Xj)
Here is the proc for the application...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// This is our window procedure, called by DispatchMessage()
LRESULT CALLBACK WinProc(HWND hWnd,UINT Message,WPARAM wParam,LPARAM lParam)
{
    // Make pView available to entire procedure.
    VIEWDATA*pView=(VIEWDATA*)GetWindowLong(hWnd,0);
    if(!pView)pView=NULL; // Set to NULL to be safe.

    switch (Message) // Pumped messages come out from here.
    {
        case WM_CREATE: // What to do before window is visible.
        {
            VIEWDATA*pView=new VIEWDATA; // Allocate memory for structure.

            // Window view parameters.
            pView->Offset_X=0; // Horizontal scrolling offset.
            pView->Offset_Y=0; // Vertical scrolling offset.
            pView->Width=97; // Width of view field.
            pView->Height=32; // Height of view field.
            pView->FileWidth=0; // Largest string found.
            pView->FileLength=0; // Total number of lines in file.
            pView->Background=RGB(0,0,200); // Background color.
            pView->Foreground=RGB(200,200,200); // Foreground color.

            // Load file into memory.
            const char*FileName="Main.cpp"; // The file to open.
            char BufferStr[1024]; // Temporary buffer.
            ifstream FileIn(FileName); // Open the file for reading.
            if(!FileIn.is_open())
            {   // File open failed, alert user.
                wsprintf(BufferStr,"Failed to open file: %s",FileName);
                MessageBox(hWnd,BufferStr,"Error",MB_ICONERROR);
                return -1; // Abort execution.
            }

            while(!FileIn.eof())
            {   // While not end of file, loop.
                FileIn.getline(BufferStr,1024); // Read line from file.
                pView->Text.push_back(BufferStr); // Store this line.
                pView->FileLength++; // Increment with every line read.

                // This will ultimately set the scroll width.
                if(pView->FileWidth<strlen(BufferStr))
                    pView->FileWidth=strlen(BufferStr);
            }

            FileIn.close(); // Close file when done.

            // Setup the scroll bar for this window.
            SetScrollRange(hWnd,SB_VERT,0,pView->FileLength-pView->Height-1,true);

            // Modify the window caption to display some data.
            wsprintf(BufferStr,"%s - %i lines.",FileName,pView->FileLength,pView->FileWidth);
            SetWindowText(hWnd,BufferStr); // Set new window caption.

            // Save pView to window memory.
            if(pView)SetWindowLong(hWnd,0,(long)pView);
                else return -1; // quit if fail.

            break;
        }

        case WM_PAINT: // What to paint on the window.
        {
            PAINTSTRUCT ps; // Our paint structure.
            HDC hDC=BeginPaint(hWnd,&ps); // Start paint scene.

            // Display the file in the client area of window.
            SetBkColor(hDC,pView->Background);
            string Space, Text; // String objects.
            for(UINT i=0,iRow=10;i<pView->Height;i++,iRow+=18)
            {
                // If the string is less than the View.FileWidth, add a space.
                if(pView->Text[pView->Offset_Y+i].length()<pView->FileWidth)
                    Space.assign(pView->FileWidth-pView->Text[pView->Offset_Y+i].length(),' ');
                // Note: The only reason I add a space is to prevent left overs from the
                // previous lines since I am not redrawing the window to prevent flicker.

                // The following assign operation takes the string at at the Y offset
                // location starting from Offset_X and up to View.Width. This is what
                // allows scrolling left and right using Offset_X (^_^)
                Text.assign(pView->Text[pView->Offset_Y+i]+Space,pView->Offset_X,pView->Width);
                // Note: Text is not the same as View.Text, these two are different!

                // Display the data in the client window.
                TxtOut(hDC,5,iRow,Text.c_str(),pView->Foreground);

                // This helps identify strings that are bigger in width than the
                // window view width. A "»" is drawn to tell the user there is more.
                if(pView->Text[pView->Offset_Y+i].length()-pView->Offset_X==pView->Width||
                   pView->Text[pView->Offset_Y+i].length()-pView->Offset_X<=pView->Width||
                   pView->Text[pView->Offset_Y+i].length()<pView->Width)
                // I normally don't use bracket enclosures for a single line of code
                // after an "if" statement but since this example already looks intense,
                // the brackets help to read the code a bit.
                {
                    TxtOut(hDC,pView->Width+690,iRow," ",RGB(0,0,200));
                }
                else // Draw the little arrows.
                {
                    TxtOut(hDC,pView->Width+690,iRow,"»",RGB(255,255,0));
                }

                // Clear the strings.
                Space.clear();Text.clear();
            }

            EndPaint(hWnd,&ps); // End paint scene.
            break;
        }

        case WM_KEYDOWN: // Scroll file with arrow keys.
        {
            if(wParam==VK_UP)
            {
                if(!pView->Offset_Y)break; // Can't go up.
                pView->Offset_Y--; // Move Up.
            }

            if(wParam==VK_DOWN)
            {
                // Can't go down, end of file has been reached.
                if(pView->Offset_Y+pView->Height+1==pView->FileLength)break;
                pView->Offset_Y++; // Move down.
            }

            if(wParam==VK_RIGHT)
            {
                // Abort if file width is less than view width.
                if(pView->FileWidth<pView->Width)break;
                // Abort if string is completely visible in window.
                if(pView->Offset_X+pView->Width==pView->FileWidth)break;
                pView->Offset_X++; // Scroll to the right.
            }

            if(wParam==VK_LEFT)
            {
                if(!pView->Offset_X)break; // Can't go left.
                pView->Offset_X--; // Scroll left.
            }

            // Update the scroll bar and client area.
            SetScrollPos(hWnd,SB_VERT,pView->Offset_Y,true);
            InvalidateRect(hWnd,NULL,false);

            break;
        }

        case WM_VSCROLL: // Scroll file with window scroll bar.
        // I used the same code as WM_KEYDOWN but added some more.
        {
            if(LOWORD(wParam)==SB_LINEUP)
            {
                // Already at top of file.
                if(!pView->Offset_Y)break;
                pView->Offset_Y--; // Move Up.
            }

            if(LOWORD(wParam)==SB_LINEDOWN)
            {
                // End of file has been reached.
                if(pView->Offset_Y+pView->Height+1==pView->FileLength)break;
                pView->Offset_Y++; // Move down.
            }

            // Scroll the file using the scroll bar thumb track.
            // Some people have problems implementing this, it is really
            // not that hard. Just get nPos from HIWORD(wParam) and set
            // your array offset to that...effing magic!
            if(LOWORD(wParam)==SB_THUMBTRACK)pView->Offset_Y=HIWORD(wParam);

            // Update the scroll bar and client area.
            SetScrollPos(hWnd,SB_VERT,pView->Offset_Y,true);
            InvalidateRect(hWnd,NULL,false);

            break;
        }

        case WM_DESTROY: // What to do when window is closed.
        {
            if(pView)delete pView; // Free memory.
            PostQuitMessage (0); // Post a WM_QUIT message into queue.
            break;
        }

        // Pump non-trapped messages to default procedure.
        default:return DefWindowProc(hWnd,Message,wParam,lParam);
    }

    return 0; // A 0 value indicates a message was trapped.
}


lol amazing...the TxtOut() functions follows.
closed account (3pj6b7Xj)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Displays text on the screen at x & y using given color
void TxtOut(HDC hDC,int x,int y,LPCSTR TextStr,COLORREF crColor)
// Adds more functionality to TextOut() (^_^)
{
    HFONT hfFont; // create font object
    // define logical units in height for font.
    long lfHeight=-MulDiv(10,GetDeviceCaps(hDC,LOGPIXELSY),72);
    // Create the font to use for output.
    hfFont=CreateFont(lfHeight,0,0,0,0,0,0,0,0,0,0,PROOF_QUALITY,0,"Fixedsys");
    SelectObject(hDC,hfFont); // Select the font to window device-context.
    SetTextColor(hDC,crColor); // Text color.
    TextOut(hDC,x,y,TextStr,strlen(TextStr)); // Display text.
    DeleteObject(hfFont); // Delete the hfFont object.
}


Oh =( this is probably overkill for you, just copy and paste it all in sequence to your compiler, call it main.cpp, the application would open the main.cpp file by default. I'm so sorry for making this so big and complex but hopefully you can understand it. Just so you know what the application looks like I took a pic.

Here it is >>> http://i56.tinypic.com/hrf22b.png

I hope it helps, I got so into this litle program that I worked on it some more. The version after this one, is able to open files, save files and even edit text...it looks like a DOS editor with the blue background and foreground colors. When I have the application fully operational, I can send you all the code if you like so you can study it.
Topic archived. No new replies allowed.