Input file path in Microsoft Visual C++

This is a discussion on Input file path in Microsoft Visual C++ within the C Programming forums, part of the General Programming Boards category; I am trying to read an input file, but so far I've had to have the file in the same ...

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257

    Input file path in Microsoft Visual C++

    I am trying to read an input file, but so far I've had to have the file in the same folder as the workspace for it to be found. I open it as follows:
    Code:
    inp = fopen("test_outp.bin", "rb");
    I've tried entering a path for the file, ie.
    Code:
    inp = fopen("c:\test\test_outp.bin", "rb");
    But an error message comes up that the file is not found.
    I nkow other compilers can have a path specified for the input files, I can't figure it out with Visual C++.

    Any idea?

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    backslashes need to be "escaped" so...

    inp = fopen("c:\\test\\test_outp.bin", "rb");
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Or use forward slashes.
    Code:
    inp = fopen("c:/test/test_outp.bin", "rb");
    Last edited by Dave_Sinkula; 05-25-2005 at 12:06 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    Thanks.

    A.

  5. #5
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    It refuses to work if the user enters the file path, ie:
    Code:
    int
    main (void)
    {
    	FILE *inp;
    	char InFileName[100];
    
    	printf("Please enter the complete file path for the file to be parceled:\nEx.c:/Test/test_outp.baf\n");
    
        scanf("%s", &InFileName);
    
    	inp = fopen(InFileName, "rb");  
    
    	if(!inp)
    		printf("The file could not be opened.\n");
    
    	return 0;
    }
    If the file provided is in the same directory as the project there is no error. Once you put in a file path, it errors out.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    FAQ > How do I... (Level 1) > Get a line of text from the user/keyboard (C)
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
       char filename[32];
       FILE *file;
       fputs("filename? ", stdout);
       fflush(stdout);
       if ( fgets(filename, sizeof filename, stdin) )
       {
          char *newline = strchr(filename, '\n');
          if ( newline )
          {
             *newline = '\0';
          }
       }
       file = fopen(filename, "r");
       if ( file )
       {
          printf("opened \"%s\"\n", filename);
          fclose(file);
       }
       else
       {
          perror(filename);
       }
       return 0;
    }
    
    /* my output
    filename? c:/test/test.c
    opened "c:/test/test.c"
    */
    
    /* my output
    filename? nothing
    nothing: No such file or directory
    */
    Last edited by Dave_Sinkula; 05-25-2005 at 01:50 PM. Reason: Added second output.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. fscanf in different functions for the same file
    By bchan90 in forum C Programming
    Replies: 5
    Last Post: 12-03-2008, 08:31 PM
  3. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  4. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

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