Thread: File Issue

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    29

    File Issue

    I got this code

    Code:
    string line;
    ifstream fin;
    ofstream ofin;
    
    do {
                if (fin.is_open()) {
                    fin.close();
                }
                fin.open(FilePath);
                if (!fin) {          
                    MessageBox(NULL,"Unable to open, file might be missing.","Error",MB_ICONERROR | MB_OK);
                }
            } while (!fin);
                if (fin.is_open()) {
                    while (! fin.eof() ) {
                       getline (fin,line);
                       if (fin.eof()) {
                            SendMessage(hTextbox, WM_SETTEXT, 0,(LPARAM)(LPSTR)line.c_str());
                       }
                    }
                    if (fin.eof()) {
                        SendMessage(hProgress,PBM_SETPOS,100,0);
                    }
                }
    But it only shows the last line of the file loaded.Is there a way to solve this?

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    getline (fin,line);
    if (fin.eof()) {
        SendMessage(hTextbox, WM_SETTEXT, 0,(LPARAM)(LPSTR)line.c_str());
    }
    Look at your code. You read in a line, then set your textbox to that line's text. Every time you send a WM_SETTEXT message, you overwrite the last line.

    You could try this instead:
    Code:
    // move cursor to end of text
    SendMessage(hTextbox, EM_SETSEL, -1,-1);
    // add new line to the end of the edit box
    SendMessage(hTextbox, EM_REPLACESEL, 0,(LPARAM)(LPSTR)line.c_str());

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    29
    Bithub, you solution replaces...just like settext
    Last edited by Nephiroth; 01-30-2006 at 10:45 AM.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Bithub, you solution replaces...just like settext
    Yeah, my code to move the cursor to the end didn't work like I thought it would, you have to get the total length of the text first like anonytmouse's code does.

    Use his code like:
    Code:
    AppendText(hTextbox,"%s",line.c_str());

  6. #6
    Registered User
    Join Date
    Jan 2006
    Posts
    29
    anonytmouse's code isnt working at me.

  7. #7
    Registered User
    Join Date
    Jan 2006
    Posts
    29
    It says that AppendText is not declared
    ...nvm, im using that now.I got this

    Code:
    SendMessage(hTextbox, WM_SETTEXT, 0,0);
                           TCHAR szText[4096];
    	                   va_list args;
    	                   int nTxtLen;
     
    	                   /* Move selection to end of text */
    	                   nTxtLen = GetWindowTextLength(hwnd);
    	                   SendMessage(hwnd, EM_SETSEL, nTxtLen, nTxtLen);
     
    	                   /* Format the text into the buffer */
    	                   va_start(args, line);
    	                   _vsntprintf(szText, sizeof(szText) / sizeof(szText[0]),(LPCSTR)line.c_str(), args);
    	                   szText[(sizeof(szText) / sizeof(szText[0])) - 1] = TEXT('\0');
    	                   va_end(args);
     
    	                   /* Add the text line and scroll it into view */
    	                   SendMessage(hTextbox, EM_REPLACESEL, FALSE, (LPARAM) szText);
    	                   SendMessage(hTextbox, EM_SCROLLCARET, 0, 0);
    Im getting the error:
    `va_start' used in function with fixed args

    ??
    Last edited by Nephiroth; 01-31-2006 at 09:31 AM.

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    If you are not going to put the code into a function, then there is no need to use the variable arguments functions. Get rid of
    Code:
    /* Format the text into the buffer */
    	                   va_start(args, line);
    	                   _vsntprintf(szText, sizeof(szText) / sizeof(szText[0]),(LPCSTR)line.c_str(), args);
    	                   szText[(sizeof(szText) / sizeof(szText[0])) - 1] = TEXT('\0');
    	                   va_end(args);

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > `va_start' used in function with fixed args
    You need
    void foo ( param1, param2, ... );
    If you don't have the ... in the function declaration, then using va macros makes no sense.

  10. #10
    Registered User
    Join Date
    Jan 2006
    Posts
    29
    bithbub: after that, it doesnt show any text at all
    Salem: [Linker error] undefined reference to `foo()'
    while i defined void foo(); above the code, and i called it with foo();

    EDIT: now called it with void foo(); now it links, but still doesnt work, it shows nothing

  11. #11
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Salem: [Linker error] undefined reference to `foo()'
    You're not even trying now...

    You need to understand the code before you use it. Take a minute, and look at the code you are using to display text in the edit box. Go over it line by line until you understand what each statement's purpose is. If you still don't understand why it's not working after doing this, then post again and we'll try to help you.

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Oh for ........s sake, pay attention!!!!
    http://faq.cprogramming.com/cgi-bin/...&id=1043284351

    I used foo() because your crummy broken code didn't even have a function prototype in scope to even quote back at you. Just a bunch or random crappy lines of poorly indented code.

    Sheesh!!!!!!!
    no wait, that's not it
    SHEESH!!!!!!!!!!!!!

    A bit of advice, do not under any circumstances buy a gun. You're simply not equipped to cope with it's point and click interface. Trying stuff and then asking "what does this do" just isn't going to work.

  13. #13
    Registered User
    Join Date
    Jan 2006
    Posts
    29
    ...im just trying to learn, not to get screwed by some guy who thinks everyone should know everything when they are coming on this forums.Im trying to understand the way, im having a hard time with it, thats why i ask for information, but hell, it doesnt matter anyway

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  2. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  3. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM