Newbie File Read and Save problem

This is a discussion on Newbie File Read and Save problem within the Windows Programming forums, part of the Platform Specific Boards category; Hi All! I hope you can help. I'm trying to write a very basic C app with one dialog box. ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    4

    Newbie File Read and Save problem

    Hi All!

    I hope you can help.

    I'm trying to write a very basic C app with one dialog box. The window contains 2 multiline text/edit fields.

    What I need is:

    1. A function to (called on opening the app):

    (a) if exisits, copy "sample.bat" to "sample.bat.bak" //create a backup of course
    (b) read "sample.bat" into the text field (IDEDIT1) in dialog box for editing.
    (c) delete "sample.bat"
    (d) if not exists create new (empty) "sample.bat" for editing
    (e) and then read the new "sample.bat" into the text field (IDEDIT1) in dialog box for editing.

    2. A function for the SAVE button to:

    (a) write/save edited text from IDEDIT1 to new "sample.bat"

    Each of these functions would be called twice I presume, one for each file. The 2 (sample.bat, sample2.bat) files will be hard coded in the function call, and will always be in the same directory as the app itself.

    As you can see its very basic. I have the structure in place using Lcc-W32 Wedit, but just cant get these functions figured out.

    I have read all the posts on this board regarding file i/o but none seem to be doing what I want, and I'm concerned about the strange characters used in batch files.

    I'm a 3 day C programmer, with most of my experience (which is minimal) in PHP...not having '$' in front of vars is really messing me up!!!

    Anyhooo, I appreciate I'm asking a lot, but I dont know where to start really, and found I learnt PHP in the early days by copying good examples, so hopefully that will work hear too.

    Thanks!!
    Last edited by simham_uk; 06-05-2002 at 05:05 PM.

  2. #2
    Unregistered
    Guest
    Look at these functions
    hFile=CreateFile(sFilePath, GENERIC_WRITE|GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);//CREATE_ALWAYS

    GetFileSize()//get the size to read and alloc
    GlobalAlloc()//to alloc a char buffer for the file
    ReadFile()//read the whole file at once
    CloseHandle()//to close the file
    SetDlgItemText()
    GetDlgItemText()
    DeleteFile()
    WriteFile()//write new file
    GlobalFree()//free the alloced buffer

    Watch your edit as may need to change the default styles to handle large(ish) amounts of data.


    good luck. Post your attempt if you have problems.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    4
    Thanks!

    I have already looked over these extensively and still cant figure it out.

    I have got the write to file working with the following function:

    Code:
    int SaveBat (HWND hwndDlg)
    {
    	// took me 3 hours to get this working!!
                    FILE *fptr;
    	char mytexta[1024];
    				
    	fptr = fopen("sample1.bat", "w");
    	GetDlgItemText(hwndDlg, IDEDIT1, mytexta, 1023);
    	fprintf(fptr,"%s",mytexta);
    	fclose(fptr);
    	return TRUE;
    }
    but I cant seem to reverse this to read the file contents into the field.

    I've tried the following, but it only shows the first line using fgets. Again I have tried examples of while statements to read all lines, but can't get them to work

    Code:
    int LoadBat (HWND hwndDlg)
    {
    	FILE *fptrc;
    	char mytextc[1024];
    				
    	fptrc = fopen("sample1.bat", "r");
    	fgets( mytextc, 1023, fptrc );
    	SetDlgItemText(hwndDlg, IDEDIT1, mytextc);
    	fclose( fptrc );
    	return TRUE;
    }
    If anyone can show me how to make this work, or show me how to make the WinAPI method work I would be most grateful.

    Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-19-2009, 11:56 AM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 01:44 PM
  3. Still can not write or read from save file
    By WackoWolf in forum C++ Programming
    Replies: 37
    Last Post: 11-14-2004, 07:28 PM
  4. Still can not write or read from save file
    By WackoWolf in forum C++ Programming
    Replies: 2
    Last Post: 11-14-2004, 01:21 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

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