CreateFile();

This is a discussion on CreateFile(); within the C++ Programming forums, part of the General Programming Boards category; can anyone help me fix this i cant seem to get CreateFile to open anything it compiles allright but does ...

  1. #1
    Banned
    Join Date
    Oct 2004
    Posts
    250

    CreateFile();

    can anyone help me fix this i cant seem to get CreateFile to open anything it compiles allright but does nothing

    Code:
    CreateFile("C:\\Documents and Settings\\wmorrish\\Desktop\\sd.txt",OPEN_EXISTING,
    		NULL,NULL,NULL,NULL,NULL);

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    If you call GetLastError() after that function call you can get the error information.

    About CreateFile() :

    Your OPEN_EXISTING param is not the correct 2nd param. Look it up on MSDN.

    http://msdn.microsoft.com/library/de...createfile.asp
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Banned
    Join Date
    Oct 2004
    Posts
    250
    I tried using the MSDN example but it still wont open the file....
    Code:
    #include <windows.h>
    #include <stdio.h>
    int main()
    {
    	HANDLE hFile; 
    	hFile = CreateFile(TEXT("myfile.txt"),    // file to open
                       GENERIC_READ,          // open for reading
                       FILE_SHARE_READ,       // share for reading
                       NULL,                  // default security
                       OPEN_EXISTING,         // existing file only
                       FILE_ATTRIBUTE_NORMAL, // normal file
                       NULL); 
    
    	if (hFile == INVALID_HANDLE_VALUE) 
    	{
    		printf("Could not open file (error %d)\n", GetLastError());
    		return 0;
    	}
    }

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Does that file exist? So it gives you an actual error? what's the value of the error?

    If it's error value is 2 then that means it cannot find the file. Make sure it exists in the correct path.

    The code above should work fine.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    Banned
    Join Date
    Oct 2004
    Posts
    250
    it doesnt give any error the file just wont open

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    What is it you are trying to do? If it doesn't output the error then that means the file was successfully opened and you now have a handle to the file to do whatever you want with. So maybe you should just post what you want to do and then maybe I can help you some more.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    i don't think most standard compilers support programmers with more than 4 red boxes
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  8. #8
    Banned
    Join Date
    Oct 2004
    Posts
    250
    i just want to open the file like system("myfile");

  9. #9
    Bob Dole for '08 B0bDole's Avatar
    Join Date
    Sep 2004
    Posts
    618
    "opening" the file with the code above, opens it to the program to read/write to...it doesnt make the file pop open...
    Hmm

  10. #10
    Banned
    Join Date
    Oct 2004
    Posts
    250
    is there any way to do that?

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    Try some of the other windows functions listed at the end of this FAQ entry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CreateFile() and CONOUT$
    By cboard_member in forum Windows Programming
    Replies: 1
    Last Post: 06-30-2006, 03:30 PM
  2. CreateFile() giving errors
    By neandrake in forum Windows Programming
    Replies: 2
    Last Post: 09-27-2004, 11:29 PM
  3. Serial comm W32 using CreateFile
    By Foldager in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2003, 02:13 PM
  4. fopen and CreateFile
    By Bajanine in forum Windows Programming
    Replies: 3
    Last Post: 04-03-2003, 07:24 PM
  5. CreateFile and Long Path/Files :: Win32
    By kuphryn in forum Windows Programming
    Replies: 4
    Last Post: 12-27-2002, 10:26 PM

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