what am I doing wrong

This is a discussion on what am I doing wrong within the C Programming forums, part of the General Programming Boards category; hi, I'm trying to open a file from a command line argument and if it gets open print a message. ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    24

    Unhappy what am I doing wrong

    hi,
    I'm trying to open a file from a command line argument and if it gets open
    print a message.
    it doesn't work
    what am I doing wrong.
    Code:
    #include <stdafx.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <string.h>
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    	FILE *input;
    	if (argc = 2) {
    		input = fopen(argv[1],"r");
    		if (input ==NULL){
    			printf ("cannot open the file");
    		}else {
    			printf ("\n File opened successfully \n");
    		}
    	fclose(input);	
    	}
    return 0;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    WHAT doesn't work?

    Aside from setting argc to two instead of checking if it is two, that is?

    Of course, if you are compiling with widechars, fopen probably doesn't work that well, but I expect the compiler to warn about that (at the least).

    Whilst I think it's not a problem right here, you may want to make sure the file-pointer isn't NULL when calling fclose() - I don't believe it's "safe" to call fopen() with a NULL parameter at all times in all systems.

    --
    Mats

  3. #3
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    What doesn't work? The program? Opening a file?

    Looking at the code, I would question whether it could compile without including "tchar.h". Other than that the code looks ok. So if you are saying the file is not opening, then I would check that the file exist and you are giving it on the command line spelled exactly with the extension

  4. #4
    Registered User
    Join Date
    Jul 2007
    Posts
    24
    I just want to see if I can open a file successfully from command line
    print a message if it opens successfully

  5. #5
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by cyber_tech View Post
    I just want to see if I can open a file successfully from command line
    print a message if it opens successfully
    We know WHAT you are trying to do, we just don't know WHAT doesn't work.

    1. Does it compile?
    2. Does it give any runtime errors?
    3. If it runs, what's the output? Is it what you expected? No? Why Not?

  6. #6
    Registered User
    Join Date
    Jul 2007
    Posts
    24
    it doesn't compile,
    and when I try to run it from command line it gives the message file cannot be opened,
    and it says jit debugging component registration is incprrect.

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    24
    and when I try to compile it it gives the following message:
    myprog2 fatal error LNK1201: error writing to program database 'c:\Documents and Settings\Alex Karimian\My Documents\Visual Studio Projects\myprog2\Debug\myprog2.pdb'; check for insufficient disk space, invalid path, or insufficient privilege

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    Quote Originally Posted by cyber_tech View Post
    and when I try to compile it it gives the following message:
    myprog2 fatal error LNK1201: error writing to program database 'c:\Documents and Settings\Alex Karimian\My Documents\Visual Studio Projects\myprog2\Debug\myprog2.pdb'; check for insufficient disk space, invalid path, or insufficient privilege
    Your linker thinks that pdb file is old. Delete that file and rebuild your project (after making the code corrections you've been advised to make).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 04:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 01:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 02:04 PM

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