Thread: directory path coding

  1. #16
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So you are passing some data block to a system call, that isn't the right size. What is the system call on the line before your error?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #17
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I'm not entirely confident that the CString default constructor doesn't do something. Place that declaration before the CreateFile call and try again.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #18
    Registered User
    Join Date
    Nov 2007
    Posts
    74
    What is the system call on the line before your error?>

    what does that mean?
    i put following code:
    Code:
    HANDLE hFile = CreateFile(TEXT("D:\\file.txt"), GENERIC_READ | GENERIC_WRITE, 
                       FILE_SHARE_READ,
                       NULL,
                       OPEN_ALWAYS,
                       FILE_ATTRIBUTE_NORMAL,
                       NULL);
    
    
    	
    CString debug;
    	if (INVALID_HANDLE_VALUE == hFile){// if setting/file not valid
    		DWORD dw = GetLastError ();
    		debug = "Step 12";
    		detail.DetailValue = debug;
    }
    detail.DoModal();
    and when i click on the item on the listbox, it will connect to the txt file to display the content for that item, but when i click the item i got the error msg and display step 12.

    should i add some code like free the buffer and how? thanks!!

  4. #19
    Registered User
    Join Date
    Nov 2007
    Posts
    74
    Quote Originally Posted by Elysia View Post
    Any reason you're using CreateFile instead of std::istream?
    i 've used istream to open and read the file, but how do i put the data into the CString list box?
    thanks!!

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    CString my_str;
    CListBox m_list;
    // Alternatively: CString m_list;
    // Assuming you've opened the file with an istream obect named my_file
    my_file.readline(my_str.GetBuffer(10000), 10000);
    my_str.ReleaseBuffer();
    m_list.AddString(my_str);
    GetBuffer returns a char* of the specified size from the CString object. See documentation.
    readline reads a line from the input file, see basic_istream and readline in docs.
    ReleaseBuffer is required after you've called GetBuffer to read data in a CString object.
    Then just add the string to the listbox.
    istream doesn't support reading into CString by default. There are other ways to do it, I'm sure, though.
    Last edited by Elysia; 11-20-2007 at 03:12 AM.

  6. #21
    Registered User
    Join Date
    Nov 2007
    Posts
    74
    Quote Originally Posted by Elysia View Post
    Code:
    CString my_str;
    CListBox m_list;
    // Alternatively: CString m_list;
    // Assuming you've opened the file with an istream obect named my_file
    my_file.readline(my_str.GetBuffer(10000), 10000);
    my_str.ReleaseBuffer();
    m_list.AddString(my_str);
    GetBuffer returns a char* of the specified size from the CString object. See documentation.
    readline reads a line from the input file, see basic_istream and readline in docs.
    ReleaseBuffer is required after you've called GetBuffer to read data in a CString object.
    Then just add the string to the listbox.
    istream doesn't support reading into CString by default. There are other ways to do it, I'm sure, though.
    actually i code this to test:
    Code:
    string line;
    ostringstream cout;
      ifstream myfile ("D:\\file.txt");
      if (myfile.is_open())
      {
        while (! myfile.eof() )
        {
          getline (myfile,line);
          cout << line << endl;
    	  string line2 = cout.str();
    
    	  const char* line3 = line2.c_str();
    
    detail.DetailValue = line3;
        }
        myfile.close();
      }
    
      else detail.DetailValue = "Unable to open file"; 
    
      
    
    
    
    
    
    		
    	detail.DoModal();
    	
    }
    but it still can't open the file, i doubt that is still the buffer problem, i really dun know how to solve this problem, do u know how ? thanks a lot!!

  7. #22
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, for one, do the actual file actually exist?
    And your code is poor is so many ways - does it even compile?
    First, cout is a reserved global ostream object - it's bad to hide it with your local varible, choose another name.
    while(! myfile.eof() ) is bad. See FAQ.
    getline is not a global function, it's part of the basic_istream object (myfile).
    cout.str() <--- ???? str() isn't even a member function of a basic_ostream object! Not to mention you're trying to so WHAT? Read the contents of the written file or something?

  8. #23
    Registered User
    Join Date
    Nov 2007
    Posts
    74
    Quote Originally Posted by Elysia View Post
    Well, for one, do the actual file actually exist?
    And your code is poor is so many ways - does it even compile?
    First, cout is a reserved global ostream object - it's bad to hide it with your local varible, choose another name.
    while(! myfile.eof() ) is bad. See FAQ.
    getline is not a global function, it's part of the basic_istream object (myfile).
    cout.str() <--- ???? str() isn't even a member function of a basic_ostream object! Not to mention you're trying to so WHAT? Read the contents of the written file or something?
    the file exists, and those coding can be compiled, anyway, i 've modified the coding:
    Code:
    CString debug;
    CString myfile;
    myfile = "\\D:\\file.txt";
    HANDLE hFile = CreateFile(myfile.GetBuffer(10000), GENERIC_READ | GENERIC_WRITE, 
                       FILE_SHARE_READ,
                       NULL,
                       OPEN_ALWAYS,
                       FILE_ATTRIBUTE_NORMAL,
                       NULL);
    
    myfile.ReleaseBuffer();
    	
    
    	if (INVALID_HANDLE_VALUE == hFile){// if setting/file not valid
    		DWORD dw = GetLastError ();
    		debug = "Step 12";
    		detail.DetailValue = debug;
    	}
    
    }
    is that correct? but the result i got is no response instead of display step 12..

    could u suggest any coding to me to cope with it? exhausted..help!!and thanks very much!!

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by mr_empty View Post
    Code:
    CString debug;
    CString myfile;
    myfile = "\\D:\\file.txt";
    HANDLE hFile = CreateFile(myfile, GENERIC_READ | GENERIC_WRITE, 
                       FILE_SHARE_READ,
                       NULL,
                       OPEN_ALWAYS,
                       FILE_ATTRIBUTE_NORMAL,
                       NULL);
    
    	if (INVALID_HANDLE_VALUE == hFile){// if setting/file not valid
    		DWORD dw = GetLastError ();
    		debug = "Step 12";
    		detail.DetailValue = debug;
    	}
    
    }
    GetBuffer/ReleaseBuffer is only required when writing data into the string buffer which you're not doing.
    But I can't say what's wrong - because the code is correct. You must pay heed to what GetLastError() says.
    What does GetLastError return? What error?

  10. #25
    Registered User
    Join Date
    Nov 2007
    Posts
    74
    Quote Originally Posted by Elysia View Post
    GetBuffer/ReleaseBuffer is only required when writing data into the string buffer which you're not doing.
    But I can't say what's wrong - because the code is correct. You must pay heed to what GetLastError() says.
    What does GetLastError return? What error?
    the error is "the data area passed to a system call is too small",
    and search on the internet its the problem related to buffer
    i just can't understand and how to solve it
    Last edited by mr_empty; 11-20-2007 at 04:15 AM.

  11. #26
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Are you by any chance using uni-code? Perhaps you want the TCHAR *file = TEXT("filename") instead of your version. If you look at either the pre-processed output or assembler listing from the compiler, you should see if it calls CreateFileA or CreateFileW.

    And you still need either no backslashes at the front of the name, or another two - so that after the compiler has removed the escape ones, you end up with two backslashes at the front of the string.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #27
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    For the error - I have no idea, but as matsp indiciates "\D:\File.txt" is an invalid filename. CreateFile will fail with error 123 - invalid filename.
    Aside from that, what is D:\? Is it a hard disk? A floppy drive? A CD-ROM? A USB drive?

  13. #28
    Registered User
    Join Date
    Nov 2007
    Posts
    74
    Quote Originally Posted by matsp View Post
    Are you by any chance using uni-code? Perhaps you want the TCHAR *file = TEXT("filename") instead of your version. If you look at either the pre-processed output or assembler listing from the compiler, you should see if it calls CreateFileA or CreateFileW.

    And you still need either no backslashes at the front of the name, or another two - so that after the compiler has removed the escape ones, you end up with two backslashes at the front of the string.

    --
    Mats
    thanks for reply, your two suggestion i've tried, but not work

  14. #29
    Registered User
    Join Date
    Nov 2007
    Posts
    74
    Quote Originally Posted by Elysia View Post
    For the error - I have no idea, but as matsp indiciates "\D:\File.txt" is an invalid filename. CreateFile will fail with error 123 - invalid filename.
    Aside from that, what is D:\? Is it a hard disk? A floppy drive? A CD-ROM? A USB drive?
    d:\ is hard disk, and i use D:\\file.txt instead of \D:\File.txt

  15. #30
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by mr_empty View Post
    D:\\file.txt instead of \D:\File.txt
    D:\\file.txt == D:\file.txt
    \D:\File.txt == \\D:\\File.txt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Couple errors please help :-D
    By JJJIrish05 in forum C Programming
    Replies: 9
    Last Post: 03-06-2008, 02:54 AM
  2. Adding a directory to a dynamic library path
    By vivharv in forum Windows Programming
    Replies: 3
    Last Post: 09-20-2007, 07:09 AM
  3. Accessing the 'home' directory path?
    By smoothdogg00 in forum C Programming
    Replies: 3
    Last Post: 04-30-2006, 10:51 AM
  4. Directory reading trouble
    By samGwilliam in forum Linux Programming
    Replies: 0
    Last Post: 03-10-2002, 09:43 AM
  5. File Input/Output Path Directory
    By tegwin in forum C++ Programming
    Replies: 7
    Last Post: 02-27-2002, 03:00 PM